Class RedisCacheConfiguration
java.lang.Object
org.springframework.data.redis.cache.RedisCacheConfiguration
Immutable
RedisCacheConfiguration used to customize RedisCache behaviour, such as caching
null values, computing cache key prefixes and handling binary serialization.
Start with defaultCacheConfig() and customize RedisCache behaviour
from that point on.
- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch, John Blum
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final booleanprotected static final booleanprotected static final booleanprotected static final boolean -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCacheKeyConverter(Converter<?, String> cacheKeyConverter) Adds aConverterto extract 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 underlyingConversionServiceused 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 cachekey 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.
-
Field Details
-
DEFAULT_CACHE_NULL_VALUES
protected static final boolean DEFAULT_CACHE_NULL_VALUES- See Also:
-
DEFAULT_USE_PREFIX
protected static final boolean DEFAULT_USE_PREFIX- See Also:
-
DO_NOT_CACHE_NULL_VALUES
protected static final boolean DO_NOT_CACHE_NULL_VALUES- See Also:
-
DO_NOT_USE_PREFIX
protected static final boolean DO_NOT_USE_PREFIX- See Also:
-
-
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
-
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:
- new
RedisCacheConfiguration. - 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.
-
entryTtl
Set the ttl to apply for cache entries. UseDuration.ZEROto declare an eternal cache.- Parameters:
ttl- 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.
-
withConversionService
Define theConversionServiceused for cache key toStringconversion.- Parameters:
conversionService- must not be null.- Returns:
- new
RedisCacheConfiguration.
-
getAllowCacheNullValues
public boolean getAllowCacheNullValues()- Returns:
- true if caching null is allowed.
-
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().
-
getConversionService
- Returns:
- The
ConversionServiceused for cache key toStringconversion. Never null.
-
getKeyPrefixFor
Get the computed key prefix for a given cacheName.- Returns:
- never null.
- Since:
- 2.0.4
-
getKeySerializationPair
- Returns:
- never null.
-
getValueSerializationPair
- Returns:
- never null.
-
getTtl
- Returns:
- The expiration time (ttl) for cache entries. Never null.
-
addCacheKeyConverter
Adds aConverterto extract theStringrepresentation of a cache key if no suitableObject.toString()method is present.- Parameters:
cacheKeyConverter-Converterused to convert a cache key into aString.- Throws:
IllegalStateException- ifgetConversionService()does not allowConverterregistration.- Since:
- 2.2
- See Also:
-
configureKeyConverters
Configure the underlyingConversionServiceused to extract the cache key.- Parameters:
registryConsumer-Consumerused to register aConverterwith the configuredConverterRegistry; never null.- Throws:
IllegalStateException- ifgetConversionService()does not allowConverterregistration.- Since:
- 2.2
- See Also:
-
registerDefaultConverters
- Parameters:
registry-ConverterRegistryin which thekey convertersare registered; must not be null.- See Also:
-