Class RedisCache
java.lang.Object
org.springframework.cache.support.AbstractValueAdaptingCache
org.springframework.data.redis.cache.RedisCache
- All Implemented Interfaces:
Cache
Cache
implementation using 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
-
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
with the givenname
andRedisCacheConfiguration
, using theRedisCacheWriter
to execute Redis commands supporting the cache operations. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
void
Clear keys that match the givenkeyPattern
.void
Reset all statistics counters and gauges for this cache.protected String
convertKey
(Object key) Convertkey
to aString
used in 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
Get theRedisCacheConfiguration
used to configure thisRedisCache
on initialization.protected RedisCacheWriter
Gets the configuredRedisCacheWriter
used to adapt Redis for cache operations.protected ConversionService
Gets the configuredConversionService
used to convertcache keys
to aString
when accessing entries in the cache.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) <T> CompletableFuture<T>
retrieve
(Object key, Supplier<CompletableFuture<T>> valueLoader) 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
with the givenname
andRedisCacheConfiguration
, using theRedisCacheWriter
to execute Redis commands supporting the cache operations.- Parameters:
name
-name
for thisCache
; must not be null.cacheWriter
-RedisCacheWriter
used to performRedisCache
operations by executing the necessary Redis commands; must not be null.cacheConfiguration
-RedisCacheConfiguration
applied to thisRedisCache
on creation; must not be null.- Throws:
IllegalArgumentException
- if either the givenRedisCacheWriter
orRedisCacheConfiguration
are null or the givenString
name for thisRedisCache
is null.
-
-
Method Details
-
getCacheConfiguration
Get theRedisCacheConfiguration
used to configure thisRedisCache
on initialization.- Returns:
- an immutable
RedisCacheConfiguration
used to configure thisRedisCache
on initialization.
-
getCacheWriter
Gets the configuredRedisCacheWriter
used to adapt Redis for cache operations.- Returns:
- the configured
RedisCacheWriter
used to adapt Redis for cache operations.
-
getConversionService
Gets the configuredConversionService
used to convertcache keys
to aString
when accessing entries in the cache.- Returns:
- the configured
ConversionService
used to convertcache keys
to aString
when accessing entries in the cache. - See Also:
-
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:
CacheStatistics
object for thisRedisCache
.- Since:
- 2.4
-
get
-
loadCacheValue
- Type Parameters:
T
-type
of the loadedcache value
.- Parameters:
key
-key
mapped to the loadedcache value
.valueLoader
-Callable
object used to load thevalue
for the givenkey
.- Returns:
- the loaded
value
.
-
lookup
- Specified by:
lookup
in classAbstractValueAdaptingCache
-
put
-
putIfAbsent
-
clear
public void clear() -
clear
Clear keys that match the givenkeyPattern
.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
-
retrieve
-
retrieve
-
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
used in cache key creation.- Parameters:
key
- will never be null.- Returns:
- never null.
- Throws:
IllegalStateException
- ifkey
cannot be converted toString
.
-