Emil Kantis
10/19/2021, 5:59 PM@Cacheable
method. Since @Cacheable
is broken for suspend functions, I tried to do this with reactor-types for the specific method instead. Looks something like this:
@Cacheable("my-cache")
fun fetchThing(key: String): Mono<Thing> =
webClient.get("/thing/{key}", key)
.retrieve()
.bodyToMono()
Now, in the code where I use this. I want to bridge back into awesome realm of suspend (๐), so I do: restClient.fetchThing(key).awaitSingle()
. However, it seems that invoking awaitSingle()
twice on a Mono
created this way will actually cause the webrequest to fire again, instead of lazily returning the already existing result in the Mono
.. Does anyone know the proper way to get back to suspend functions after creating a Mono like this? Should I map the mono to a lazy mono of first result or something?nfrankel
10/19/2021, 6:00 PMEmil Kantis
10/19/2021, 6:01 PMnfrankel
10/19/2021, 6:04 PMEmil Kantis
10/19/2021, 6:05 PMnfrankel
10/19/2021, 6:06 PMEmil Kantis
10/19/2021, 6:13 PMsuspend fun fetchThing(key: String): Thing =
thingCache.get(key, Thing::class.java) ?: webClient.get()
.uri("/thing/{key}", key)
.retrieve()
.awaitBody<Thing>().also {
offeringCache.put(key, it)
}
I guess this will put prometheus metrics that @Cacheable
normally generates out of play however.. ๐nfrankel
10/19/2021, 6:18 PM@Cacheable
just hides the magic
but metrics depend on the underlying cache
what are you using?Emil Kantis
10/19/2021, 6:18 PMCache
and CacheManager
are both registered in Spring context, so maybe it'll work then ๐คnfrankel
10/19/2021, 6:32 PMEmil Kantis
10/19/2021, 6:40 PM# HELP cache_gets_total the number of times cache lookup methods have returned an uncached (newly loaded) value, or null
# TYPE cache_gets_total counter
cache_gets_total{cache="things",cacheManager="cacheManager",name="things",result="hit",} 8.0
cache_gets_total{cache="things",cacheManager="cacheManager",name="things",result="miss",} 1.0
Emil Kantis
10/19/2021, 6:40 PMnfrankel
10/19/2021, 6:42 PM