Affects: 5.2.6
It is quite common to have multiple caches with different value lifetime. Right now CaffeineCacheManager creates every Cache with the default settings or the settings set through either of setCacheLoader, setCacheSpecification or setCaffeineSpec. But those settings apply to all Caches. If cacheMap would be exposed (maybe just a simple getter) it would be possible to add prebuilt caches with custom settings to it.
Comment From: jhoeller
I suppose we could alternatively introduce dedicated registration methods for individual caches, e.g. taking cache name plus cache specification, or potentially taking a callback for creating the native cache instance... I'd rather make the use case first-class than expose the inner map here, e.g.: registerCache(String name, com.github.benmanes.caffeine.cache.Cache cache)
If you'd like full control of individual caches, you could also use SimpleCacheManager
and populate it with a collection of CaffeineCache
instances of your choice.
Comment From: T3rm1
I like the idea with registerCache(String name, com.github.benmanes.caffeine.cache.Cache cache)
which then puts the cache into the map. I think it is better than the callback approach.
Comment From: jhoeller
I've just implemented such a registerCustomCache
method, the only hurdle having been that setCaffeineSpec
etc calls on the CaffeineCacheManager
should only reinitialize default caches but not such custom cache registrations (that's also the reason for the registerCustomCache
naming). In any case, it seems to flow nicely with any kind of Caffeine.newBuilder()
or Caffeine.from(...)
usage.
Alright, I'll roll this into 5.2.8 right away if this matches your use case as well :-)
Comment From: T3rm1
Awesome! Thanks for doing this so fast. :beer:
Comment From: Praveen1027
Hi. Is there a way I can get the generics in place while attempting to registerCustomCache(spring), something like :
Cache<String, String> cityCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.DAYS).maximumSize(1000).build();
caffeineCacheManager.registerCustomCache("city_data", cityCache);// this fails