Class CaffeineCacheManager
- java.lang.Object
- org.springframework.cache.caffeine.CaffeineCacheManager
- All Implemented Interfaces:
CacheManager
public class CaffeineCacheManager extends Object implements CacheManager
CacheManager
implementation that lazily buildsCaffeineCache
instances for eachgetCache(java.lang.String)
request. Also supports a 'static' mode where the set of cache names is pre-defined throughsetCacheNames(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 orCaffeineSpec
, passed into this CacheManager throughsetCaffeine(com.github.benmanes.caffeine.cache.Caffeine<java.lang.Object, java.lang.Object>)
/setCaffeineSpec(com.github.benmanes.caffeine.cache.CaffeineSpec)
. ACaffeineSpec
-compliant expression value can also be applied via the"cacheSpecification"
bean property.Requires Caffeine 2.1 or higher.
- Since:
- 4.3
- Author:
- Ben Manes, Juergen Hoeller, Stephane Nicoll
- See Also:
CaffeineCache
Constructor Summary
Constructors Constructor 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.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
Get the cache associated with the given name.Collection<String>
getCacheNames()
Get a collection of the cache names known by this manager.boolean
isAllowNullValues()
Return whether this cache manager accepts and convertsnull
values for all of its caches.void
setAllowNullValues(boolean allowNullValues)
Specify whether to accept and convertnull
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 individualCaffeineCache
instance, turning it into a LoadingCache.void
setCacheNames(Collection<String> cacheNames)
Specify the set of cache names for this CacheManager's 'static' mode.void
setCacheSpecification(String cacheSpecification)
Set the Caffeine cache specification String to use for building each individualCaffeineCache
instance.void
setCaffeine(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> caffeine)
Set the Caffeine to use for building each individualCaffeineCache
instance.void
setCaffeineSpec(com.github.benmanes.caffeine.cache.CaffeineSpec caffeineSpec)
Set theCaffeineSpec
to use for building each individualCaffeineCache
instance.
Constructor Detail
CaffeineCacheManager
public CaffeineCacheManager()
Construct a dynamic CaffeineCacheManager, lazily creating cache instances as they are being requested.
CaffeineCacheManager
public CaffeineCacheManager(String... cacheNames)
Construct a static CaffeineCacheManager, managing caches for the specified cache names only.
Method Detail
setCacheNames
public void setCacheNames(Collection<String> cacheNames)
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.
setCaffeine
public void setCaffeine(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> caffeine)
Set the Caffeine to use for building each individualCaffeineCache
instance.- See Also:
createNativeCaffeineCache(java.lang.String)
,Caffeine.build()
setCaffeineSpec
public void setCaffeineSpec(com.github.benmanes.caffeine.cache.CaffeineSpec caffeineSpec)
Set theCaffeineSpec
to use for building each individualCaffeineCache
instance.- See Also:
createNativeCaffeineCache(java.lang.String)
,Caffeine.from(CaffeineSpec)
setCacheSpecification
public void setCacheSpecification(String cacheSpecification)
Set the Caffeine cache specification String to use for building each individualCaffeineCache
instance. The given value needs to comply with Caffeine'sCaffeineSpec
(see its javadoc).- See Also:
createNativeCaffeineCache(java.lang.String)
,Caffeine.from(String)
setCacheLoader
public void setCacheLoader(com.github.benmanes.caffeine.cache.CacheLoader<Object,Object> cacheLoader)
Set the Caffeine CacheLoader to use for building each individualCaffeineCache
instance, turning it into a LoadingCache.- See Also:
createNativeCaffeineCache(java.lang.String)
,Caffeine.build(CacheLoader)
,LoadingCache
setAllowNullValues
public void setAllowNullValues(boolean allowNullValues)
Specify whether to accept and convertnull
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-levelnull
s.
isAllowNullValues
public boolean isAllowNullValues()
Return whether this cache manager accepts and convertsnull
values for all of its caches.
getCacheNames
public Collection<String> getCacheNames()
Description copied from interface:CacheManager
Get a collection of the cache names known by this manager.- Specified by:
getCacheNames
in interfaceCacheManager
- Returns:
- the names of all caches known by the cache manager
getCache
public Cache getCache(String name)
Description copied from interface:CacheManager
Get the cache associated with the given name.Note that the cache may be lazily created at runtime if the native provider supports it.
- Specified by:
getCache
in interfaceCacheManager
- Parameters:
name
- the cache identifier (must not benull
)- Returns:
- the associated cache, or
null
if such a cache does not exist or could be not created
createCaffeineCache
protected Cache createCaffeineCache(String name)
Create a new CaffeineCache instance for the specified cache name.- Parameters:
name
- the name of the cache- Returns:
- the Spring CaffeineCache adapter (or a decorator thereof)
createNativeCaffeineCache
protected com.github.benmanes.caffeine.cache.Cache<Object,Object> createNativeCaffeineCache(String name)
Create a native Caffeine Cache instance for the specified cache name.- Parameters:
name
- the name of the cache- Returns:
- the native Caffeine Cache instance