Package org.cache2k.extra.spring
Class SpringCache2kCacheManager
- java.lang.Object
-
- org.cache2k.extra.spring.SpringCache2kCacheManager
-
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean
,org.springframework.cache.CacheManager
public class SpringCache2kCacheManager extends Object implements org.springframework.cache.CacheManager, org.springframework.beans.factory.DisposableBean
A Spring cache manager that manages cache2k caches. The manager delegates to the cache2k nativeCacheManager
. The available caches can be configured programmatically in a Spring configuration class via the use ofaddCaches(Function[])
or via the cache2k XML configuration.- Author:
- Jens Wilke
- See Also:
- Spring Framework - cache2k User Guide
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_SPRING_CACHE_MANAGER_NAME
Default name used for the cache2k cache manager controlled by the spring cache manager.
-
Constructor Summary
Constructors Constructor Description SpringCache2kCacheManager()
Construct a spring cache manager, using the cache2k cache manager with the name "springDefault".SpringCache2kCacheManager(String name)
Construct a spring cache manager, using the cache2k cache manager instance with the specified name.SpringCache2kCacheManager(org.cache2k.CacheManager manager)
Construct a spring cache manager, using a custom cache2k cache manager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SpringCache2kCacheManager
addCache(String name, Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>> f)
Adds a cache to this cache manager that may be configured via theCache2kBuilder
.SpringCache2kCacheManager
addCache(Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>> f)
Adds a cache to this cache manager that may be configured via theCache2kBuilder
.SpringCache2kCacheManager
addCaches(Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>>... fs)
SpringCache2kCacheManager
defaultSetup(Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>> f)
Function providing default settings for caches added to the manager viaaddCaches(java.util.function.Function<org.cache2k.Cache2kBuilder<?, ?>, org.cache2k.Cache2kBuilder<?, ?>>...)
and for dynamically created caches (not configured explicitly) that are retrieved viagetCache(String)
.void
destroy()
@NonNull SpringCache2kCache
getCache(String name)
Returns an existing cache or creates and wraps a new cache if the creation of unknown caches is enabled.Map<String,SpringCache2kCache>
getCacheMap()
Expose the map of created and wrapped caches.Collection<String>
getCacheNames()
Get a list of known caches.Collection<String>
getConfiguredCacheNames()
List of configured cache names.org.cache2k.CacheManager
getNativeCacheManager()
Expose the native cache manager.boolean
isAllowUnknownCache()
void
setAllowUnknownCache(boolean v)
Iffalse
, the cache manager only manages a cache if added viaaddCaches(java.util.function.Function<org.cache2k.Cache2kBuilder<?, ?>, org.cache2k.Cache2kBuilder<?, ?>>...)
, ofsetDefaultCacheNames(String...)
or enlisted in the cache2k XML configuration.SpringCache2kCacheManager
setDefaultCacheNames(String... names)
SpringCache2kCacheManager
setDefaultCacheNames(Collection<String> names)
Initialize the specified caches with defaults and disable dynamic creation of caches by settingsetAllowUnknownCache(boolean)
tofalse
static void
springDefaults(org.cache2k.Cache2kBuilder<?,?> builder)
Defaults for cache2k caches within Spring context.
-
-
-
Field Detail
-
DEFAULT_SPRING_CACHE_MANAGER_NAME
public static final String DEFAULT_SPRING_CACHE_MANAGER_NAME
Default name used for the cache2k cache manager controlled by the spring cache manager.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SpringCache2kCacheManager
public SpringCache2kCacheManager()
Construct a spring cache manager, using the cache2k cache manager with the name "springDefault". It is recommended that the spring cache manager uses a cache2k cache manager exclusively and all caches are created only via the spring cache manager. (Behavior since cache2k version 1.4)- See Also:
CacheManager
-
SpringCache2kCacheManager
public SpringCache2kCacheManager(String name)
Construct a spring cache manager, using the cache2k cache manager instance with the specified name.- See Also:
CacheManager
,CacheManager.getInstance()
-
SpringCache2kCacheManager
public SpringCache2kCacheManager(org.cache2k.CacheManager manager)
Construct a spring cache manager, using a custom cache2k cache manager. This allows for the use of a different manager name and classloader.- See Also:
CacheManager
,CacheManager.getInstance(String)
,CacheManager.getInstance(ClassLoader)
,CacheManager.getInstance(ClassLoader, String)
-
-
Method Detail
-
getCache
@NonNull public @NonNull SpringCache2kCache getCache(String name)
Returns an existing cache or creates and wraps a new cache if the creation of unknown caches is enabled. When a new cache is created which was not added byaddCaches(java.util.function.Function<org.cache2k.Cache2kBuilder<?, ?>, org.cache2k.Cache2kBuilder<?, ?>>...)
the default setup fromdefaultSetup
is applied.- Specified by:
getCache
in interfaceorg.springframework.cache.CacheManager
-
defaultSetup
public SpringCache2kCacheManager defaultSetup(Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>> f)
Function providing default settings for caches added to the manager viaaddCaches(java.util.function.Function<org.cache2k.Cache2kBuilder<?, ?>, org.cache2k.Cache2kBuilder<?, ?>>...)
and for dynamically created caches (not configured explicitly) that are retrieved viagetCache(String)
. The defaults must be set first, before caches are added or retrieved.- Throws:
IllegalStateException
- if caches were already added
-
addCaches
@SafeVarargs public final SpringCache2kCacheManager addCaches(Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>>... fs)
- See Also:
addCache(Function)
-
addCache
public SpringCache2kCacheManager addCache(Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>> f)
Adds a cache to this cache manager that may be configured via theCache2kBuilder
. This method can be used in case a programmatic configuration of a cache manager is preferred.The provided builder is already seeded with the effective cache2k cache manager and default configuration.
- Throws:
IllegalStateException
- if cache is already created
-
addCache
public SpringCache2kCacheManager addCache(String name, Function<org.cache2k.Cache2kBuilder<?,?>,org.cache2k.Cache2kBuilder<?,?>> f)
Adds a cache to this cache manager that may be configured via theCache2kBuilder
.- Parameters:
name
- name of the cache to addf
- customizer function for the builder provided with the defaults- Returns:
- the customized builder
-
setDefaultCacheNames
public SpringCache2kCacheManager setDefaultCacheNames(String... names)
- See Also:
setDefaultCacheNames(Collection)
-
setDefaultCacheNames
public SpringCache2kCacheManager setDefaultCacheNames(Collection<String> names)
Initialize the specified caches with defaults and disable dynamic creation of caches by settingsetAllowUnknownCache(boolean)
tofalse
-
getCacheNames
public Collection<String> getCacheNames()
Get a list of known caches. Depending on the configuration, caches may be created dynamically with a default configuration. Because of this combine the known names from configuration and activated caches.- Specified by:
getCacheNames
in interfaceorg.springframework.cache.CacheManager
-
getConfiguredCacheNames
public Collection<String> getConfiguredCacheNames()
List of configured cache names. These are caches configured in cache2k via XML and caches added to this cache manager with programmatic configurationbuildAndAddCache(org.cache2k.Cache2kBuilder<?, ?>)
. Used for testing purposes to determine whether a cache was added dynamically.
-
getNativeCacheManager
public org.cache2k.CacheManager getNativeCacheManager()
Expose the native cache manager.
-
getCacheMap
public Map<String,SpringCache2kCache> getCacheMap()
Expose the map of created and wrapped caches.
-
isAllowUnknownCache
public boolean isAllowUnknownCache()
-
setAllowUnknownCache
public void setAllowUnknownCache(boolean v)
Iffalse
, the cache manager only manages a cache if added viaaddCaches(java.util.function.Function<org.cache2k.Cache2kBuilder<?, ?>, org.cache2k.Cache2kBuilder<?, ?>>...)
, ofsetDefaultCacheNames(String...)
or enlisted in the cache2k XML configuration. Setting this totrue
will create a cache with a default configuration if the requested cache name is not known. The default istrue
.Other Spring cache managers use the notion of 'dynamic' cache creation for the same concept.
-
destroy
public void destroy()
- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
-
springDefaults
public static void springDefaults(org.cache2k.Cache2kBuilder<?,?> builder)
Defaults for cache2k caches within Spring context. Spring caches allow the storage of null values by default.
-
-