Maurice
Maurice

Reputation: 7401

How to add a new cache to a CaffeineCacheManager

I'm looking to dynamically add new caches to an already initialized CaffeineCacheManager instance.

The class doesn't seem to have an obvious method to do this. I could add a new cache name via CaffeineCacheManager.getCacheNames().add() but i'm not sure thats the proper way.

I'm thinking this because the documentation of the method setCacheNames() basically states that you can't add any new caches after you've changed the collection once. Heres the actual documentation:

Specify the set of cache names for this CacheManager's 'static' mode. The number of caches and their names will be fixed after a call to this method, with no creation of further cache regions at runtime. Calling this with a null collection argument resets the mode to 'dynamic', allowing for further creation of caches again

Lastly there is the method registerCustomCache() that could be of use. But the documentation of this method is really confusing.

Register the given native Caffeine Cache instance with this cache manager, adapting it to Spring's cache API for exposure through getCache(java.lang.String). Any number of such custom caches may be registered side by side. This allows for custom settings per cache (as opposed to all caches sharing the common settings in the cache manager's configuration) and is typically used with the Caffeine builder API: registerCustomCache("myCache", Caffeine.newBuilder().maximumSize(10).build()) Note that any other caches, whether statically specified through setCacheNames(java.util.Collection<java.lang.String>) or dynamically built on demand, still operate with the common settings in the cache manager's configuration

The last part is especially confusing:

Note that any other caches, whether statically specified through setCacheNames(java.util.Collection<java.lang.String>) or dynamically built on demand, still operate with the common settings in the cache manager's configuration.

When i use registerCustomCache() to add a cache i'm basically building the cache dynamically and using this method allows for custom settings per cache but then a few lines below the documentation suddenly says that they all still operate with the common settings in the cache manager's configuration.

So i'm a bit confused now. Please tell me which method i should use to dynamically add a new cache to the cache manager.

Thank you

Upvotes: 0

Views: 1225

Answers (0)

Related Questions