public class CaffeineCacheManager extends Object implements CacheManager
CacheManager implementation that lazily builds CaffeineCache
instances for each getCache(java.lang.String) request. Also supports a 'static' mode
where the set of cache names is pre-defined through setCacheNames(java.util.Collection<java.lang.String>),
with no dynamic creation of further cache regions at runtime.
The configuration of the underlying cache can be fine-tuned through a
Caffeine builder, passed into this CacheManager through
setCaffeine(com.github.benmanes.caffeine.cache.Caffeine<java.lang.Object, java.lang.Object>).
Requires Caffeine 2.0 or higher.
CaffeineCache| Constructor and Description |
|---|
CaffeineCacheManager()
Construct a dynamic CaffeineCacheManager,
lazily creating cache instances as they are being requested.
|
CaffeineCacheManager(String... cacheNames)
Construct a static CaffeineCacheManager,
managing caches for the specified cache names only.
|
| Modifier and Type | Method and Description |
|---|---|
protected Cache |
createCaffeineCache(String name)
Create a new CaffeineCache instance for the specified cache name.
|
protected com.github.benmanes.caffeine.cache.Cache<Object,Object> |
createNativeCaffeineCache(String name)
Create a native Caffeine Cache instance for the specified cache name.
|
Cache |
getCache(String name)
Return the cache associated with the given name.
|
Collection<String> |
getCacheNames()
Return a collection of the cache names known by this manager.
|
boolean |
isAllowNullValues()
Return whether this cache manager accepts and converts
null values
for all of its caches. |
void |
setAllowNullValues(boolean allowNullValues)
Specify whether to accept and convert
null values for all caches
in this cache manager. |
void |
setCacheLoader(com.github.benmanes.caffeine.cache.CacheLoader<Object,Object> cacheLoader)
Set the Caffeine CacheLoader to use for building each individual
CaffeineCache instance, turning it into a LoadingCache. |
void |
setCacheNames(Collection<String> cacheNames)
Specify the set of cache names for this CacheManager's 'static' mode.
|
void |
setCaffeine(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> cacheBuilder)
Set the Caffeine to use for building each individual
CaffeineCache instance. |
public CaffeineCacheManager()
public CaffeineCacheManager(String... cacheNames)
public void setCacheNames(Collection<String> cacheNames)
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.
public void setCaffeine(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> cacheBuilder)
CaffeineCache instance.createNativeCaffeineCache(java.lang.String),
Caffeine.build()public void setCacheLoader(com.github.benmanes.caffeine.cache.CacheLoader<Object,Object> cacheLoader)
CaffeineCache instance, turning it into a LoadingCache.createNativeCaffeineCache(java.lang.String),
Caffeine.build(CacheLoader),
LoadingCachepublic void setAllowNullValues(boolean allowNullValues)
null values for all caches
in this cache manager.
Default is "true", despite Caffeine itself not supporting null values.
An internal holder object will be used to store user-level nulls.
public boolean isAllowNullValues()
null values
for all of its caches.public Collection<String> getCacheNames()
CacheManagergetCacheNames in interface CacheManagerpublic Cache getCache(String name)
CacheManagergetCache in interface CacheManagername - the cache identifier (must not be null)null if none foundprotected Cache createCaffeineCache(String name)
name - the name of the cache