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
ConstructorsModifierConstructorDescriptionprotectedRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) Create a newRedisCachewith the givennameandRedisCacheConfiguration, using theRedisCacheWriterto execute Redis commands supporting the cache operations. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidClear keys that match the givenkeyPattern.voidReset all statistics counters and gauges for this cache.protected StringconvertKey(Object key) Convertkeyto aStringused in 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> TGet theRedisCacheConfigurationused to configure thisRedisCacheon initialization.protected RedisCacheWriterGets the configuredRedisCacheWriterused to adapt Redis for cache operations.protected ConversionServiceGets the configuredConversionServiceused to convertcache keysto aStringwhen accessing entries in the cache.getName()Return theCacheStatisticssnapshot for this cache instance.protected <T> TloadCacheValue(Object key, Callable<T> valueLoader) Loads theObjectusing the givenvalueLoader.protected Objectprotected ObjectpreProcessCacheValue(Object value) Customization hook called before passing object toRedisSerializer.voidputIfAbsent(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 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 newRedisCachewith the givennameandRedisCacheConfiguration, using theRedisCacheWriterto execute Redis commands supporting the cache operations.- Parameters:
name-namefor thisCache; must not be null.cacheWriter-RedisCacheWriterused to performRedisCacheoperations by executing the necessary Redis commands; must not be null.cacheConfiguration-RedisCacheConfigurationapplied to thisRedisCacheon creation; must not be null.- Throws:
IllegalArgumentException- if either the givenRedisCacheWriterorRedisCacheConfigurationare null or the givenStringname for thisRedisCacheis null.
-
-
Method Details
-
getCacheConfiguration
Get theRedisCacheConfigurationused to configure thisRedisCacheon initialization.- Returns:
- an immutable
RedisCacheConfigurationused to configure thisRedisCacheon initialization.
-
getCacheWriter
Gets the configuredRedisCacheWriterused to adapt Redis for cache operations.- Returns:
- the configured
RedisCacheWriterused to adapt Redis for cache operations.
-
getConversionService
Gets the configuredConversionServiceused to convertcache keysto aStringwhen accessing entries in the cache.- Returns:
- the configured
ConversionServiceused to convertcache keysto aStringwhen accessing entries in the cache. - See Also:
-
getName
-
getNativeCache
-
getStatistics
Return theCacheStatisticssnapshot for this cache instance.Statistics are accumulated per cache instance and not from the backing Redis data store.
- Returns:
CacheStatisticsobject for thisRedisCache.- Since:
- 2.4
-
get
-
loadCacheValue
Loads theObjectusing the givenvalueLoader.- Type Parameters:
T-typeof the loadedcache value.- Parameters:
key-keymapped to the loadedcache value.valueLoader-Callableobject used to load thevaluefor the givenkey.- Returns:
- the loaded
value.
-
lookup
- Specified by:
lookupin 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-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
-
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 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 aStringused in cache key creation.- Parameters:
key- will never be null.- Returns:
- never null.
- Throws:
IllegalStateException- ifkeycannot be converted toString.
-