public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
CacheManager
implementation for Redis. By default saves the keys directly, without appending a prefix (which
acts as a namespace). To avoid clashes, it is recommended to change this (by setting 'usePrefix' to 'true'). RedisCache
s will be lazily initialized for each AbstractCacheManager.getCache(String)
request unless a set of
predefined cache names is provided. AbstractTransactionSupportingCacheManager.setTransactionAware(boolean)
to true
will force Caches to be decorated as
TransactionAwareCacheDecorator
so values will only be written to the cache after successful commit of
surrounding transaction.Constructor and Description |
---|
RedisCacheManager(RedisOperations redisOperations)
Construct a
RedisCacheManager . |
RedisCacheManager(RedisOperations redisOperations,
Collection<String> cacheNames)
Construct a static
RedisCacheManager , managing caches for the specified cache names only. |
RedisCacheManager(RedisOperations redisOperations,
Collection<String> cacheNames,
boolean cacheNullValues)
Construct a static
RedisCacheManager , managing caches for the specified cache names only. |
Modifier and Type | Method and Description |
---|---|
protected Collection<? extends Cache> |
addConfiguredCachesIfNecessary(Collection<? extends Cache> caches)
Returns a new
Collection of Cache from the given caches collection and adds the configured
Cache s of they are not already present. |
protected long |
computeExpiration(String name) |
protected Cache |
createAndAddCache(String cacheName)
Deprecated.
since 1.8 - please use
AbstractCacheManager.getCache(String) . |
protected RedisCache |
createCache(String cacheName) |
protected Cache |
decorateCache(Cache cache) |
protected RedisCachePrefix |
getCachePrefix() |
protected Cache |
getMissingCache(String name) |
protected RedisOperations |
getRedisOperations() |
protected boolean |
isCacheAlreadyDecorated(Cache cache) |
protected boolean |
isUsePrefix() |
protected List<Cache> |
loadAndInitRemoteCaches() |
protected Collection<? extends Cache> |
loadCaches() |
protected Set<String> |
loadRemoteCacheKeys() |
void |
setCacheNames(Collection<String> cacheNames)
Specify the set of cache names for this CacheManager's 'static' mode.
|
void |
setCachePrefix(RedisCachePrefix cachePrefix)
Sets the cachePrefix.
|
void |
setDefaultExpiration(long defaultExpireTime)
Sets the default expire time (in seconds).
|
void |
setExpires(Map<String,Long> expires)
Sets the expire time (in seconds) for cache regions (by key).
|
void |
setLoadRemoteCachesOnStartup(boolean loadRemoteCachesOnStartup)
If set to
true RedisCacheManager will try to retrieve cache names from redis server using
KEYS command and initialize RedisCache for each of them. |
void |
setUsePrefix(boolean usePrefix) |
isTransactionAware, setTransactionAware
addCache, afterPropertiesSet, getCache, getCacheNames, initializeCaches, lookupCache
public RedisCacheManager(RedisOperations redisOperations)
RedisCacheManager
.redisOperations
- public RedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames)
RedisCacheManager
, managing caches for the specified cache names only.redisOperations
- cacheNames
- public RedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames, boolean cacheNullValues)
RedisCacheManager
, managing caches for the specified cache names only. cacheNullValues
please make sure the RedisSerializer
used by
RedisOperations
is capable of serializing NullValue
.redisOperations
- RedisOperations
to work upon.cacheNames
- Collection
of known cache names.cacheNullValues
- set to true to allow caching null.public void setCacheNames(Collection<String> cacheNames)
null
or empty collection argument resets the mode to 'dynamic', allowing for further
creation of caches again.public void setUsePrefix(boolean usePrefix)
public void setCachePrefix(RedisCachePrefix cachePrefix)
cachePrefix
- the cachePrefix to setpublic void setDefaultExpiration(long defaultExpireTime)
defaultExpireTime
- time in seconds.public void setExpires(Map<String,Long> expires)
expires
- time in secondspublic void setLoadRemoteCachesOnStartup(boolean loadRemoteCachesOnStartup)
true
RedisCacheManager
will try to retrieve cache names from redis server using
KEYS command and initialize RedisCache
for each of them.loadRemoteCachesOnStartup
- protected Collection<? extends Cache> loadCaches()
loadCaches
in class AbstractCacheManager
protected Collection<? extends Cache> addConfiguredCachesIfNecessary(Collection<? extends Cache> caches)
Collection
of Cache
from the given caches collection and adds the configured
Cache
s of they are not already present.caches
- must not be null@Deprecated protected Cache createAndAddCache(String cacheName)
AbstractCacheManager.getCache(String)
.cacheName
- protected Cache getMissingCache(String name)
getMissingCache
in class AbstractCacheManager
protected RedisCache createCache(String cacheName)
protected long computeExpiration(String name)
protected RedisOperations getRedisOperations()
protected RedisCachePrefix getCachePrefix()
protected boolean isUsePrefix()
protected Cache decorateCache(Cache cache)
decorateCache
in class AbstractTransactionSupportingCacheManager
protected boolean isCacheAlreadyDecorated(Cache cache)
Copyright © 2011-2016–2016 Pivotal Software, Inc.. All rights reserved.