Class RedisCacheConfiguration
RedisCacheConfiguration used to customize RedisCache behavior, such as caching
 null values, computing cache key prefixes and handling binary serialization.
 
 Start with defaultCacheConfig() and customize RedisCache behavior
 using the builder methods, such as entryTtl(Duration), serializeKeysWith(SerializationPair)
 and serializeValuesWith(SerializationPair).
- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch, John Blum
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected static final booleanprotected static final booleanprotected static final booleanprotected static final booleanprotected static final booleanprotected static final boolean
- 
Method SummaryModifier 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.Enables time-to-idle (TTI) expiration onCacheread operations, such asCache.get(Object).Set the ttl to apply for cache entries.entryTtl(RedisCacheWriter.TtlFunction ttlFunction) Set theTTL functionto compute the time to live for cache entries.booleanGets the configuredCacheKeyPrefix.getKeyPrefixFor(String cacheName) Get the computed key prefix for a given cacheName.getTtl()Deprecated.since 3.2.Gets theRedisCacheWriter.TtlFunctionused to compute a cache key time-to-live (TTL) expiration.booleanDetermines whether time-to-idle (TTI) expiration has been enabled for caching.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_VALUESprotected static final boolean DEFAULT_CACHE_NULL_VALUES- See Also:
 
- 
DEFAULT_ENABLE_TIME_TO_IDLE_EXPIRATIONprotected static final boolean DEFAULT_ENABLE_TIME_TO_IDLE_EXPIRATION- See Also:
 
- 
DEFAULT_USE_PREFIXprotected static final boolean DEFAULT_USE_PREFIX- See Also:
 
- 
DO_NOT_CACHE_NULL_VALUESprotected static final boolean DO_NOT_CACHE_NULL_VALUES- See Also:
 
- 
DO_NOT_USE_PREFIXprotected static final boolean DO_NOT_USE_PREFIX- See Also:
 
- 
USE_TIME_TO_IDLE_EXPIRATIONprotected static final boolean USE_TIME_TO_IDLE_EXPIRATION- See Also:
 
 
- 
- 
Method Details- 
defaultCacheConfigDefaultRedisCacheConfigurationusing 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
- DefaultFormattingConversionServicewith- defaultcache key converters
 - Returns:
- new RedisCacheConfiguration.
 
- 
defaultCacheConfigCreate 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
- DefaultFormattingConversionServicewith- defaultcache key converters
 - Parameters:
- classLoader- the- ClassLoaderused for deserialization by the- JdkSerializationRedisSerializer.
- Returns:
- new RedisCacheConfiguration.
- Since:
- 2.1
 
- 
prefixCacheNameWithPrefix 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:
 
- 
computePrefixWithUse 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:
 
- 
disableCachingNullValuesDisable 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.
 
- 
disableKeyPrefixDisable 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.
 
- 
enableTimeToIdleEnables time-to-idle (TTI) expiration onCacheread operations, such asCache.get(Object).Enabling this option applies the same TTL expiration policytoCacheread operations as it does forCachewrite operations. In effect, this will invoke the Redis GETEX command in place of GET.Redis does not support the concept of TTI, only TTL. However, if TTL expiration is applied to all Cacheoperations, both read and write alike, andCacheoperations passed with expiration are used consistently across the application, then in effect, an application can achieve TTI expiration-like behavior.Requires Redis 6.2.0 or newer. - Returns:
- this RedisCacheConfiguration.
- Since:
- 3.2.0
- See Also:
 
- 
entryTtlSet the ttl to apply for cache entries. UseDuration.ZEROto declare an eternal cache.- Parameters:
- ttl- must not be null.
- Returns:
- new RedisCacheConfiguration.
 
- 
entryTtlSet theTTL functionto compute the time to live for cache entries.- Parameters:
- ttlFunction- the- RedisCacheWriter.TtlFunctionto compute the time to live for cache entries, must not be null.
- Returns:
- new RedisCacheConfiguration.
- Since:
- 3.2
 
- 
serializeKeysWithpublic RedisCacheConfiguration serializeKeysWith(RedisSerializationContext.SerializationPair<String> keySerializationPair) Define theRedisSerializationContext.SerializationPairused for de-/serializing cache keys.- Parameters:
- keySerializationPair- must not be null.
- Returns:
- new RedisCacheConfiguration.
 
- 
serializeValuesWithpublic RedisCacheConfiguration serializeValuesWith(RedisSerializationContext.SerializationPair<?> valueSerializationPair) Define theRedisSerializationContext.SerializationPairused for de-/serializing cache values.- Parameters:
- valueSerializationPair- must not be null.
- Returns:
- new RedisCacheConfiguration.
 
- 
withConversionServiceDefine theConversionServiceused for cache key toStringconversion.- Parameters:
- conversionService- must not be null.
- Returns:
- new RedisCacheConfiguration.
 
- 
getAllowCacheNullValuespublic boolean getAllowCacheNullValues()- Returns:
- true if caching null is allowed.
 
- 
isTimeToIdleEnabledpublic boolean isTimeToIdleEnabled()Determines whether time-to-idle (TTI) expiration has been enabled for caching.Use enableTimeToIdle()to opt-in and enable time-to-idle (TTI) expiration for caching.- Returns:
- true if time-to-idle (TTI) expiration was configured and enabled for caching. Defaults to false.
- Since:
- 3.2.0
- See Also:
 
- 
usePrefixpublic 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.
 
- 
getKeyPrefixGets the configuredCacheKeyPrefix.- Returns:
- the configured CacheKeyPrefix.
 
- 
getKeyPrefixForGet the computed key prefix for a given cacheName.- Returns:
- never null.
- Since:
- 2.0.4
 
- 
getKeySerializationPair- Returns:
- never null.
 
- 
getValueSerializationPair- Returns:
- never null.
 
- 
getTtlDeprecated.since 3.2. UsegetTtlFunction()instead.Returns a computedTTL expiration timeoutbased on cache entry key/value if aRedisCacheWriter.TtlFunctionwas confiugred usingentryTtl(TtlFunction).Otherwise, returns the user-provided, fixed DurationifentryTtl(Duration)was called during cache configuration.- Returns:
- the configured TTL expiration.
 
- 
getTtlFunctionGets theRedisCacheWriter.TtlFunctionused to compute a cache key time-to-live (TTL) expiration.- Returns:
- the RedisCacheWriter.TtlFunctionused to compute expiration time (TTL) for cache entries; never null.
 
- 
addCacheKeyConverterAdds aConverterto extract theStringrepresentation of a cache key if no suitableObject.toString()method is present.- Parameters:
- cacheKeyConverter-- Converterused to convert a cache key into a- String.
- Throws:
- IllegalStateException- if- getConversionService()does not allow- Converterregistration.
- Since:
- 2.2
- See Also:
 
- 
configureKeyConvertersConfigure the underlyingConversionServiceused to extract the cache key.- Parameters:
- registryConsumer-- Consumerused to register a- Converterwith the configured- ConverterRegistry; never null.
- Throws:
- IllegalStateException- if- getConversionService()does not allow- Converterregistration.
- Since:
- 2.2
- See Also:
 
- 
registerDefaultConvertersRegisters default cachekey converters.The following converters get registered: - Parameters:
- registry-- ConverterRegistryin which the- key convertersare registered; must not be null.
- See Also:
 
 
-