Class RedisCache
java.lang.Object
org.springframework.cache.support.AbstractValueAdaptingCache
org.springframework.data.redis.cache.RedisCache
- All Implemented Interfaces:
Cache
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.cache.Cache
Cache.ValueRetrievalException, Cache.ValueWrapper
-
Constructor Summary
ModifierConstructorDescriptionprotected
RedisCache
(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) Create a newRedisCache
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
void
Clear keys that match the providedkeyPattern
.void
Reset all statistics counters and gauges for this cache.protected String
convertKey
(Object key) Convertkey
to aString
representation used for cache key creation.protected String
createCacheKey
(Object key) Customization hook for creating cache key before it gets serialized.protected Object
deserializeCacheValue
(byte[] value) Deserialize the given the array of bytes to the actualcache value
.void
<T> T
GetRedisCacheConfiguration
used.protected RedisCacheWriter
protected ConversionService
getName()
Return theCacheStatistics
snapshot for this cache instance.protected <T> T
loadCacheValue
(Object key, Callable<T> valueLoader) protected Object
protected Object
preProcessCacheValue
(Object value) Customization hook called before passing object toRedisSerializer
.void
putIfAbsent
(Object key, Object value) protected byte[]
serializeCacheKey
(String cacheKey) Serialize the givencache key
.protected byte[]
serializeCacheValue
(Object value) Serialize thevalue
to cache as an array of bytes.Methods inherited from class org.springframework.cache.support.AbstractValueAdaptingCache
fromStoreValue, get, get, isAllowNullValues, toStoreValue, toValueWrapper
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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
-name
for thisCache
; must not be null.cacheWriter
-RedisCacheWriter
used to performRedisCache
operations by executing appropriate Redis commands; must not be null.cacheConfiguration
-RedisCacheConfiguration
applied to thison creation; must not be {@literal null}.
- Throws:
IllegalArgumentException
- if either the givenRedisCacheWriter
orRedisCacheConfiguration
are null or the givenString
name for thisRedisCache
is null.
-
-
Method Details
-
getCacheConfiguration
GetRedisCacheConfiguration
used.- Returns:
- immutable
RedisCacheConfiguration
. Never null.
-
getCacheWriter
-
getConversionService
-
getName
-
getNativeCache
-
getStatistics
Return theCacheStatistics
snapshot 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:
lookup
in classAbstractValueAdaptingCache
-
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
-pattern
used 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 thevalue
to 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
Object
deserialized 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
Convertkey
to aString
representation used for cache key creation.- Parameters:
key
- will never be null.- Returns:
- never null.
- Throws:
IllegalStateException
- ifkey
cannot be converted toString
.
-