spring-framework / org.springframework.cache.caffeine / CaffeineCacheManager

CaffeineCacheManager

open class CaffeineCacheManager : CacheManager

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.

Requires Caffeine 2.1 or higher.

Author
Ben Manes

Author
Juergen Hoeller

Author
Stephane Nicoll

Since
4.3

See Also
CaffeineCache

Constructors

<init>

CaffeineCacheManager()

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

CaffeineCacheManager(vararg cacheNames: String)

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

Functions

getCache

open fun getCache(name: String): Cache

getCacheNames

open fun getCacheNames(): MutableCollection<String>

isAllowNullValues

open fun isAllowNullValues(): Boolean

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

setAllowNullValues

open fun setAllowNullValues(allowNullValues: Boolean): Unit

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.

setCacheLoader

open fun setCacheLoader(cacheLoader: CacheLoader<Any, Any>): Unit

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

setCacheNames

open fun setCacheNames(cacheNames: MutableCollection<String>): Unit

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.

setCacheSpecification

open fun setCacheSpecification(cacheSpecification: String): Unit

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).

setCaffeine

open fun setCaffeine(caffeine: Caffeine<Any, Any>): Unit

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

setCaffeineSpec

open fun setCaffeineSpec(caffeineSpec: CaffeineSpec): Unit

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