Interface ValkeyCacheWriter
- All Superinterfaces:
CacheStatisticsProvider
ValkeyCacheWriter provides low-level access to Valkey commands (SET, SETNX, GET, EXPIRE,...) used for
caching.
The ValkeyCacheWriter may be shared by multiple cache implementations and is responsible for reading/writing
binary data from/to Valkey. The implementation honors potential cache lock flags that might be set.
The default ValkeyCacheWriter implementation can be customized with BatchStrategy to tune performance
behavior.
- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch, John Blum
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceInterface that allows for configuring cache locking options.static interfaceInterface that allows for configuring cache locking.static interfaceFunction to compute the time to live from the cachekeyandvalue.static interfaceInterface that allows for configuring aValkeyCacheWriter. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidDeprecated, for removal: This API element is subject to removal in a future version.voidRemove all keys following the given pattern.voidclearStatistics(String name) Reset all statistics counters and gauges for this cache.static ValkeyCacheWritercreate(ValkeyConnectionFactory connectionFactory, Consumer<ValkeyCacheWriter.ValkeyCacheWriterConfigurer> configurerConsumer) Create newValkeyCacheWriterconfigure it throughValkeyCacheWriter.ValkeyCacheWriterConfigurer.voidRemove the given key from Valkey.default booleanevictIfPresent(String name, byte[] key) Remove the given key from Valkey if it is present, expecting the key to be immediately invisible for subsequent lookups.default <T extends @Nullable Object>
Texecute(Function<ValkeyConnection, T> callback) Executes the givenFunctionwith aValkeyConnection.byte @Nullable []Get the binary value representation from Valkey stored for the given key.default byte @Nullable []Get the binary value representation from Valkey stored for the given key and set the givenTTL expirationfor the cache entry.default byte[]get(String name, byte[] key, Supplier<byte[]> valueLoader, @Nullable Duration ttl, boolean timeToIdleEnabled) Get the binary value representation from Valkey stored for the given key and set the givenTTL expirationfor the cache entry, obtaining the value fromvalueLoaderif necessary.default booleaninvalidate(String name, byte[] pattern) Remove all keys following the given pattern expecting all entries to be immediately invisible for subsequent lookups.static ValkeyCacheWriterlockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory) Create newValkeyCacheWriterwith locking behavior usingBatchStrategies.keys().static ValkeyCacheWriterlockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newValkeyCacheWriterwith locking behavior.static ValkeyCacheWriterlockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory, Duration sleepTime, ValkeyCacheWriter.TtlFunction lockTtlFunction, BatchStrategy batchStrategy) Create newValkeyCacheWriterwith locking behavior.static ValkeyCacheWriternonLockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory) Create newValkeyCacheWriterwithout locking behavior usingBatchStrategies.keys().static ValkeyCacheWriternonLockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newValkeyCacheWriterwithout locking behavior.voidWrite the given key/value pair to Valkey and set the expiration time if defined.byte @Nullable []putIfAbsent(String name, byte[] key, byte[] value, @Nullable Duration ttl) Write the given value to Valkey if the key does not already exist.default voidDeprecated, for removal: This API element is subject to removal in a future version.since 4.0 in favor ofevict(String, byte[])default CompletableFuture<byte[]>CompletableFuture<byte[]>Asynchronously retrieves thevalueto which theValkeyCachemaps the givenbyte[] keysetting theTTL expirationfor the cache entry.Store the given key/value pair asynchronously to Valkey and set the expiration time if defined.default booleanDetermines whether the asynchronousretrieve(String, byte[])andretrieve(String, byte[], Duration)cache operations are supported by the implementation.withStatisticsCollector(CacheStatisticsCollector cacheStatisticsCollector) Obtain aValkeyCacheWriterusing the givenCacheStatisticsCollectorto collect metrics.Methods inherited from interface io.valkey.springframework.data.valkey.cache.CacheStatisticsProvider
getCacheStatistics
-
Method Details
-
create
static ValkeyCacheWriter create(ValkeyConnectionFactory connectionFactory, Consumer<ValkeyCacheWriter.ValkeyCacheWriterConfigurer> configurerConsumer) Create newValkeyCacheWriterconfigure it throughValkeyCacheWriter.ValkeyCacheWriterConfigurer. The cache writer defaults does not lock the cache by default usingBatchStrategies.keys().- Parameters:
connectionFactory- the connection factory to use.configurerConsumer- a configuration function that configuresValkeyCacheWriter.ValkeyCacheWriterConfigurer.- Returns:
- new instance of
DefaultValkeyCacheWriter. - Since:
- 4.0
-
nonLockingValkeyCacheWriter
Create newValkeyCacheWriterwithout locking behavior usingBatchStrategies.keys().- Parameters:
connectionFactory- must not be null.- Returns:
- new instance of
DefaultValkeyCacheWriter.
-
nonLockingValkeyCacheWriter
static ValkeyCacheWriter nonLockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newValkeyCacheWriterwithout locking behavior.- Parameters:
connectionFactory- must not be null.batchStrategy- must not be null.- Returns:
- new instance of
DefaultValkeyCacheWriter. - Since:
- 2.6
-
lockingValkeyCacheWriter
Create newValkeyCacheWriterwith locking behavior usingBatchStrategies.keys().- Parameters:
connectionFactory- must not be null.- Returns:
- new instance of
DefaultValkeyCacheWriter.
-
lockingValkeyCacheWriter
static ValkeyCacheWriter lockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory, BatchStrategy batchStrategy) Create newValkeyCacheWriterwith locking behavior.- Parameters:
connectionFactory- must not be null.batchStrategy- must not be null.- Returns:
- new instance of
DefaultValkeyCacheWriter. - Since:
- 2.6
-
lockingValkeyCacheWriter
static ValkeyCacheWriter lockingValkeyCacheWriter(ValkeyConnectionFactory connectionFactory, Duration sleepTime, ValkeyCacheWriter.TtlFunction lockTtlFunction, BatchStrategy batchStrategy) Create newValkeyCacheWriterwith 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
DefaultValkeyCacheWriter. - Since:
- 3.2
-
get
Get the binary value representation from Valkey 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 Valkey stored for the given key and set the givenTTL expirationfor the cache entry.- Parameters:
name- must not be null.key- must not be null.ttl-Durationspecifying the expiration timeout for the cache entry.- Returns:
- null if key does not exist or has expired.
-
get
default byte[] get(String name, byte[] key, Supplier<byte[]> valueLoader, @Nullable Duration ttl, boolean timeToIdleEnabled) Get the binary value representation from Valkey stored for the given key and set the givenTTL expirationfor the cache entry, obtaining the value fromvalueLoaderif necessary.If possible (and configured for locking), implementations should ensure that the loading operation is synchronized so that the specified
valueLoaderis only called once in case of concurrent access on the same key.- Parameters:
name- must not be null.key- must not be null.valueLoader- value loader that creates the value if the cache lookup has been not successful.ttl-Durationspecifying the expiration timeout for the cache entry.timeToIdleEnabled- true to enable Time to Idle when retrieving the value.- Since:
- 3.4
-
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 Valkey driver in use at runtime.
Returns false by default. This will have an effect of
ValkeyCache.retrieve(Object)andValkeyCache.retrieve(Object, Supplier)throwing anUnsupportedOperationException.- Returns:
- true if asynchronous retrieve operations are supported by the implementation.
- Since:
- 3.2
-
retrieve
Asynchronously retrieves thevalueto which theValkeyCachemaps the givenbyte[] key.This operation is non-blocking.
- Parameters:
name-Stringwith the name of theValkeyCache.key-byte[] keymapped to thevaluein theValkeyCache.- Returns:
- the
valueto which theValkeyCachemaps the givenbyte[] key. - Since:
- 3.2
- See Also:
-
retrieve
Asynchronously retrieves thevalueto which theValkeyCachemaps the givenbyte[] keysetting theTTL expirationfor the cache entry.This operation is non-blocking.
- Parameters:
name-Stringwith the name of theValkeyCache.key-byte[] keymapped to thevaluein theValkeyCache.ttl-Durationspecifying the expiration timeout for the cache entry.- Returns:
- the
valueto which theValkeyCachemaps the givenbyte[] key. - Since:
- 3.2
-
put
Write the given key/value pair to Valkey and set the expiration time if defined.- Parameters:
name- cache name must not be null.key- key for the cache entry. Must not be null.value- 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 Valkey and set the expiration time if defined.This operation is non-blocking.
- Parameters:
name- cache name must not be null.key- key for the cache entry. Must not be null.value- 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 Valkey if the key does not already exist.- Parameters:
name- cache name must not be null.key- key for the cache entry. Must not be null.value- 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
Deprecated, for removal: This API element is subject to removal in a future version.since 4.0 in favor ofevict(String, byte[])Remove the given key from Valkey.Actual eviction may be performed in an asynchronous or deferred fashion, with subsequent lookups possibly still seeing the entry.
- Parameters:
name- cache name must not be null.key- key for the cache entry. Must not be null.
-
evict
Remove the given key from Valkey.Actual eviction may be performed in an asynchronous or deferred fashion, with subsequent lookups possibly still seeing the entry.
- Parameters:
name- cache name must not be null.key- key for the cache entry. Must not be null.- Since:
- 4.0
-
evictIfPresent
Remove the given key from Valkey if it is present, expecting the key to be immediately invisible for subsequent lookups.- Parameters:
name- cache name must not be null.key- key for the cache entry. Must not be null.- Returns:
trueif the cache was known to have a mapping for this key before,falseif it did not (or if prior presence could not be determined).- Since:
- 4.0
-
clean
Deprecated, for removal: This API element is subject to removal in a future version.since 4.0 in favor ofclear(String, byte[])Remove all keys following the given pattern.Actual clearing may be performed in an asynchronous or deferred fashion, with subsequent lookups possibly still seeing the entries.
- Parameters:
name- cache name must not be null.pattern- pattern for the keys to remove. Must not be null.
-
clear
Remove all keys following the given pattern.Actual clearing may be performed in an asynchronous or deferred fashion, with subsequent lookups possibly still seeing the entries.
- Parameters:
name- cache name must not be null.pattern- pattern for the keys to remove. Must not be null.- Since:
- 4.0
-
invalidate
Remove all keys following the given pattern expecting all entries to be immediately invisible for subsequent lookups.- Parameters:
name- cache name must not be null.pattern- pattern for the keys to remove. Must not be null.- Returns:
trueif the cache was known to have mappings before,falseif it did not (or if prior presence of entries could not be determined).
-
clearStatistics
Reset all statistics counters and gauges for this cache.- Since:
- 2.4
-
execute
Executes the givenFunctionwith aValkeyConnection.- Type Parameters:
T-- Parameters:
callback- the callback action to invoke with a connection.- Returns:
- return value of the callback.
- Throws:
UnsupportedOperationException- if the cache writer does not support direct access toValkeyConnection.- Since:
- 4.1
-
withStatisticsCollector
Obtain aValkeyCacheWriterusing the givenCacheStatisticsCollectorto collect metrics.- Parameters:
cacheStatisticsCollector- must not be null.- Returns:
- new instance of
ValkeyCacheWriter.
-
clear(String, byte[])