Reactor CacheMono Kotlin lookup type problems

Viewed 349

I have a project where i use Reactor with Spring Cloud Gateway (version Hoxton.SR5) I wanted to implement a cache for data that is queried by the gateway from other services.

From what i've read, the @Cacheable annotations aren't compatible with reactor 3.3, so i went to the CacheMono in combination with Caffeine.

The code i'm trying to write should do a fairly simple job:

  • Check Cache for entry
  • If missed, query the service
    • Create a mono
    • After mono returns, put the value inside the cache
  • If cache hits, just return that Mono

The abomination of code (function getFromSource returns a Mono via Webclient):


    var myCache: Cache<String, DomainObject> = Caffeine.newBuilder()
        .expireAfterWrite(1, TimeUnit.MINUTES)
        .maximumSize(1000)
        .build()

    fun get(id: String): Mono<DomainObject> {
        return CacheMono
            .lookup(
                { key ->
                    Mono
                        .justOrEmpty(myCache.getIfPresent(key))
                        .map {s ->
                            Signal.next(s)
                        }
                },
                id
            )
            .onCacheMissResume {
                getFromSource(id)
            }
            .andWriteWith { (key, signal) ->
                Mono.fromRunnable {
                    Optional
                        .ofNullable(signal.get())
                        .ifPresent { value: DomainObject ->
                            myCache.put(key, value)
                        }
                }
            }
    }

It's complaining about the following:

Type inference failed: fun <KEY : Any!, VALUE : Any!> lookup(p0: (KEY!) -> Mono<Signal<out VALUE!>!>!, p1: KEY): CacheMono.MonoCacheBuilderCacheMiss<KEY!, VALUE!>
cannot be applied to
((String!) -> Mono<Signal<@Nullable DomainObject!>!>,String)

Help is gladly appreciated.

1 Answers

Provide type parameters to lookup method:

.lookup<String, DomainObject>(
Related