2

I'm working with the Scala version of Caffeine, Scaffeine. I'm trying to populate a cache, in a way that it should reload all the values after expiration (10sec in the test). I probably I could use Guava's Suppliers.memoizeWithExpiration but I'd like to take the advantage of the asynchronous loading of data in the background. I have the following snippet:

val dummy = List()
val myCache: AsyncLoadingCache[String, String] =
  Scaffeine()
    .refreshAfterWrite(10.second)  //test
    .buildAsyncFuture(
      loader = userId => client.getByUserId(userId),
      allLoader = Some(dummy => client.getAllUsers),
      reloadLoader = None)

I'd thought that allLoader is for this purpose, but it's called only once during cache construction time, and only those values are populated for which the keys are enumerated in the "dummy" list.

There's a similar thread which suggests to implement CacheLoader#loadAll but any hint would be great how to implement this possibly in Scala with Scaffeine?

Bruckwald
  • 797
  • 8
  • 23
  • 3
    Perhaps a problem is that refresh is per-key and [batch refresh](https://github.com/ben-manes/caffeine/issues/7) is not yet implemented. If you always want the full list then adapting `memoizeWithExpiration` is probably cleaner. You might use a scheduling thread to refresh, since both Guava and Caffeine reload on the first stale call (so may be dated if low activity). – Ben Manes Oct 24 '16 at 16:16
  • Thank you for the clarification! – Bruckwald Oct 25 '16 at 13:51

0 Answers0