sksamuel / aedile

Kotlin Wrapper for Caffeine
Apache License 2.0
170 stars 15 forks source link

How to override com.github.benmanes.caffeine.cache.AsyncCacheLoader#asyncReload method #17

Closed hu553in closed 1 year ago

hu553in commented 1 year ago

Hello!

When using builder, how com.github.benmanes.caffeine.cache.AsyncCacheLoader#asyncReload method can be overriden?

For me it should be something like this (not exactly, it's just a stupid example) in com.sksamuel.aedile.core.Builder class:

   fun build(compute: suspend (K) -> V, reloadCompute: suspend (K, V) -> V): LoadingCache<K, V> {
      return LoadingCache(scope, caffeine.buildAsync(object : AsyncCacheLoader<K, V> {
         override fun asyncLoad(key: K, executor: Executor?): CompletableFuture<out V> {
            return scope.async { compute(key) }.asCompletableFuture()
         }

         override fun asyncReload(key: K, oldValue: V, executor: Executor?): CompletableFuture<out V> {
            return scope.async { reloadCompute(key, oldValue) }.asCompletableFuture()
         }
      }))
   }

What do you think?

Am I missing something?

Looked through docs and issues, but haven't found anything about this.

If this functionality is just missing, maybe I can try to prepare a PR...

Or maybe there is some workaround?

sksamuel commented 1 year ago

Thanks for the suggestion. I've added this to 1.2.4 if you want to give it a try.

hu553in commented 1 year ago

Nice, thank you! :+1: