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

public Class ConcurrentMapCacheManager

extends Object
implements CacheManager, BeanClassLoaderAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.Aware, org.springframework.cache.CacheManager
Imports
java.util.Arrays, .Collection, .Collections, .Map, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, java.util.function.Supplier, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.cache.Cache, .CacheManager, org.springframework.core.serializer.support.SerializationDelegate, org.springframework.lang.Nullable

CacheManager implementation that lazily builds ConcurrentMapCache 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.

Supports the asynchronous Cache#retrieve(Object) and Cache#retrieve(Object, Supplier) operations through basic CompletableFuture adaptation, with early-determined cache misses.

Note

This is by no means a sophisticated CacheManager; it comes with no cache configuration options. However, it may be useful for testing or simple caching scenarios. For advanced local caching needs, consider org.springframework.cache.caffeine.CaffeineCacheManager or org.springframework.cache.jcache.JCacheCacheManager.

Author
Juergen Hoeller
Since
3.1
See Also
ConcurrentMapCache

Field Summary

Modifier and TypeField and Description
private boolean
private final ConcurrentMap<String, Cache>
private boolean
private SerializationDelegate
private boolean

Constructor Summary

AccessConstructor and Description
public
ConcurrentMapCacheManager()

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

public
ConcurrentMapCacheManager(String... cacheNames)

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

Method Summary

Modifier and TypeMethod and Description
protected Cache

Returns:

the ConcurrentMapCache (or a decorator thereof)
createConcurrentMapCache
(String
the name of the cache
name
)

Create a new ConcurrentMapCache instance for the specified cache name.

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.

public boolean
isStoreByValue()

Return whether this cache manager stores a copy of each entry or a reference for all its caches.

private void
public void
setAllowNullValues(boolean allowNullValues)

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

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

public void
setCacheNames(Collection<String> cacheNames)

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

public void
setStoreByValue(boolean storeByValue)

Specify whether this cache manager stores a copy of each entry (true or the reference (false for all of its caches.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowNullValuesback to summary
private boolean allowNullValues
cacheMapback to summary
private final ConcurrentMap<String, Cache> cacheMap
dynamicback to summary
private boolean dynamic
serializationback to summary
private SerializationDelegate serialization
Annotations
@Nullable
storeByValueback to summary
private boolean storeByValue

Constructor Detail

ConcurrentMapCacheManagerback to summary
public ConcurrentMapCacheManager()

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

ConcurrentMapCacheManagerback to summary
public ConcurrentMapCacheManager(String... cacheNames)

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

Method Detail

createConcurrentMapCacheback to summary
protected Cache createConcurrentMapCache(String name)

Create a new ConcurrentMapCache instance for the specified cache name.

Parameters
name:String

the name of the cache

Returns:Cache

the ConcurrentMapCache (or a decorator thereof)

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.

isStoreByValueback to summary
public boolean isStoreByValue()

Return whether this cache manager stores a copy of each entry or a reference for all its caches. If store by value is enabled, any cache entry must be serializable.

Since
4.3
recreateCachesback to summary
private void recreateCaches()
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 ConcurrentHashMap itself not supporting null values. An internal holder object will be used to store user-level nulls.

Note

A change of the null-value setting will reset all existing caches, if any, to reconfigure them with the new null-value requirement.

setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's InitializingBean#afterPropertiesSet() method or a custom init-method.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
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.

setStoreByValueback to summary
public void setStoreByValue(boolean storeByValue)

Specify whether this cache manager stores a copy of each entry (true or the reference (false for all of its caches.

Default is "false" so that the value itself is stored and no serializable contract is required on cached values.

Note

A change of the store-by-value setting will reset all existing caches, if any, to reconfigure them with the new store-by-value requirement.

Since
4.3