Interface RedisOperations<K,V>
- All Known Implementing Classes:
RedisTemplate,StringRedisTemplate
@NullUnmarked
public interface RedisOperations<K,V>
Interface that specified a basic set of Redis operations, implemented by
RedisTemplate. Not often used but a
useful option for extensibility and testability (as it can be easily mocked or stubbed).
Redis command methods are exempted from the default non-nullable return value assumption as nullness
depends not only on the command but also on the connection state. Methods invoked during a transaction or while
pipelining are required to return null at the time invoking a command as the response is not available
until the transaction is executed or the pipeline is closed. To avoid excessive null checks in calling code and to
not express a faulty assumption of non-nullness, all command interfaces are annotated with @NullUnmarked.
- Author:
- Costin Leau, Christoph Strobl, Ninad Divadkar, Mark Paluch, ihaohong, Todd Merrill, Chen Li, Vedran Pavic, Marcin Grzejszczak
-
Method Summary
Modifier and TypeMethodDescription@NonNull BoundGeoOperations<K,V> boundGeoOps(@NonNull K key) Returns geospatial specific operations interface bound to the given key.<HK,HV> @NonNull BoundHashOperations<K, HK, HV> boundHashOps(@NonNull K key) Returns the operations performed on hash values bound to the given key.@NonNull BoundListOperations<K,V> boundListOps(K key) Returns the operations performed on list values bound to the given key.@NonNull BoundSetOperations<K,V> boundSetOps(@NonNull K key) Returns the operations performed on set values bound to the given key.<HK,HV> @NonNull BoundStreamOperations<K, HK, HV> boundStreamOps(@NonNull K key) Returns the operations performed on Streams bound to the given key.@NonNull BoundValueOperations<K,V> boundValueOps(@NonNull K key) Returns the operations performed on simple values (or Strings in Redis terminology) bound to the given key.@NonNull BoundZSetOperations<K,V> boundZSetOps(@NonNull K key) Returns the operations performed on zset values (also known as sorted sets) bound to the given key.convertAndSend(@NonNull String destination, @NonNull Object message) Publishes the given message to the given channel.Copy givensourceKeytotargetKey.countExistingKeys(@NonNull Collection<@NonNull K> keys) Count the number ofkeysthat exist.delete(@NonNull Collection<@NonNull K> keys) Delete givenkeys.Delete givenkey.voiddiscard()Discard all commands issued aftermulti().byte[]Retrieve serialized version of the value stored atkey.exec()Executes all queued commands in a transaction started withmulti().exec(@NonNull RedisSerializer<?> valueSerializer) Execute a transaction, using the providedRedisSerializerto deserialize any results that are byte[]s or Collections of byte[]s.<T extends @Nullable Object>
Texecute(@NonNull RedisCallback<T> action) Executes the given action within a Redis connection.<T extends @Nullable Object>
Texecute(@NonNull RedisScript<T> script, @NonNull List<@NonNull K> keys, @NonNull Object @NonNull ... args) Executes the givenRedisScript<T extends @Nullable Object>
Texecute(@NonNull RedisScript<T> script, @NonNull RedisSerializer<?> argsSerializer, @NonNull RedisSerializer<T> resultSerializer, @NonNull List<@NonNull K> keys, @NonNull Object @NonNull ... args) Executes the givenRedisScript, using the providedRedisSerializers to serialize the script arguments and result.<T extends @Nullable Object>
Texecute(@NonNull SessionCallback<T> session) Executes a Redis session.executePipelined(@NonNull RedisCallback<?> action) Executes the given action object on a pipelined connection, returning the results.executePipelined(@NonNull RedisCallback<?> action, @NonNull RedisSerializer<?> resultSerializer) Executes the given action object on a pipelined connection, returning the results using a dedicated serializer.executePipelined(@NonNull SessionCallback<?> session) Executes the given Redis session on a pipelined connection.executePipelined(@NonNull SessionCallback<?> session, @NonNull RedisSerializer<?> resultSerializer) Executes the given Redis session on a pipelined connection, returning the results using a dedicated serializer.<T extends Closeable>
TexecuteWithStickyConnection(@NonNull RedisCallback<T> callback) Allocates and binds a newRedisConnectionto the actual return type of the method.default @NonNull BoundKeyExpirationOperationsexpiration(@NonNull K key) Returns a bound operations object to perform expiration operations on the bound key.Set time to live for givenkey.default BooleanSet time to live for givenkey.expire(@NonNull K key, @NonNull Expiration expiration, @NonNull ExpirationOptions options) Set the expiration for givenkey.default BooleanSet the expiration for givenkeyas a date timestamp.Set the expiration for givenkeyas a date timestamp.List<@NonNull RedisClientInfo>Request information and statistics about connected clients.Get the time to live forkeyin seconds.Get the time to live forkeyin and convert it to the givenTimeUnit.@Nullable RedisSerializer<?>@Nullable RedisSerializer<?>@Nullable RedisSerializer<?>@Nullable RedisSerializer<?>Determine if givenkeyexists.Retrieve all keys matching the given pattern viaKEYScommand.voidkillClient(@NonNull String host, int port) Closes a given client connection identified by ip:port given inclient.Move givenkeyto database withindex.voidmulti()Mark the start of a transaction block.@NonNull ClusterOperations<K,V> Returns the cluster specific operations interface.@NonNull GeoOperations<K,V> Returns geospatial specific operations interface.<HK,HV> @NonNull HashOperations<K, HK, HV> Returns the operations performed on hash values.@NonNull HyperLogLogOperations<K,V> @NonNull ListOperations<K,V> Returns the operations performed on list values.@NonNull SetOperations<K,V> Returns the operations performed on set values.<HK,HV> @NonNull StreamOperations<K, HK, HV> Returns the operations performed on Streams.<HK,HV> @NonNull StreamOperations<K, HK, HV> opsForStream(@NonNull HashMapper<? super K, ? super HK, ? super HV> hashMapper) Returns the operations performed on Streams.@NonNull ValueOperations<K,V> Returns the operations performed on simple values (or Strings in Redis terminology).@NonNull ZSetOperations<K,V> Returns the operations performed on zset values (also known as sorted sets).Remove the expiration from givenkey.Return a random key from the keyspace.voidRename keyoldKeytonewKey.renameIfAbsent(@NonNull K oldKey, @NonNull K newKey) Rename keyoldKeytonewKeyonly ifnewKeydoes not exist.voidChange redis replication setting to new master.voidChange server into master.default voidvoidrestore(@NonNull K key, byte @NonNull [] value, long timeToLive, @NonNull TimeUnit unit, boolean replace) scan(@NonNull ScanOptions options) Use aCursorto iterate over keys.Sort the elements forquery.Sort the elements forqueryand store result instoreKey.<T> List<T>sort(@NonNull SortQuery<@NonNull K> query, @NonNull BulkMapper<T, V> bulkMapper) Sort the elements forqueryapplyingBulkMapper.<T> List<T>sort(@NonNull SortQuery<@NonNull K> query, @NonNull RedisSerializer<T> resultSerializer) Sort the elements forqueryapplyingRedisSerializer.<T,S> List<T> sort(SortQuery<@NonNull K> query, @NonNull BulkMapper<T, S> bulkMapper, @NonNull RedisSerializer<S> resultSerializer) Determine the type stored atkey.unlink(@NonNull Collection<@NonNull K> keys) Unlink thekeysfrom the keyspace.Unlink thekeyfrom the keyspace.voidunwatch()Flushes all the previouslywatch(Object)keys.voidwatch(@NonNull Collection<@NonNull K> keys) Watch givenkeysfor modifications during transaction started withmulti().voidWatch givenkeyfor modifications during transaction started withmulti().
-
Method Details
-
execute
Executes the given action within a Redis connection. Application exceptions thrown by the action object get propagated to the caller (can only be unchecked) whenever possible. Redis exceptions are transformed into appropriate DAO ones. Allows for returning a result object, that is a domain object or a collection of domain objects. Performs automatic serialization/deserialization for the given objects to and from binary data suitable for the Redis storage. Note: Callback code is not supposed to handle transactions itself! Use an appropriate transaction manager. Generally, callback code must not touch any Connection lifecycle methods, like close, to let the template do its work.- Type Parameters:
T- return type- Parameters:
action- callback object that specifies the Redis action. Must not be null.- Returns:
- result of the given
RedisCallback.doInRedis(RedisConnection)invocation.
-
execute
Executes a Redis session. Allows multiple operations to be executed in the same session enabling 'transactional' capabilities throughmulti()andwatch(Collection)operations.- Type Parameters:
T- return type- Parameters:
session- session callback. Must not be null.- Returns:
- result of the given
SessionCallback.execute(RedisOperations)invocation.
-
executePipelined
Executes the given action object on a pipelined connection, returning the results. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline. This method will use the default serializers to deserialize results- Parameters:
action- callback object to execute- Returns:
- pipeline results of the given
RedisCallback.doInRedis(RedisConnection)invocation. Results are collected fromRedisConnectioncalls,RedisCallback.doInRedis(RedisConnection)itself must return null.
-
executePipelined
@NonNull List<Object> executePipelined(@NonNull RedisCallback<?> action, @NonNull RedisSerializer<?> resultSerializer) Executes the given action object on a pipelined connection, returning the results using a dedicated serializer. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline.- Parameters:
action- callback object to executeresultSerializer- The Serializer to use for individual values or Collections of values. If any returned values are hashes, this serializer will be used to deserialize both the key and value- Returns:
- pipeline results of the given
RedisCallback.doInRedis(RedisConnection)invocation. Results are collected fromRedisConnectioncalls,RedisCallback.doInRedis(RedisConnection)itself must return null.
-
executePipelined
Executes the given Redis session on a pipelined connection. Allows transactions to be pipelined. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline.- Parameters:
session- Session callback- Returns:
- pipeline results of the given
SessionCallback.execute(RedisOperations)invocation. Results are collected fromRedisOperationscalls,SessionCallback.execute(RedisOperations)itself must return null.
-
executePipelined
@NonNull List<Object> executePipelined(@NonNull SessionCallback<?> session, @NonNull RedisSerializer<?> resultSerializer) Executes the given Redis session on a pipelined connection, returning the results using a dedicated serializer. Allows transactions to be pipelined. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline.- Parameters:
session- Session callbackresultSerializer-- Returns:
- pipeline results of the given
SessionCallback.execute(RedisOperations)invocation. Results are collected fromRedisOperationscalls,SessionCallback.execute(RedisOperations)itself must return null.
-
execute
<T extends @Nullable Object> T execute(@NonNull RedisScript<T> script, @NonNull List<@NonNull K> keys, @NonNull Object @NonNull ... args) Executes the givenRedisScript- Parameters:
script- The script to executekeys- Any keys that need to be passed to the scriptargs- Any args that need to be passed to the script- Returns:
- The return value of the script or null if
RedisScript.getResultType()is null, likely indicating a throw-away status reply (i.e. "OK")
-
execute
<T extends @Nullable Object> T execute(@NonNull RedisScript<T> script, @NonNull RedisSerializer<?> argsSerializer, @NonNull RedisSerializer<T> resultSerializer, @NonNull List<@NonNull K> keys, @NonNull Object @NonNull ... args) Executes the givenRedisScript, using the providedRedisSerializers to serialize the script arguments and result.- Parameters:
script- The script to executeargsSerializer- TheRedisSerializerto use for serializing argsresultSerializer- TheRedisSerializerto use for serializing the script return valuekeys- Any keys that need to be passed to the scriptargs- Any args that need to be passed to the script- Returns:
- The return value of the script or null if
RedisScript.getResultType()is null, likely indicating a throw-away status reply (i.e. "OK")
-
executeWithStickyConnection
Allocates and binds a newRedisConnectionto the actual return type of the method. It is up to the caller to free resources after use.- Parameters:
callback- must not be null.- Returns:
- the
resultof the operation performed in the callback or null. - Since:
- 1.8
-
copy
Copy givensourceKeytotargetKey.- Parameters:
sourceKey- must not be null.targetKey- must not be null.replace- whether the key was copied. null when used in pipeline / transaction.- Returns:
truewhen copied successfully or null when used in pipeline / transaction.- Since:
- 2.6
- See Also:
-
hasKey
Determine if givenkeyexists.- Parameters:
key- must not be null.- Returns:
- true if key exists. null when used in pipeline / transaction.
- See Also:
-
countExistingKeys
Count the number ofkeysthat exist.- Parameters:
keys- must not be null.- Returns:
- The number of keys existing among the ones specified as arguments. Keys mentioned multiple times and existing are counted multiple times.
- Since:
- 2.1
- See Also:
-
delete
Delete givenkey.- Parameters:
key- must not be null.- Returns:
- true if the key was removed.
- See Also:
-
delete
Delete givenkeys.- Parameters:
keys- must not be null.- Returns:
- The number of keys that were removed. null when used in pipeline / transaction.
- See Also:
-
unlink
Unlink thekeyfrom the keyspace. Unlike withdelete(Object)the actual memory reclaiming here happens asynchronously.- Parameters:
key- must not be null.- Returns:
- The number of keys that were removed. null when used in pipeline / transaction.
- Since:
- 2.1
- See Also:
-
unlink
Unlink thekeysfrom the keyspace. Unlike withdelete(Collection)the actual memory reclaiming here happens asynchronously.- Parameters:
keys- must not be null.- Returns:
- The number of keys that were removed. null when used in pipeline / transaction.
- Since:
- 2.1
- See Also:
-
type
Determine the type stored atkey.- Parameters:
key- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
keys
Retrieve all keys matching the given pattern viaKEYScommand.IMPORTANT: This command is non-interruptible and scans the entire keyspace which may cause performance issues. Consider
scan(ScanOptions)for large datasets.- Parameters:
pattern- key pattern- Returns:
- set of matching keys, or null when used in pipeline / transaction
- See Also:
-
scan
Use aCursorto iterate over keys.
Important: CallCloseableIterator.close()when done to avoid resource leaks.- Parameters:
options- must not be null.- Returns:
- the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a try-with-resources clause).
- Since:
- 2.7
- See Also:
-
randomKey
K randomKey()Return a random key from the keyspace.- Returns:
- null no keys exist or when used in pipeline / transaction.
- See Also:
-
rename
Rename keyoldKeytonewKey.- Parameters:
oldKey- must not be null.newKey- must not be null.- See Also:
-
renameIfAbsent
Rename keyoldKeytonewKeyonly ifnewKeydoes not exist.- Parameters:
oldKey- must not be null.newKey- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
expire
Set time to live for givenkey.- Parameters:
key- must not be null.timeout-unit- must not be null.- Returns:
- null when used in pipeline / transaction.
-
expire
Set time to live for givenkey.- Parameters:
key- must not be null.timeout- must not be null.- Returns:
- null when used in pipeline / transaction.
- Throws:
IllegalArgumentException- if the timeout is null.- Since:
- 2.3
-
expireAt
Set the expiration for givenkeyas a date timestamp.- Parameters:
key- must not be null.date- must not be null.- Returns:
- null when used in pipeline / transaction.
-
expireAt
Set the expiration for givenkeyas a date timestamp.- Parameters:
key- must not be null.expireAt- must not be null.- Returns:
- null when used in pipeline / transaction.
- Throws:
IllegalArgumentException- if the instant is null or too large to represent as aDate.- Since:
- 2.3
-
expire
ExpireChanges.ExpiryChangeState expire(@NonNull K key, @NonNull Expiration expiration, @NonNull ExpirationOptions options) Set the expiration for givenkey.- Parameters:
key- must not be null.expiration- must not be null.options- must not be null.- Returns:
- changes to the expiry. null when used in pipeline / transaction.
- Throws:
IllegalArgumentException- any of the required arguments is null.- Since:
- 3.5
- See Also:
-
expiration
Returns a bound operations object to perform expiration operations on the bound key.- Returns:
- the bound operations object to perform operations on the hash field expiration.
- Since:
- 3.5
-
persist
Remove the expiration from givenkey.- Parameters:
key- must not be null.- Returns:
truewhen persisted successfully or null when used in pipeline / transaction.- See Also:
-
getExpire
Get the time to live forkeyin seconds.- Parameters:
key- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
getExpire
Get the time to live forkeyin and convert it to the givenTimeUnit.- Parameters:
key- must not be null.timeUnit- must not be null.- Returns:
- null when used in pipeline / transaction.
- Since:
- 1.8
-
move
Move givenkeyto database withindex.- Parameters:
key- must not be null.dbIndex-- Returns:
- null when used in pipeline / transaction.
- See Also:
-
dump
Retrieve serialized version of the value stored atkey.- Parameters:
key- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
restore
default void restore(@NonNull K key, byte @NonNull [] value, long timeToLive, @NonNull TimeUnit unit) - Parameters:
key- must not be null.value- must not be null.timeToLive-unit- must not be null.- See Also:
-
restore
void restore(@NonNull K key, byte @NonNull [] value, long timeToLive, @NonNull TimeUnit unit, boolean replace) - Parameters:
key- must not be null.value- must not be null.timeToLive-unit- must not be null.replace- use true to replace a potentially existing value instead of erroring.- Since:
- 2.1
- See Also:
-
sort
Sort the elements forquery.- Parameters:
query- must not be null.- Returns:
- the results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
<T> List<T> sort(@NonNull SortQuery<@NonNull K> query, @NonNull RedisSerializer<T> resultSerializer) Sort the elements forqueryapplyingRedisSerializer.- Parameters:
query- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
Sort the elements forqueryapplyingBulkMapper.- Parameters:
query- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
<T,S> List<T> sort(SortQuery<@NonNull K> query, @NonNull BulkMapper<T, S> bulkMapper, @NonNull RedisSerializer<S> resultSerializer) - Parameters:
query- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
Sort the elements forqueryand store result instoreKey.- Parameters:
query- must not be null.storeKey- must not be null.- Returns:
- number of values. null when used in pipeline / transaction.
- See Also:
-
watch
Watch givenkeyfor modifications during transaction started withmulti().- Parameters:
key- must not be null.- See Also:
-
watch
Watch givenkeysfor modifications during transaction started withmulti().- Parameters:
keys- must not be null.- See Also:
-
unwatch
void unwatch()Flushes all the previouslywatch(Object)keys.- See Also:
-
multi
void multi()Mark the start of a transaction block.
Commands will be queued and can then be executed by callingexec()or rolled back usingdiscard()- See Also:
-
discard
void discard()Discard all commands issued aftermulti().- See Also:
-
exec
Executes all queued commands in a transaction started withmulti().
If used along withwatch(Object)the operation will fail if any of watched keys has been modified.- Returns:
- List of replies for each executed command.
- See Also:
-
exec
Execute a transaction, using the providedRedisSerializerto deserialize any results that are byte[]s or Collections of byte[]s. If a result is a Map, the providedRedisSerializerwill be used for both the keys and values. Other result types (Long, Boolean, etc) are left as-is in the converted results. Tuple results are automatically converted to TypedTuples.- Parameters:
valueSerializer- TheRedisSerializerto use for deserializing the results of transaction exec- Returns:
- The deserialized results of transaction exec
-
getClientList
List<@NonNull RedisClientInfo> getClientList()Request information and statistics about connected clients.- Returns:
ListofRedisClientInfoobjects.- Since:
- 1.3
-
killClient
Closes a given client connection identified by ip:port given inclient.- Parameters:
host- of connection to close.port- of connection to close- Since:
- 1.3
-
replicaOf
Change redis replication setting to new master.- Parameters:
host- must not be null.port-- Since:
- 1.3
- See Also:
-
replicaOfNoOne
void replicaOfNoOne()Change server into master.- Since:
- 1.3
- See Also:
-
convertAndSend
Publishes the given message to the given channel.- Parameters:
destination- the channel to publish to, must not be null.message- message to publish.- Returns:
- the number of clients that received the message. null when used in pipeline / transaction.
- See Also:
-
opsForCluster
@NonNull ClusterOperations<K,V> opsForCluster()Returns the cluster specific operations interface.- Returns:
- never null.
- Since:
- 1.7
-
opsForGeo
@NonNull GeoOperations<K,V> opsForGeo()Returns geospatial specific operations interface.- Returns:
- never null.
- Since:
- 1.8
-
boundGeoOps
Returns geospatial specific operations interface bound to the given key.- Parameters:
key- must not be null.- Returns:
- never null.
- Since:
- 1.8
-
opsForHash
Returns the operations performed on hash values.- Type Parameters:
HK- hash key (or field) typeHV- hash value type- Returns:
- hash operations
-
boundHashOps
Returns the operations performed on hash values bound to the given key.- Type Parameters:
HK- hash key (or field) typeHV- hash value type- Parameters:
key- Redis key- Returns:
- hash operations bound to the given key.
-
opsForHyperLogLog
@NonNull HyperLogLogOperations<K,V> opsForHyperLogLog()- Returns:
- never null.
- Since:
- 1.5
-
opsForList
@NonNull ListOperations<K,V> opsForList()Returns the operations performed on list values.- Returns:
- list operations
-
boundListOps
Returns the operations performed on list values bound to the given key.- Parameters:
key- Redis key- Returns:
- list operations bound to the given key
-
opsForSet
@NonNull SetOperations<K,V> opsForSet()Returns the operations performed on set values.- Returns:
- set operations
-
boundSetOps
Returns the operations performed on set values bound to the given key.- Parameters:
key- Redis key- Returns:
- set operations bound to the given key
-
opsForStream
Returns the operations performed on Streams.- Returns:
- stream operations.
- Since:
- 2.2
-
opsForStream
<HK,HV> @NonNull StreamOperations<K,HK, opsForStreamHV> (@NonNull HashMapper<? super K, ? super HK, ? super HV> hashMapper) Returns the operations performed on Streams.- Parameters:
hashMapper- theHashMapperto use when convertingObjectRecord.- Returns:
- stream operations.
- Since:
- 2.2
-
boundStreamOps
Returns the operations performed on Streams bound to the given key.- Returns:
- stream operations.
- Since:
- 2.2
-
opsForValue
@NonNull ValueOperations<K,V> opsForValue()Returns the operations performed on simple values (or Strings in Redis terminology).- Returns:
- value operations
-
boundValueOps
Returns the operations performed on simple values (or Strings in Redis terminology) bound to the given key.- Parameters:
key- Redis key- Returns:
- value operations bound to the given key
-
opsForZSet
@NonNull ZSetOperations<K,V> opsForZSet()Returns the operations performed on zset values (also known as sorted sets).- Returns:
- zset operations
-
boundZSetOps
Returns the operations performed on zset values (also known as sorted sets) bound to the given key.- Parameters:
key- Redis key- Returns:
- zset operations bound to the given key.
-
getKeySerializer
@Nullable RedisSerializer<?> getKeySerializer()- Returns:
- the key
RedisSerializer.
-
getValueSerializer
@Nullable RedisSerializer<?> getValueSerializer()- Returns:
- the value
RedisSerializer.
-
getHashKeySerializer
@Nullable RedisSerializer<?> getHashKeySerializer()- Returns:
- the hash key
RedisSerializer.
-
getHashValueSerializer
@Nullable RedisSerializer<?> getHashValueSerializer()- Returns:
- the hash value
RedisSerializer.
-