Class RedisCacheConfiguration
java.lang.Object
org.springframework.data.redis.cache.RedisCacheConfiguration
Immutable
Start with
RedisCacheConfiguration helps customizing RedisCache behaviour such as caching
null values, cache key prefixes and binary serialization. Start with
defaultCacheConfig() and customize RedisCache behaviour from there
on.- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddCacheKeyConverter(Converter<?, String> cacheKeyConverter) Add aConverterfor extracting theStringrepresentation of a cache key if no suitableObject.toString()method is present.computePrefixWith(CacheKeyPrefix cacheKeyPrefix) Use the givenCacheKeyPrefixto compute the prefix for the actual Redis key given the cache name as function input.voidconfigureKeyConverters(Consumer<ConverterRegistry> registryConsumer) Configure the underlying conversion system used to extract the cache key.static RedisCacheConfigurationDefaultRedisCacheConfigurationusing the following: key expiration eternal cache null values yes prefix cache keys yes default prefix [the actual cache name] key serializerStringRedisSerializervalue serializerJdkSerializationRedisSerializerconversion serviceDefaultFormattingConversionServicewithdefaultcache key convertersstatic RedisCacheConfigurationdefaultCacheConfig(ClassLoader classLoader) Create defaultRedisCacheConfigurationgivenClassLoaderusing the following: key expiration eternal cache null values yes prefix cache keys yes default prefix [the actual cache name] key serializerStringRedisSerializervalue serializerJdkSerializationRedisSerializerconversion serviceDefaultFormattingConversionServicewithdefaultcache key convertersDisable caching null values.Disable using cache key prefixes.Set the ttl to apply for cache entries.booleangetKeyPrefixFor(String cacheName) Get the computed key prefix for a given cacheName.getTtl()prefixCacheNameWith(String prefix) Prefix thecache namewith the given value.static voidregisterDefaultConverters(ConverterRegistry registry) Registers default cache key converters.serializeKeysWith(RedisSerializationContext.SerializationPair<String> keySerializationPair) Define theRedisSerializationContext.SerializationPairused for de-/serializing cache keys.serializeValuesWith(RedisSerializationContext.SerializationPair<?> valueSerializationPair) Define theRedisSerializationContext.SerializationPairused for de-/serializing cache values.booleanwithConversionService(ConversionService conversionService) Define theConversionServiceused for cache key toStringconversion.
-
Method Details
-
defaultCacheConfig
DefaultRedisCacheConfigurationusing the following:- key expiration
- eternal
- cache null values
- yes
- prefix cache keys
- yes
- default prefix
- [the actual cache name]
- key serializer
StringRedisSerializer- value serializer
JdkSerializationRedisSerializer- conversion service
DefaultFormattingConversionServicewithdefaultcache key converters
- Returns:
- new
RedisCacheConfiguration.
-
defaultCacheConfig
Create defaultRedisCacheConfigurationgivenClassLoaderusing the following:- key expiration
- eternal
- cache null values
- yes
- prefix cache keys
- yes
- default prefix
- [the actual cache name]
- key serializer
StringRedisSerializer- value serializer
JdkSerializationRedisSerializer- conversion service
DefaultFormattingConversionServicewithdefaultcache key converters
- Parameters:
classLoader- theClassLoaderused for deserialization by theJdkSerializationRedisSerializer.- Returns:
- new
RedisCacheConfiguration. - Since:
- 2.1
-
entryTtl
Set the ttl to apply for cache entries. UseDuration.ZEROto declare an eternal cache.- Parameters:
ttl- must not be null.- Returns:
- new
RedisCacheConfiguration.
-
prefixCacheNameWith
Prefix thecache namewith the given value.
The generated cache key will be:prefix + cache name + "::" + cache entry key.- Parameters:
prefix- the prefix to prepend to the cache name.- Returns:
- this.
- Since:
- 2.3
- See Also:
-
computePrefixWith
Use the givenCacheKeyPrefixto compute the prefix for the actual Redis key given the cache name as function input.- Parameters:
cacheKeyPrefix- must not be null.- Returns:
- new
RedisCacheConfiguration. - Since:
- 2.0.4
- See Also:
-
disableCachingNullValues
Disable caching null values.
NOTE anyCache.put(Object, Object)operation involving null value will error. Nothing will be written to Redis, nothing will be removed. An already existing key will still be there afterwards with the very same value as before.- Returns:
- new
RedisCacheConfiguration.
-
disableKeyPrefix
Disable using cache key prefixes.
NOTE:Cache.clear()might result in unintended removal of keys in Redis. Make sure to use a dedicated Redis instance when disabling prefixes.- Returns:
- new
RedisCacheConfiguration.
-
withConversionService
Define theConversionServiceused for cache key toStringconversion.- Parameters:
conversionService- must not be null.- Returns:
- new
RedisCacheConfiguration.
-
serializeKeysWith
public RedisCacheConfiguration serializeKeysWith(RedisSerializationContext.SerializationPair<String> keySerializationPair) Define theRedisSerializationContext.SerializationPairused for de-/serializing cache keys.- Parameters:
keySerializationPair- must not be null.- Returns:
- new
RedisCacheConfiguration.
-
serializeValuesWith
public RedisCacheConfiguration serializeValuesWith(RedisSerializationContext.SerializationPair<?> valueSerializationPair) Define theRedisSerializationContext.SerializationPairused for de-/serializing cache values.- Parameters:
valueSerializationPair- must not be null.- Returns:
- new
RedisCacheConfiguration.
-
getKeyPrefixFor
Get the computed key prefix for a given cacheName.- Returns:
- never null.
- Since:
- 2.0.4
-
usePrefix
public boolean usePrefix()- Returns:
- true if cache keys need to be prefixed with the
getKeyPrefixFor(String)if present or the default which resolves toCache.getName().
-
getAllowCacheNullValues
public boolean getAllowCacheNullValues()- Returns:
- true if caching null is allowed.
-
getKeySerializationPair
- Returns:
- never null.
-
getValueSerializationPair
- Returns:
- never null.
-
getTtl
- Returns:
- The expiration time (ttl) for cache entries. Never null.
-
getConversionService
- Returns:
- The
ConversionServiceused for cache key toStringconversion. Never null.
-
addCacheKeyConverter
Add aConverterfor extracting theStringrepresentation of a cache key if no suitableObject.toString()method is present.- Parameters:
cacheKeyConverter-- Throws:
IllegalStateException- ifgetConversionService()does not allow converter registration.- Since:
- 2.2
-
configureKeyConverters
Configure the underlying conversion system used to extract the cache key.- Parameters:
registryConsumer- never null.- Throws:
IllegalStateException- ifgetConversionService()does not allow converter registration.- Since:
- 2.2
-
registerDefaultConverters
Registers default cache key converters. The following converters get registered:- Parameters:
registry- must not be null.
-