I'm creating a REST-client with a `@Cacheable` met...
# spring
e
I'm creating a REST-client with a
@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:
Copy code
@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?
n
do you need annotations?
e
Hmm, not really, tbh prefer not to.. I just need to cache 🙂
e
Thanks, I'll give that a shot 🙂
👍 1
n
hope it helps
e
It did. Integration test succeeds now.
Copy code
suspend 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.. 😕
🎉 1
n
not sure...
@Cacheable
just hides the magic but metrics depend on the underlying cache what are you using?
e
Using Caffeine, and the
Cache
and
CacheManager
are both registered in Spring context, so maybe it'll work then 🤔
n
yes, it has nothing to do with annotation the annotation will just make the method a proxy and spring will check the cache before calling the real method under the cover
e
True. And it's working.. 🙂
Copy code
# 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
👏 1
Thanks a lot Nicolas 🙇🙂
n
you’re welcome!
205 Views