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
Modifier and TypeFieldDescriptionprotected 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.Set the ttl to apply for cache entries.boolean
getKeyPrefixFor
(String cacheName) Get the computed key prefix for a given cacheName.getTtl()
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_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
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
.
-
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
.
-
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.
-
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.
-
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 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
- Parameters:
registry
-ConverterRegistry
in which thekey converters
are registered; must not be null.- See Also:
-