Class RedisCacheManager
- All Implemented Interfaces:
InitializingBean,CacheManager
CacheManager implementation for Redis backed by RedisCache.
This CacheManager creates caches on first write, by default. Empty caches
are not visible in Redis due to how Redis represents empty data structures.
Caches requiring a different cache configuration than the
default cache configuration can be specified via
RedisCacheManager.RedisCacheManagerBuilder.withInitialCacheConfigurations(Map) or individually using
RedisCacheManager.RedisCacheManagerBuilder.withCacheConfiguration(String, RedisCacheConfiguration).
- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch, Yanming Zhou, John Blum
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final boolean -
Constructor Summary
ConstructorsConstructorDescriptionRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand defaultRedisCacheConfiguration.RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowRuntimeCacheCreation, String... initialCacheNames) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand defaultRedisCacheConfigurationalong with whether to allow cache creation at runtime.RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowRuntimeCacheCreation, Map<String, RedisCacheConfiguration> initialCacheConfigurations) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand a defaultRedisCacheConfiguration, and whether to allowRedisCachecreation at runtime.RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand a defaultRedisCacheConfigurationalong with an optional, initial set ofcache namesused to createRedis cacheson startup.RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations) RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations, boolean allowRuntimeCacheCreation) Deprecated.since 3.2. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Factory method returning a Builder used to construct and configure aRedisCacheManager.builder(RedisCacheWriter cacheWriter) Factory method returning a Builder used to construct and configure aRedisCacheManagerinitialized with the givenRedisCacheWriter.builder(RedisConnectionFactory connectionFactory) Factory method returning a Builder used to construct and configure aRedisCacheManagerinitialized with the givenRedisConnectionFactory.static RedisCacheManagercreate(RedisConnectionFactory connectionFactory) Factory method used to construct a newRedisCacheManagerinitialized with the givenRedisConnectionFactoryand usingdefaultsfor caching.protected RedisCachecreateRedisCache(String name, RedisCacheConfiguration cacheConfiguration) protected RedisCacheWriterReturns a reference to the configuredRedisCacheWriterused to performRedisCacheoperations, such as reading from and writing to the cache.protected RedisCacheConfigurationGets the defaultRedisCacheConfigurationapplied to newRedisCacheinstances on creation when custom, non-specificRedisCacheConfigurationwas not provided.protected Map<String,RedisCacheConfiguration> Gets aMapofcache namestoRedisCacheConfigurationobjects as the initial set ofRedis cachesto create on startup.protected RedisCachegetMissingCache(String name) booleanDetermines whetherRedis cachesare allowed to be created at runtime.protected Collection<RedisCache>Methods inherited from class org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager
decorateCache, isTransactionAware, setTransactionAwareMethods inherited from class org.springframework.cache.support.AbstractCacheManager
afterPropertiesSet, getCache, getCacheNames, initializeCaches, lookupCache
-
Field Details
-
DEFAULT_ALLOW_RUNTIME_CACHE_CREATION
protected static final boolean DEFAULT_ALLOW_RUNTIME_CACHE_CREATION- See Also:
-
-
Constructor Details
-
RedisCacheManager
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand defaultRedisCacheConfiguration.Allows
cachecreation at runtime.- Parameters:
cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing appropriate Redis commands; must not be null.defaultCacheConfiguration-RedisCacheConfigurationapplied to newRedis cachesby default when no cache-specificRedisCacheConfigurationis provided; must not be null.- Throws:
IllegalArgumentException- if either the givenRedisCacheWriterorRedisCacheConfigurationare null.- See Also:
-
RedisCacheManager
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand a defaultRedisCacheConfigurationalong with an optional, initial set ofcache namesused to createRedis cacheson startup.Allows
cachecreation at runtime.- Parameters:
cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing appropriate Redis commands; must not be null.defaultCacheConfiguration-RedisCacheConfigurationapplied to newRedis cachesby default when no cache-specificRedisCacheConfigurationis provided; must not be null.initialCacheNames- optional set ofcache namesused to createRedis cacheson startup. The defaultRedisCacheConfigurationwill be applied to each cache.- Throws:
IllegalArgumentException- if either the givenRedisCacheWriterorRedisCacheConfigurationare null.- See Also:
-
RedisCacheManager
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowRuntimeCacheCreation, String... initialCacheNames) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand defaultRedisCacheConfigurationalong with whether to allow cache creation at runtime.Additionally, the optional, initial set of
cache nameswill be used to createRedis cacheson startup.- Parameters:
cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing appropriate Redis commands; must not be null.defaultCacheConfiguration-RedisCacheConfigurationapplied to newRedis cachesby default when no cache-specificRedisCacheConfigurationis provided; must not be null.allowRuntimeCacheCreation- boolean specifying whether to allow creation of undeclared caches at runtime; true by default. Maybe just useRedisCacheConfiguration.defaultCacheConfig().initialCacheNames- optional set ofcache namesused to createRedis cacheson startup. The defaultRedisCacheConfigurationwill be applied to each cache.- Throws:
IllegalArgumentException- if either the givenRedisCacheWriterorRedisCacheConfigurationare null.- Since:
- 2.0.4
- See Also:
-
RedisCacheManager
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations) Creates newRedisCacheManagerusing givenRedisCacheWriterand defaultRedisCacheConfiguration.Additionally, an initial
RedisCachewill be created and configured using the associatedRedisCacheConfigurationfor eachnamedRedisCachein the givenMap.Allows
cachecreation at runtime.- Parameters:
cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing appropriate Redis commands; must not be null.defaultCacheConfiguration-RedisCacheConfigurationapplied to newRedis cachesby default when no cache-specificRedisCacheConfigurationis provided; must not be null.initialCacheConfigurations-Mapof declared, knowncache namesalong with associatedRedisCacheConfigurationused to create and configureReds cacheson startup; must not be null.- Throws:
IllegalArgumentException- if either the givenRedisCacheWriterorRedisCacheConfigurationare null.- See Also:
-
RedisCacheManager
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowRuntimeCacheCreation, Map<String, RedisCacheConfiguration> initialCacheConfigurations) Creates a newRedisCacheManagerinitialized with the givenRedisCacheWriterand a defaultRedisCacheConfiguration, and whether to allowRedisCachecreation at runtime.Additionally, an initial
RedisCachewill be created and configured using the associatedRedisCacheConfigurationfor eachnamedRedisCachein the givenMap.- Parameters:
cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing appropriate Redis commands; must not be null.defaultCacheConfiguration-RedisCacheConfigurationapplied to newRedis cachesby default when no cache-specificRedisCacheConfigurationis provided; must not be null.allowRuntimeCacheCreation- boolean specifying whether to allow creation of undeclared caches at runtime; true by default. Maybe just useRedisCacheConfiguration.defaultCacheConfig().initialCacheConfigurations-Mapof declared, knowncache namesalong with the associatedRedisCacheConfigurationused to create and configureRedis cacheson startup; must not be null.- Throws:
IllegalArgumentException- if either the givenRedisCacheWriterorRedisCacheConfigurationare null.- Since:
- 2.0.4
- See Also:
-
RedisCacheManager
@Deprecated(since="3.2") public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations, boolean allowRuntimeCacheCreation) Deprecated.since 3.2. UseRedisCacheManager(RedisCacheWriter, RedisCacheConfiguration, boolean, Map)instead.
-
-
Method Details
-
builder
Factory method returning a Builder used to construct and configure aRedisCacheManager.- Returns:
- new
RedisCacheManager.RedisCacheManagerBuilder. - Since:
- 2.3
-
builder
Factory method returning a Builder used to construct and configure aRedisCacheManagerinitialized with the givenRedisCacheWriter.- Parameters:
cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing appropriate Redis commands; must not be null.- Returns:
- new
RedisCacheManager.RedisCacheManagerBuilder. - Throws:
IllegalArgumentException- if the givenRedisCacheWriteris null.- See Also:
-
builder
public static RedisCacheManager.RedisCacheManagerBuilder builder(RedisConnectionFactory connectionFactory) Factory method returning a Builder used to construct and configure aRedisCacheManagerinitialized with the givenRedisConnectionFactory.- Parameters:
connectionFactory-RedisConnectionFactoryused by theRedisCacheManagerto acquire connections to Redis when performingRedisCacheoperations; must not be null.- Returns:
- new
RedisCacheManager.RedisCacheManagerBuilder. - Throws:
IllegalArgumentException- if the givenRedisConnectionFactoryis null.- See Also:
-
create
Factory method used to construct a newRedisCacheManagerinitialized with the givenRedisConnectionFactoryand usingdefaultsfor caching.- locking
- disabled
- batch strategy
BatchStrategies.keys()- cache configuration
RedisCacheConfiguration.defaultCacheConfig()- initial caches
- none
- transaction aware
- no
- in-flight cache creation
- enabled
- Parameters:
connectionFactory-RedisConnectionFactoryused by theRedisCacheManagerto acquire connections to Redis when performingRedisCacheoperations; must not be null.- Returns:
- new
RedisCacheManager. - Throws:
IllegalArgumentException- if the givenRedisConnectionFactoryis null.- See Also:
-
isAllowRuntimeCacheCreation
public boolean isAllowRuntimeCacheCreation()Determines whetherRedis cachesare allowed to be created at runtime.- Returns:
- a boolean value indicating whether
Redis cachesare allowed to be created at runtime.
-
getCacheConfigurations
- Returns:
- unmodifiable
Mapcontainingcache name/configurationpairs.
-
getDefaultCacheConfiguration
Gets the defaultRedisCacheConfigurationapplied to newRedisCacheinstances on creation when custom, non-specificRedisCacheConfigurationwas not provided.- Returns:
- the default
RedisCacheConfiguration.
-
getInitialCacheConfiguration
Gets aMapofcache namestoRedisCacheConfigurationobjects as the initial set ofRedis cachesto create on startup.- Returns:
- a
Mapofcache namestoRedisCacheConfigurationobjects.
-
getCacheWriter
Returns a reference to the configuredRedisCacheWriterused to performRedisCacheoperations, such as reading from and writing to the cache.- Returns:
- a reference to the configured
RedisCacheWriter. - See Also:
-
getMissingCache
- Overrides:
getMissingCachein classAbstractCacheManager
-
createRedisCache
protected RedisCache createRedisCache(String name, @Nullable RedisCacheConfiguration cacheConfiguration) - Parameters:
name-namefor theRedisCache; must not be null.cacheConfiguration-RedisCacheConfigurationused to configure theRedisCache; resolves to thegetDefaultCacheConfiguration()if null.- Returns:
- a new
RedisCacheinstance; never null.
-
loadCaches
- Specified by:
loadCachesin classAbstractCacheManager
-