Package org.springframework.cache.concurrent

# Class ConcurrentMapCacheManager

java.lang.Object
org.springframework.cache.concurrent.ConcurrentMapCacheManager

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanClassLoaderAware`, `CacheManager`

public class ConcurrentMapCacheManager extends Object implements CacheManager, org.springframework.beans.factory.BeanClassLoaderAware
`CacheManager` implementation that lazily builds `ConcurrentMapCache` instances for each `getCache(java.lang.String)` request. Also supports a 'static' mode where the set of cache names is pre-defined through `setCacheNames(java.util.Collection<java.lang.String>)`, 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`.

Since:
  3.1
Author:
  Juergen Hoeller
See Also:
  - `ConcurrentMapCache`

## Constructor Summary

Constructors
Constructor
Description
`ConcurrentMapCacheManager()`
Construct a dynamic ConcurrentMapCacheManager, lazily creating cache instances as they are being requested.
`ConcurrentMapCacheManager(String... cacheNames)`
Construct a static ConcurrentMapCacheManager, managing caches for the specified cache names only.

## Method Summary

Modifier and Type
Method
Description
`protected Cache`
`createConcurrentMapCache(String name)`
Create a new ConcurrentMapCache 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 converts `null` values for all of its caches.
`boolean`
`isStoreByValue()`
Return whether this cache manager stores a copy of each entry or a reference for all its caches.
`void`
`removeCache(String name)`
Remove the specified cache from this cache manager.
`void`
`resetCaches()`
Reset this cache manager's caches, removing them completely for on-demand re-creation in 'dynamic' mode, or simply clearing their entries otherwise.
`void`
`setAllowNullValues(boolean allowNullValues)`
Specify whether to accept and convert `null` values for all caches in this cache manager.
`void`
`setBeanClassLoader(ClassLoader classLoader)`
`void`
`setCacheNames(Collection<String> cacheNames)`
Specify the set of cache names for this CacheManager's 'static' mode.
`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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ConcurrentMapCacheManager

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

### ConcurrentMapCacheManager

public ConcurrentMapCacheManager(String... cacheNames)
Construct a static ConcurrentMapCacheManager, managing caches for the specified cache names only.

## Method Details

### setCacheNames

public void setCacheNames(@Nullable 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.

Note that this method replaces existing caches of the given names and prevents the creation of further cache regions from here on - but does not remove unrelated existing caches. For a full reset, consider calling `resetCaches()` before calling this method.

Calling this method with a `null` collection argument resets the mode to 'dynamic', allowing for further creation of caches again.

See Also:
  - `resetCaches()`

### setAllowNullValues

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 `null`s.

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

### isAllowNullValues

public boolean isAllowNullValues()
Return whether this cache manager accepts and converts `null` values for all of its caches.

### setStoreByValue

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

### isStoreByValue

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

### setBeanClassLoader

public void setBeanClassLoader(ClassLoader classLoader)

Specified by:
  `setBeanClassLoader` in interface `org.springframework.beans.factory.BeanClassLoaderAware`

### getCache

@Nullable 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 interface `CacheManager`
Parameters:
  `name` - the cache identifier (must not be `null`)
Returns:
  the associated cache, or `null` if such a cache does not exist or could be not created

### 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 interface `CacheManager`
Returns:
  the names of all caches known by the cache manager

### resetCaches

public void resetCaches()
Reset this cache manager's caches, removing them completely for on-demand re-creation in 'dynamic' mode, or simply clearing their entries otherwise.

Since:
  6.2.14

### removeCache

public void removeCache(String name)
Remove the specified cache from this cache manager.

Parameters:
  `name` - the name of the cache
Since:
  6.1.15

### createConcurrentMapCache

protected Cache createConcurrentMapCache(String name)
Create a new ConcurrentMapCache instance for the specified cache name.

Parameters:
  `name` - the name of the cache
Returns:
  the ConcurrentMapCache (or a decorator thereof)