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 TypeMethodDescriptionvoid
addCacheKeyConverter
(Converter<?, String> cacheKeyConverter) Add aConverter
for extracting 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 underlying conversion system 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 cache key 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.
-
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
-
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
.
-
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
.
-
withConversionService
Define theConversionService
used for cache key toString
conversion.- Parameters:
conversionService
- 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
.
-
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
ConversionService
used for cache key toString
conversion. Never null.
-
addCacheKeyConverter
Add aConverter
for extracting theString
representation 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.
-