Interface RedisCacheWriter
- All Superinterfaces:
CacheStatisticsProvider
RedisCacheWriter
provides low-level access to Redis commands (SET, SETNX, GET, EXPIRE,...
)
used for caching.
The RedisCacheWriter
may be shared by multiple cache implementations and is responsible for reading/writing
binary data from/to Redis. The implementation honors potential cache lock flags that might be set.
The default RedisCacheWriter
implementation can be customized with BatchStrategy
to tune performance behavior.
- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch, John Blum
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Function to compute the time to live from the cachekey
andvalue
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Remove all keys following the given pattern.void
clearStatistics
(String name) Reset all statistics counters and gauges for this cache.byte[]
Get the binary value representation from Redis stored for the given key.default byte[]
Get the binary value representation from Redis stored for the given key and set the givenTTL expiration
for the cache entry.static RedisCacheWriter
lockingRedisCacheWriter
(RedisConnectionFactory connectionFactory) Create newRedisCacheWriter
with locking behavior.static RedisCacheWriter
lockingRedisCacheWriter
(RedisConnectionFactory connectionFactory, Duration sleepTime, RedisCacheWriter.TtlFunction lockTtlFunction, BatchStrategy batchStrategy) Create newRedisCacheWriter
with locking behavior.static RedisCacheWriter
lockingRedisCacheWriter
(RedisConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newRedisCacheWriter
with locking behavior.static RedisCacheWriter
nonLockingRedisCacheWriter
(RedisConnectionFactory connectionFactory) Create newRedisCacheWriter
without locking behavior.static RedisCacheWriter
nonLockingRedisCacheWriter
(RedisConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newRedisCacheWriter
without locking behavior.void
Write the given key/value pair to Redis and set the expiration time if defined.byte[]
putIfAbsent
(String name, byte[] key, byte[] value, Duration ttl) Write the given value to Redis if the key does not already exist.void
Remove the given key from Redis.default CompletableFuture<byte[]>
CompletableFuture<byte[]>
Asynchronously retrieves thevalue
to which theRedisCache
maps the givenkey
setting theTTL expiration
for the cache entry.Store the given key/value pair asynchronously to Redis and set the expiration time if defined.default boolean
Determines whether the asynchronousretrieve(String, byte[])
andretrieve(String, byte[], Duration)
cache operations are supported by the implementation.withStatisticsCollector
(CacheStatisticsCollector cacheStatisticsCollector) Obtain aRedisCacheWriter
using the givenCacheStatisticsCollector
to collect metrics.Methods inherited from interface org.springframework.data.redis.cache.CacheStatisticsProvider
getCacheStatistics
-
Method Details
-
nonLockingRedisCacheWriter
Create newRedisCacheWriter
without locking behavior.- Parameters:
connectionFactory
- must not be null.- Returns:
- new instance of
DefaultRedisCacheWriter
.
-
nonLockingRedisCacheWriter
static RedisCacheWriter nonLockingRedisCacheWriter(RedisConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newRedisCacheWriter
without locking behavior.- Parameters:
connectionFactory
- must not be null.batchStrategy
- must not be null.- Returns:
- new instance of
DefaultRedisCacheWriter
. - Since:
- 2.6
-
lockingRedisCacheWriter
Create newRedisCacheWriter
with locking behavior.- Parameters:
connectionFactory
- must not be null.- Returns:
- new instance of
DefaultRedisCacheWriter
.
-
lockingRedisCacheWriter
static RedisCacheWriter lockingRedisCacheWriter(RedisConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newRedisCacheWriter
with locking behavior.- Parameters:
connectionFactory
- must not be null.batchStrategy
- must not be null.- Returns:
- new instance of
DefaultRedisCacheWriter
. - Since:
- 2.6
-
lockingRedisCacheWriter
static RedisCacheWriter lockingRedisCacheWriter(RedisConnectionFactory connectionFactory, Duration sleepTime, RedisCacheWriter.TtlFunction lockTtlFunction, BatchStrategy batchStrategy) Create newRedisCacheWriter
with locking behavior.- Parameters:
connectionFactory
- must not be null.sleepTime
- sleep time between lock access attempts, must not be null.lockTtlFunction
- TTL function to compute the Lock TTL. The function is called with contextual keys and values (such as the cache name on cleanup or the actual key/value on put requests); must not be null.batchStrategy
- must not be null.- Returns:
- new instance of
DefaultRedisCacheWriter
. - Since:
- 3.2
-
get
Get the binary value representation from Redis stored for the given key.- Parameters:
name
- must not be null.key
- must not be null.- Returns:
- null if key does not exist.
- See Also:
-
get
Get the binary value representation from Redis stored for the given key and set the givenTTL expiration
for the cache entry.- Parameters:
name
- must not be null.key
- must not be null.ttl
-Duration
specifying the expiration timeout for the cache entry.- Returns:
- null if key does not exist or has expired.
-
supportsAsyncRetrieve
default boolean supportsAsyncRetrieve()Determines whether the asynchronousretrieve(String, byte[])
andretrieve(String, byte[], Duration)
cache operations are supported by the implementation.The main factor for whether the retrieve operation can be supported will primarily be determined by the Redis driver in use at runtime.
Returns false by default. This will have an effect of
RedisCache.retrieve(Object)
andRedisCache.retrieve(Object, Supplier)
throwing anUnsupportedOperationException
.- Returns:
- true if asynchronous retrieve operations are supported by the implementation.
- Since:
- 3.2
-
retrieve
Asynchronously retrieves thevalue
to which theRedisCache
maps the givenkey
.This operation is non-blocking.
- Parameters:
name
-String
with the name of theRedisCache
.key
-key
mapped to thevalue
in theRedisCache
.- Returns:
- the
value
to which theRedisCache
maps the givenkey
. - Since:
- 3.2
- See Also:
-
retrieve
Asynchronously retrieves thevalue
to which theRedisCache
maps the givenkey
setting theTTL expiration
for the cache entry.This operation is non-blocking.
- Parameters:
name
-String
with the name of theRedisCache
.key
-key
mapped to thevalue
in theRedisCache
.ttl
-Duration
specifying the expiration timeout for the cache entry.- Returns:
- the
value
to which theRedisCache
maps the givenkey
. - Since:
- 3.2
-
put
Write the given key/value pair to Redis and set the expiration time if defined.- Parameters:
name
- The cache name must not be null.key
- The key for the cache entry. Must not be null.value
- The value stored for the key. Must not be null.ttl
- Optional expiration time. Can be null.
-
store
Store the given key/value pair asynchronously to Redis and set the expiration time if defined.This operation is non-blocking.
- Parameters:
name
- The cache name must not be null.key
- The key for the cache entry. Must not be null.value
- The value stored for the key. Must not be null.ttl
- Optional expiration time. Can be null.- Since:
- 3.2
-
putIfAbsent
Write the given value to Redis if the key does not already exist.- Parameters:
name
- The cache name must not be null.key
- The key for the cache entry. Must not be null.value
- The value stored for the key. Must not be null.ttl
- Optional expiration time. Can be null.- Returns:
- null if the value has been written, the value stored for the key if it already exists.
-
remove
Remove the given key from Redis.- Parameters:
name
- The cache name must not be null.key
- The key for the cache entry. Must not be null.
-
clean
Remove all keys following the given pattern.- Parameters:
name
- The cache name must not be null.pattern
- The pattern for the keys to remove. Must not be null.
-
clearStatistics
Reset all statistics counters and gauges for this cache.- Since:
- 2.4
-
withStatisticsCollector
Obtain aRedisCacheWriter
using the givenCacheStatisticsCollector
to collect metrics.- Parameters:
cacheStatisticsCollector
- must not be null.- Returns:
- new instance of
RedisCacheWriter
.
-