Class RedisCache
java.lang.Object
org.springframework.cache.support.AbstractValueAdaptingCache
org.springframework.data.redis.cache.RedisCache
- All Implemented Interfaces:
org.springframework.cache.Cache
public class RedisCache
extends org.springframework.cache.support.AbstractValueAdaptingCache
Cache implementation using for Redis as the underlying store for cache data.
Use RedisCacheManager to create RedisCache instances.
- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch, Piotr Mionskowski, Jos Roseboom, John Blum
- See Also:
-
AbstractValueAdaptingCache
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.cache.Cache
org.springframework.cache.Cache.ValueRetrievalException, org.springframework.cache.Cache.ValueWrapper -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) Create a newRedisCache. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidClear keys that match the providedkeyPattern.voidReset all statistics counters and gauges for this cache.protected StringconvertKey(Object key) Convertkeyto aStringrepresentation used for cache key creation.protected StringcreateCacheKey(Object key) Customization hook for creating cache key before it gets serialized.protected ObjectdeserializeCacheValue(byte[] value) Deserialize the given the array of bytes to the actualcache value.void<T> TGetRedisCacheConfigurationused.protected RedisCacheWriterprotected org.springframework.core.convert.ConversionServicegetName()Return theCacheStatisticssnapshot for this cache instance.protected <T> TloadCacheValue(Object key, Callable<T> valueLoader) protected Objectprotected ObjectpreProcessCacheValue(Object value) Customization hook called before passing object toRedisSerializer.voidorg.springframework.cache.Cache.ValueWrapperputIfAbsent(Object key, Object value) protected byte[]serializeCacheKey(String cacheKey) Serialize the givencache key.protected byte[]serializeCacheValue(Object value) Serialize thevalueto cache as an array of bytes.Methods inherited from class org.springframework.cache.support.AbstractValueAdaptingCache
fromStoreValue, get, get, isAllowNullValues, toStoreValue, toValueWrapperMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.cache.Cache
evictIfPresent, invalidate
-
Constructor Details
-
RedisCache
protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) Create a newRedisCache.- Parameters:
name-namefor thisCache; must not be null.cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing appropriate Redis commands; must not be null.cacheConfiguration-RedisCacheConfigurationapplied to thison creation; must not be {@literal null}.- Throws:
IllegalArgumentException- if either the givenRedisCacheWriterorRedisCacheConfigurationare null or the givenStringname for thisRedisCacheis null.
-
-
Method Details
-
getCacheConfiguration
GetRedisCacheConfigurationused.- Returns:
- immutable
RedisCacheConfiguration. Never null.
-
getCacheWriter
-
getConversionService
protected org.springframework.core.convert.ConversionService getConversionService() -
getName
-
getNativeCache
-
getStatistics
Return theCacheStatisticssnapshot for this cache instance. Statistics are accumulated per cache instance and not from the backing Redis data store.- Returns:
- statistics object for this
RedisCache. - Since:
- 2.4
-
get
-
loadCacheValue
-
lookup
- Specified by:
lookupin classorg.springframework.cache.support.AbstractValueAdaptingCache
-
put
-
putIfAbsent
-
clear
public void clear() -
clear
Clear keys that match the providedkeyPattern.Useful when cache keys are formatted in a style where Redis patterns can be used for matching these.
- Parameters:
keyPattern-patternused to match Redis keys to clear.- Since:
- 3.0
-
clearStatistics
public void clearStatistics()Reset all statistics counters and gauges for this cache.- Since:
- 2.4
-
evict
-
preProcessCacheValue
Customization hook called before passing object toRedisSerializer.- Parameters:
value- can be null.- Returns:
- preprocessed value. Can be null.
-
serializeCacheKey
Serialize the givencache key. -
serializeCacheValue
Serialize thevalueto cache as an array of bytes. -
deserializeCacheValue
Deserialize the given the array of bytes to the actualcache value.- Parameters:
value- array of bytes to deserialize; must not be null.- Returns:
- an
Objectdeserialized from the array of bytes using the configured valueRedisSerializationContext.SerializationPair; can be null. - See Also:
-
createCacheKey
Customization hook for creating cache key before it gets serialized.- Parameters:
key- will never be null.- Returns:
- never null.
-
convertKey
Convertkeyto aStringrepresentation used for cache key creation.- Parameters:
key- will never be null.- Returns:
- never null.
- Throws:
IllegalStateException- ifkeycannot be converted toString.
-