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.
CaffeineCache, setCaffeineSpec, setCacheSpecification, setAsyncCacheMode
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private boolean | |
| private Caffeine | |
| private AsyncCacheLoader | |
| private final Map | |
| private final Collection | |
| private boolean |
| Access | Constructor 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. |
| Modifier and Type | Method and Description |
|---|---|
| protected Cache | |
| protected Cache | Returns: the Spring CaffeineCache adapter (or a decorator thereof)the name of the cache name, AsyncCache<Object, Object> the Caffeine AsyncCache instance cache)Adapt the given new Caffeine AsyncCache instance to Spring's |
| protected AsyncCache | Returns: the Caffeine AsyncCache instancethe 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)the name of the cache name)Build a common |
| protected Cache | Returns: the native Caffeine Cache instancethe 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 name)null)Implements org. Get the cache associated with the given name. |
| public Collection | getCacheNames()
Implements org. Get a collection of the cache names known by this manager. |
| public boolean | isAllowNullValues()
Return whether this cache manager accepts and converts |
| private void | |
| public void | |
| 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 |
| public void | setAllowNullValues(boolean allowNullValues)
Specify whether to accept and convert |
| public void | setAsyncCacheLoader(AsyncCacheLoader<Object, Object> cacheLoader)
Set the Caffeine AsyncCacheLoader to use for building each individual
|
| 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
|
| 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 |
| public void | setCaffeine(Caffeine<Object, Object> caffeine)
Set the Caffeine to use for building each individual
|
| public void | setCaffeineSpec(CaffeineSpec caffeineSpec)
Set the |
| allowNullValues | back to summary |
|---|---|
| private boolean allowNullValues | |
| asyncCacheMode | back to summary |
|---|---|
| private boolean asyncCacheMode | |
| cacheBuilder | back to summary |
|---|---|
| private Caffeine<Object, Object> cacheBuilder | |
| cacheLoader | back to summary |
|---|---|
| private AsyncCacheLoader<Object, Object> cacheLoader
| |
| cacheMap | back to summary |
|---|---|
| private final Map<String, Cache> cacheMap | |
| customCacheNames | back to summary |
|---|---|
| private final Collection<String> customCacheNames | |
| dynamic | back to summary |
|---|---|
| private boolean dynamic | |
| CaffeineCacheManager | back to summary |
|---|---|
| public CaffeineCacheManager() Construct a dynamic CaffeineCacheManager, lazily creating cache instances as they are being requested. | |
| CaffeineCacheManager | back to summary |
|---|---|
| public CaffeineCacheManager(String... cacheNames) Construct a static CaffeineCacheManager, managing caches for the specified cache names only. | |
| adaptCaffeineCache | back to summary |
|---|---|
| protected Cache adaptCaffeineCache(String name, Cache<Object, Object> cache) Adapt the given new native Caffeine Cache instance to Spring's
| |
| adaptCaffeineCache | back to summary |
|---|---|
| protected Cache adaptCaffeineCache(String name, AsyncCache<Object, Object> cache) Adapt the given new Caffeine AsyncCache instance to Spring's
| |
| createAsyncCaffeineCache | back to summary |
|---|---|
| protected AsyncCache Build a common Caffeine AsyncCache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.
| |
| createCaffeineCache | back to summary |
|---|---|
| protected Cache createCaffeineCache(String name) Build a common Delegates to
| |
| createNativeCaffeineCache | back to summary |
|---|---|
| protected Cache Build a common Caffeine Cache instance for the specified cache name, using the common Caffeine configuration specified on this cache manager.
| |
| doSetCaffeine | back to summary |
|---|---|
| private void doSetCaffeine(Caffeine<Object, Object> cacheBuilder) | |
| getCache | back to summary |
|---|---|
| public Cache getCache(String name) Implements org. Doc from org. Get the cache associated with the given name. Note that the cache may be lazily created at runtime if the native provider supports it. | |
| getCacheNames | back to summary |
|---|---|
| public Collection Implements org. Doc from org. Get a collection of the cache names known by this manager.
| |
| isAllowNullValues | back to summary |
|---|---|
| public boolean isAllowNullValues() Return whether this cache manager accepts and converts | |
| refreshCommonCaches | back to summary |
|---|---|
| private void refreshCommonCaches() Recreate the common caches with the current state of this manager. | |
| registerCustomCache | back 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 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:
Note that any other caches, whether statically specified through
| |
| registerCustomCache | back 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 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:
Note that any other caches, whether statically specified through
| |
| setAllowNullValues | back to summary |
|---|---|
| public void setAllowNullValues(boolean allowNullValues) Specify whether to accept and convert Default is "true", despite Caffeine itself not supporting | |
| setAsyncCacheLoader | back to summary |
|---|---|
| public void setAsyncCacheLoader(AsyncCacheLoader<Object, Object> cacheLoader) Set the Caffeine AsyncCacheLoader to use for building each individual
This implicitly switches the | |
| setAsyncCacheMode | back to summary |
|---|---|
| public void setAsyncCacheMode(boolean asyncCacheMode) Set the common cache type that this cache manager builds to async.
This applies to Individual cache registrations (such as 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 Note that while null values in the cache are tolerated in async cache mode,
the recommendation is to disallow null values through
If you come here for the adaptation of reactive types such as a Reactor
| |
| setCacheLoader | back to summary |
|---|---|
| public void setCacheLoader(CacheLoader<Object, Object> cacheLoader) Set the Caffeine CacheLoader to use for building each individual
| |
| setCacheNames | back 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 | |
| setCacheSpecification | back to summary |
|---|---|
| public void setCacheSpecification(String cacheSpecification) Set the Caffeine cache specification String to use for building each
individual | |
| setCaffeine | back to summary |
|---|---|
| public void setCaffeine(Caffeine<Object, Object> caffeine) Set the Caffeine to use for building each individual
| |
| setCaffeineSpec | back to summary |
|---|---|
| public void setCaffeineSpec(CaffeineSpec caffeineSpec) Set the | |