org.springframework.data.redis.cache
Class RedisCacheManager

java.lang.Object
  extended by org.springframework.cache.support.AbstractCacheManager
      extended by org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager
          extended by org.springframework.data.redis.cache.RedisCacheManager
All Implemented Interfaces:
InitializingBean, CacheManager

public class RedisCacheManager
extends org.springframework.cache.transaction.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').
By default RedisCaches will be lazily initialized for each getCache(String) request unless a set of predefined cache names is provided.

Setting 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 Summary
RedisCacheManager(RedisTemplate template)
          Construct a RedisCacheManager.
RedisCacheManager(RedisTemplate template, Collection<String> cacheNames)
          Construct a static RedisCacheManager, managing caches for the specified cache names only.
 
Method Summary
 Cache getCache(String name)
           
protected  Collection<? extends Cache> loadCaches()
           
 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)
           
 
Methods inherited from class org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager
decorateCache, isTransactionAware, setTransactionAware
 
Methods inherited from class org.springframework.cache.support.AbstractCacheManager
addCache, afterPropertiesSet, getCacheNames
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RedisCacheManager

public RedisCacheManager(RedisTemplate template)
Construct a RedisCacheManager.

Parameters:
template -

RedisCacheManager

public RedisCacheManager(RedisTemplate template,
                         Collection<String> cacheNames)
Construct a static RedisCacheManager, managing caches for the specified cache names only.

Parameters:
template -
cacheNames -
Since:
1.2
Method Detail

getCache

public Cache getCache(String name)
Specified by:
getCache in interface CacheManager
Overrides:
getCache in class AbstractCacheManager

setCacheNames

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.


setUsePrefix

public void setUsePrefix(boolean usePrefix)

setCachePrefix

public void setCachePrefix(RedisCachePrefix cachePrefix)
Sets the cachePrefix. Defaults to 'DefaultRedisCachePrefix').

Parameters:
cachePrefix - the cachePrefix to set

setDefaultExpiration

public void setDefaultExpiration(long defaultExpireTime)
Sets the default expire time (in seconds).

Parameters:
defaultExpireTime - time in seconds.

setExpires

public void setExpires(Map<String,Long> expires)
Sets the expire time (in seconds) for cache regions (by key).

Parameters:
expires - time in seconds

setLoadRemoteCachesOnStartup

public 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.

Parameters:
loadRemoteCachesOnStartup -
Since:
1.2

loadCaches

protected Collection<? extends Cache> loadCaches()
Specified by:
loadCaches in class AbstractCacheManager