Top Description Fields Constructors Methods
org.springframework.cache.caffeine

public Class CaffeineCacheManager

extends Object
implements CacheManager
Class Inheritance
All Implemented Interfaces
org.springframework.cache.CacheManager
Imports
java.util.Arrays, .Collection, .Collections, .Map, java.util.concurrent.ConcurrentHashMap, .CopyOnWriteArrayList, java.util.function.Supplier, com.github.benmanes.caffeine.cache.AsyncCache, .AsyncCacheLoader, .CacheLoader, .Caffeine, .CaffeineSpec, org.springframework.cache.Cache, .CacheManager, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils

CacheManager implementation that lazily builds CaffeineCache instances for each getCache request. Also supports a 'static' mode where the set of cache names is pre-defined through setCacheNames, with no dynamic creation of further cache regions at runtime.

The configuration of the underlying cache can be fine-tuned through a Caffeine builder or CaffeineSpec, passed into this CacheManager through setCaffeine/setCaffeineSpec. A CaffeineSpec-compliant expression value can also be applied via the "cacheSpecification" bean property.

Supports the asynchronous Cache#retrieve(Object) and Cache#retrieve(Object, Supplier) operations through Caffeine's AsyncCache, when configured via setAsyncCacheMode, with early-determined cache misses.

Requires Caffeine 3.0 or higher, as of Spring Framework 6.1.

Authors
Ben Manes, Juergen Hoeller, Stephane Nicoll, Sam Brannen, Brian Clozel
Since
4.3
See Also
CaffeineCache, setCaffeineSpec, setCacheSpecification, setAsyncCacheMode

Field Summary

Modifier and TypeField and Description
private boolean
private boolean
private Caffeine<Object, Object>
private AsyncCacheLoader<Object, Object>
private final Map<String, Cache>
private final Collection<String>
private boolean

Constructor Summary

AccessConstructor and Description
public
CaffeineCacheManager()

Construct a dynamic CaffeineCacheManager, lazily creating cache instances as they are being requested.

public
CaffeineCacheManager(String... cacheNames)

Construct a static CaffeineCacheManager, managing caches for the specified cache names only.

Method Summary

Modifier and TypeMethod and Description
protected Cache

Returns:

the Spring CaffeineCache adapter (or a decorator thereof)
adaptCaffeineCache
(String
the name of the cache
name
,
Cache<Object, Object>
the native Caffeine Cache instance
cache
)

Adapt the given new native Caffeine Cache instance to Spring's Cache abstraction for the specified cache name.

protected Cache

Returns:

the Spring CaffeineCache adapter (or a decorator thereof)
adaptCaffeineCache
(String
the name of the cache
name
,
AsyncCache<Object, Object>
the Caffeine AsyncCache instance
cache
)

Adapt the given new Caffeine AsyncCache instance to Spring's Cache abstraction for the specified cache name.

protected AsyncCache<Object, Object>

Returns:

the Caffeine AsyncCache instance
createAsyncCaffeineCache
(String
the name of the cache
name
)

Build a common Caffeine AsyncCache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.

protected Cache

Returns:

the Spring CaffeineCache adapter (or a decorator thereof)
createCaffeineCache
(String
the name of the cache
name
)

Build a common CaffeineCache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.

protected Cache<Object, Object>

Returns:

the native Caffeine Cache instance
createNativeCaffeineCache
(String
the name of the cache
name
)

Build a common Caffeine Cache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.

private void
public Cache
getCache(String
the cache identifier (must not be null)
name
)

Implements org.springframework.cache.CacheManager.getCache.

Get the cache associated with the given name.

public Collection<String>
getCacheNames()

Implements org.springframework.cache.CacheManager.getCacheNames.

Get a collection of the cache names known by this manager.

public boolean
isAllowNullValues()

Return whether this cache manager accepts and converts null values for all of its caches.

private void
refreshCommonCaches()

Recreate the common caches with the current state of this manager.

public void
registerCustomCache(String
the name of the cache
name
,
Cache<Object, Object>
the custom Caffeine Cache instance to register
cache
)

Register the given native Caffeine Cache instance with this cache manager, adapting it to Spring's cache API for exposure through getCache.

public void
registerCustomCache(String
the name of the cache
name
,
AsyncCache<Object, Object>
the custom Caffeine AsyncCache instance to register
cache
)

Register the given Caffeine AsyncCache instance with this cache manager, adapting it to Spring's cache API for exposure through getCache.

public void
setAllowNullValues(boolean allowNullValues)

Specify whether to accept and convert null values for all caches in this cache manager.

public void
setAsyncCacheLoader(AsyncCacheLoader<Object, Object> cacheLoader)

Set the Caffeine AsyncCacheLoader to use for building each individual CaffeineCache instance, turning it into a LoadingCache.

public void
setAsyncCacheMode(boolean asyncCacheMode)

Set the common cache type that this cache manager builds to async.

public void
setCacheLoader(CacheLoader<Object, Object> cacheLoader)

Set the Caffeine CacheLoader to use for building each individual CaffeineCache instance, turning it into a LoadingCache.

public void
setCacheNames(Collection<String> cacheNames)

Specify the set of cache names for this CacheManager's 'static' mode.

public void
setCacheSpecification(String cacheSpecification)

Set the Caffeine cache specification String to use for building each individual CaffeineCache instance.

public void
setCaffeine(Caffeine<Object, Object> caffeine)

Set the Caffeine to use for building each individual CaffeineCache instance.

public void
setCaffeineSpec(CaffeineSpec caffeineSpec)

Set the CaffeineSpec to use for building each individual CaffeineCache instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowNullValuesback to summary
private boolean allowNullValues
asyncCacheModeback to summary
private boolean asyncCacheMode
cacheBuilderback to summary
private Caffeine<Object, Object> cacheBuilder
cacheLoaderback to summary
private AsyncCacheLoader<Object, Object> cacheLoader
Annotations
@Nullable
cacheMapback to summary
private final Map<String, Cache> cacheMap
customCacheNamesback to summary
private final Collection<String> customCacheNames
dynamicback to summary
private boolean dynamic

Constructor Detail

CaffeineCacheManagerback to summary
public CaffeineCacheManager()

Construct a dynamic CaffeineCacheManager, lazily creating cache instances as they are being requested.

CaffeineCacheManagerback to summary
public CaffeineCacheManager(String... cacheNames)

Construct a static CaffeineCacheManager, managing caches for the specified cache names only.

Method Detail

adaptCaffeineCacheback to summary
protected Cache adaptCaffeineCache(String name, Cache<Object, Object> cache)

Adapt the given new native Caffeine Cache instance to Spring's Cache abstraction for the specified cache name.

Parameters
name:String

the name of the cache

cache:Cache<Object, Object>

the native Caffeine Cache instance

Returns:Cache

the Spring CaffeineCache adapter (or a decorator thereof)

Since
5.2.8
See Also
CaffeineCache#CaffeineCache(String, com.github.benmanes.caffeine.cache.Cache, boolean), isAllowNullValues()
adaptCaffeineCacheback to summary
protected Cache adaptCaffeineCache(String name, AsyncCache<Object, Object> cache)

Adapt the given new Caffeine AsyncCache instance to Spring's Cache abstraction for the specified cache name.

Parameters
name:String

the name of the cache

cache:AsyncCache<Object, Object>

the Caffeine AsyncCache instance

Returns:Cache

the Spring CaffeineCache adapter (or a decorator thereof)

Since
6.1
See Also
CaffeineCache#CaffeineCache(String, AsyncCache, boolean), isAllowNullValues()
createAsyncCaffeineCacheback to summary
protected AsyncCache<Object, Object> createAsyncCaffeineCache(String name)

Build a common Caffeine AsyncCache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.

Parameters
name:String

the name of the cache

Returns:AsyncCache<Object, Object>

the Caffeine AsyncCache instance

Since
6.1
See Also
createCaffeineCache
createCaffeineCacheback to summary
protected Cache createCaffeineCache(String name)

Build a common CaffeineCache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.

Delegates to adaptCaffeineCache as the adaptation method to Spring's cache abstraction (allowing for centralized decoration etc.), passing in a freshly built native Caffeine Cache instance.

Parameters
name:String

the name of the cache

Returns:Cache

the Spring CaffeineCache adapter (or a decorator thereof)

See Also
adaptCaffeineCache, createNativeCaffeineCache
createNativeCaffeineCacheback to summary
protected Cache<Object, Object> createNativeCaffeineCache(String name)

Build a common Caffeine Cache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.

Parameters
name:String

the name of the cache

Returns:Cache<Object, Object>

the native Caffeine Cache instance

See Also
createCaffeineCache
doSetCaffeineback to summary
private void doSetCaffeine(Caffeine<Object, Object> cacheBuilder)
getCacheback to summary
public Cache getCache(String name)

Implements org.springframework.cache.CacheManager.getCache.

Doc from org.springframework.cache.CacheManager.getCache.

Get the cache associated with the given name.

Note that the cache may be lazily created at runtime if the native provider supports it.

Parameters
name:String

the cache identifier (must not be null)

Returns:Cache

the associated cache, or null if such a cache does not exist or could be not created

Annotations
@Override
@Nullable
getCacheNamesback to summary
public Collection<String> getCacheNames()

Implements org.springframework.cache.CacheManager.getCacheNames.

Doc from org.springframework.cache.CacheManager.getCacheNames.

Get a collection of the cache names known by this manager.

Returns:Collection<String>

the names of all caches known by the cache manager

Annotations
@Override
isAllowNullValuesback to summary
public boolean isAllowNullValues()

Return whether this cache manager accepts and converts null values for all of its caches.

refreshCommonCachesback to summary
private void refreshCommonCaches()

Recreate the common caches with the current state of this manager.

registerCustomCacheback to summary
public void registerCustomCache(String name, Cache<Object, Object> cache)

Register the given native Caffeine Cache instance with this cache manager, adapting it to Spring's cache API for exposure through getCache. 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 or dynamically built on demand, still operate with the common settings in the cache manager's configuration.

Parameters
name:String

the name of the cache

cache:Cache<Object, Object>

the custom Caffeine Cache instance to register

Since
5.2.8
See Also
adaptCaffeineCache(String, com.github.benmanes.caffeine.cache.Cache)
registerCustomCacheback to summary
public void registerCustomCache(String name, AsyncCache<Object, Object> cache)

Register the given Caffeine AsyncCache instance with this cache manager, adapting it to Spring's cache API for exposure through getCache. 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).buildAsync())

Note that any other caches, whether statically specified through setCacheNames or dynamically built on demand, still operate with the common settings in the cache manager's configuration.

Parameters
name:String

the name of the cache

cache:AsyncCache<Object, Object>

the custom Caffeine AsyncCache instance to register

Since
6.1
See Also
adaptCaffeineCache(String, AsyncCache)
setAllowNullValuesback to summary
public void setAllowNullValues(boolean allowNullValues)

Specify whether to accept and convert 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.

setAsyncCacheLoaderback to summary
public void setAsyncCacheLoader(AsyncCacheLoader<Object, Object> cacheLoader)

Set the Caffeine AsyncCacheLoader to use for building each individual CaffeineCache instance, turning it into a LoadingCache.

This implicitly switches the "asyncCacheMode" flag to true.

Since
6.1
See Also
createAsyncCaffeineCache, Caffeine#buildAsync(AsyncCacheLoader), com.github.benmanes.caffeine.cache.LoadingCache
setAsyncCacheModeback to summary
public void setAsyncCacheMode(boolean asyncCacheMode)

Set the common cache type that this cache manager builds to async. This applies to setCacheNames as well as on-demand caches.

Individual cache registrations (such as registerCustomCache(String, AsyncCache) and registerCustomCache(String, com.github.benmanes.caffeine.cache.Cache)) are not dependent on this setting.

By default, this cache manager builds regular native Caffeine caches. To switch to async caches which can also be used through the synchronous API but come with support for Cache#retrieve, set this flag to true.

Note that while null values in the cache are tolerated in async cache mode, the recommendation is to disallow null values through setAllowNullValues(false). This makes the semantics of CompletableFuture-based access simpler and optimizes retrieval performance since a Caffeine-provided CompletableFuture handle does not have to get wrapped.

If you come here for the adaptation of reactive types such as a Reactor Mono or Flux onto asynchronous caching, we recommend the standard arrangement for caching the produced values asynchronously in 6.1 through enabling this Caffeine mode. If this is not immediately possible/desirable for existing apps, you may set the system property "spring.cache.reactivestreams.ignore=true" to restore 6.0 behavior where reactive handles are treated as regular values.

Since
6.1
See Also
Caffeine#buildAsync(), Cache#retrieve(Object), Cache#retrieve(Object, Supplier), org.springframework.cache.interceptor.CacheAspectSupport#IGNORE_REACTIVESTREAMS_PROPERTY_NAME
setCacheLoaderback to summary
public void setCacheLoader(CacheLoader<Object, Object> cacheLoader)

Set the Caffeine CacheLoader to use for building each individual CaffeineCache instance, turning it into a LoadingCache.

See Also
createNativeCaffeineCache, Caffeine#build(CacheLoader), com.github.benmanes.caffeine.cache.LoadingCache
setCacheNamesback to summary
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.

setCacheSpecificationback to summary
public void setCacheSpecification(String cacheSpecification)

Set the Caffeine cache specification String to use for building each individual CaffeineCache instance. The given value needs to comply with Caffeine's CaffeineSpec (see its javadoc).

See Also
createNativeCaffeineCache, Caffeine#from(String)
setCaffeineback to summary
public void setCaffeine(Caffeine<Object, Object> caffeine)

Set the Caffeine to use for building each individual CaffeineCache instance.

See Also
createNativeCaffeineCache, Caffeine#build()
setCaffeineSpecback to summary
public void setCaffeineSpec(CaffeineSpec caffeineSpec)

Set the CaffeineSpec to use for building each individual CaffeineCache instance.

See Also
createNativeCaffeineCache, Caffeine#from(CaffeineSpec)