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 Summary
Modifier and TypeFieldDescriptionprotected static final boolean
protected static final boolean
protected static final boolean
protected static final boolean
protected static final boolean
protected static final boolean
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addCacheKeyConverter
(Converter<?, String> cacheKeyConverter) Adds aConverter
to extract theString
representation of a cache key if no suitableObject.toString()
method is present.computePrefixWith
(CacheKeyPrefix cacheKeyPrefix) Use the givenCacheKeyPrefix
to compute the prefix for the actual Redis key given the cache name as function input.void
configureKeyConverters
(Consumer<ConverterRegistry> registryConsumer) Configure the underlyingConversionService
used to extract the cache key.static RedisCacheConfiguration
DefaultRedisCacheConfiguration
using the following: key expiration eternal cache null values yes prefix cache keys yes default prefix [the actual cache name] key serializerStringRedisSerializer
value serializerJdkSerializationRedisSerializer
conversion serviceDefaultFormattingConversionService
withdefault
cache key convertersstatic RedisCacheConfiguration
defaultCacheConfig
(ClassLoader classLoader) Create defaultRedisCacheConfiguration
givenClassLoader
using the following: key expiration eternal cache null values yes prefix cache keys yes default prefix [the actual cache name] key serializerStringRedisSerializer
value serializerJdkSerializationRedisSerializer
conversion serviceDefaultFormattingConversionService
withdefault
cache key convertersDisable caching null values.Disable using cache key prefixes.Enables time-to-idle (TTI) expiration onCache
read operations, such asCache.get(Object)
.Set the ttl to apply for cache entries.entryTtl
(RedisCacheWriter.TtlFunction ttlFunction) Set theTTL function
to compute the time to live for cache entries.boolean
Gets the configuredCacheKeyPrefix
.getKeyPrefixFor
(String cacheName) Get the computed key prefix for a given cacheName.getTtl()
Deprecated.since 3.2.Gets theRedisCacheWriter.TtlFunction
used to compute a cache key time-to-live (TTL) expiration.boolean
Determines whether time-to-idle (TTI) expiration has been enabled for caching.prefixCacheNameWith
(String prefix) Prefix thecache name
with the given value.static void
registerDefaultConverters
(ConverterRegistry registry) Registers default cachekey converters
.serializeKeysWith
(RedisSerializationContext.SerializationPair<String> keySerializationPair) Define theRedisSerializationContext.SerializationPair
used for de-/serializing cache keys.serializeValuesWith
(RedisSerializationContext.SerializationPair<?> valueSerializationPair) Define theRedisSerializationContext.SerializationPair
used for de-/serializing cache values.boolean
withConversionService
(ConversionService conversionService) Define theConversionService
used for cache key toString
conversion.
-
Field Details
-
DEFAULT_CACHE_NULL_VALUES
protected static final boolean DEFAULT_CACHE_NULL_VALUES- See Also:
-
DEFAULT_ENABLE_TIME_TO_IDLE_EXPIRATION
protected static final boolean DEFAULT_ENABLE_TIME_TO_IDLE_EXPIRATION- 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:
-
USE_TIME_TO_IDLE_EXPIRATION
protected static final boolean USE_TIME_TO_IDLE_EXPIRATION- See Also:
-
-
Method Details
-
defaultCacheConfig
DefaultRedisCacheConfiguration
using 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
DefaultFormattingConversionService
withdefault
cache key converters
- Returns:
- new
RedisCacheConfiguration
.
-
defaultCacheConfig
Create defaultRedisCacheConfiguration
givenClassLoader
using 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
DefaultFormattingConversionService
withdefault
cache key converters
- Parameters:
classLoader
- theClassLoader
used for deserialization by theJdkSerializationRedisSerializer
.- Returns:
- new
RedisCacheConfiguration
. - Since:
- 2.1
-
prefixCacheNameWith
Prefix thecache name
with 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 givenCacheKeyPrefix
to 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
.
-
enableTimeToIdle
Enables time-to-idle (TTI) expiration onCache
read operations, such asCache.get(Object)
.Enabling this option applies the same
TTL expiration policy
toCache
read operations as it does forCache
write 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
Cache
operations, both read and write alike, andCache
operations 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:
-
entryTtl
Set the ttl to apply for cache entries. UseDuration.ZERO
to declare an eternal cache.- Parameters:
ttl
- must not be null.- Returns:
- new
RedisCacheConfiguration
.
-
entryTtl
Set theTTL function
to compute the time to live for cache entries.- Parameters:
ttlFunction
- theRedisCacheWriter.TtlFunction
to compute the time to live for cache entries, must not be null.- Returns:
- new
RedisCacheConfiguration
. - Since:
- 3.2
-
serializeKeysWith
public RedisCacheConfiguration serializeKeysWith(RedisSerializationContext.SerializationPair<String> keySerializationPair) Define theRedisSerializationContext.SerializationPair
used for de-/serializing cache keys.- Parameters:
keySerializationPair
- must not be null.- Returns:
- new
RedisCacheConfiguration
.
-
serializeValuesWith
public RedisCacheConfiguration serializeValuesWith(RedisSerializationContext.SerializationPair<?> valueSerializationPair) Define theRedisSerializationContext.SerializationPair
used for de-/serializing cache values.- Parameters:
valueSerializationPair
- must not be null.- Returns:
- new
RedisCacheConfiguration
.
-
withConversionService
Define theConversionService
used for cache key toString
conversion.- Parameters:
conversionService
- must not be null.- Returns:
- new
RedisCacheConfiguration
.
-
getAllowCacheNullValues
public boolean getAllowCacheNullValues()- Returns:
- true if caching null is allowed.
-
isTimeToIdleEnabled
public 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:
-
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
ConversionService
used for cache key toString
conversion. Never null.
-
getKeyPrefix
Gets the configuredCacheKeyPrefix
.- Returns:
- the configured
CacheKeyPrefix
.
-
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
Deprecated.since 3.2. UsegetTtlFunction()
instead.Returns a computedTTL expiration timeout
based on cache entry key/value if aRedisCacheWriter.TtlFunction
was confiugred usingentryTtl(TtlFunction)
.Otherwise, returns the user-provided, fixed
Duration
ifentryTtl(Duration)
was called during cache configuration.- Returns:
- the configured
TTL expiration
.
-
getTtlFunction
Gets theRedisCacheWriter.TtlFunction
used to compute a cache key time-to-live (TTL) expiration.- Returns:
- the
RedisCacheWriter.TtlFunction
used to compute expiration time (TTL) for cache entries; never null.
-
addCacheKeyConverter
Adds aConverter
to extract theString
representation of a cache key if no suitableObject.toString()
method is present.- Parameters:
cacheKeyConverter
-Converter
used to convert a cache key into aString
.- Throws:
IllegalStateException
- ifgetConversionService()
does not allowConverter
registration.- Since:
- 2.2
- See Also:
-
configureKeyConverters
Configure the underlyingConversionService
used to extract the cache key.- Parameters:
registryConsumer
-Consumer
used to register aConverter
with the configuredConverterRegistry
; never null.- Throws:
IllegalStateException
- ifgetConversionService()
does not allowConverter
registration.- Since:
- 2.2
- See Also:
-
registerDefaultConverters
Registers default cachekey converters
.The following converters get registered:
- Parameters:
registry
-ConverterRegistry
in which thekey converters
are registered; must not be null.- See Also:
-