Interface RedisOperations<K,V>
- All Known Implementing Classes:
RedisTemplate
,StringRedisTemplate
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).- Author:
- Costin Leau, Christoph Strobl, Ninad Divadkar, Mark Paluch, ihaohong, Todd Merrill, Chen Li, Vedran Pavic
-
Method Summary
Modifier and TypeMethodDescriptionboundGeoOps
(K key) Returns geospatial specific operations interface bound to the given key.<HK,
HV> BoundHashOperations<K, HK, HV> boundHashOps
(K key) Returns the operations performed on hash values bound to the given key.boundListOps
(K key) Returns the operations performed on list values bound to the given key.boundSetOps
(K key) Returns the operations performed on set values bound to the given key.<HK,
HV> BoundStreamOperations<K, HK, HV> boundStreamOps
(K key) Returns the operations performed on Streams bound to the given key.boundValueOps
(K key) Returns the operations performed on simple values (or Strings in Redis terminology) bound to the given key.boundZSetOps
(K key) Returns the operations performed on zset values (also known as sorted sets) bound to the given key.convertAndSend
(String destination, Object message) Publishes the given message to the given channel.Copy givensourceKey
totargetKey
.countExistingKeys
(Collection<K> keys) Count the number ofkeys
that exist.delete
(Collection<K> keys) Delete givenkeys
.Delete givenkey
.void
discard()
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
(RedisSerializer<?> valueSerializer) Execute a transaction, using the providedRedisSerializer
to deserialize any results that are byte[]s or Collections of byte[]s.<T> T
execute
(RedisCallback<T> action) Executes the given action within a Redis connection.<T> T
execute
(RedisScript<T> script, List<K> keys, Object... args) Executes the givenRedisScript
<T> T
execute
(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSerializer<T> resultSerializer, List<K> keys, Object... args) Executes the givenRedisScript
, using the providedRedisSerializer
s to serialize the script arguments and result.<T> T
execute
(SessionCallback<T> session) Executes a Redis session.executePipelined
(RedisCallback<?> action) Executes the given action object on a pipelined connection, returning the results.executePipelined
(RedisCallback<?> action, RedisSerializer<?> resultSerializer) Executes the given action object on a pipelined connection, returning the results using a dedicated serializer.executePipelined
(SessionCallback<?> session) Executes the given Redis session on a pipelined connection.executePipelined
(SessionCallback<?> session, RedisSerializer<?> resultSerializer) Executes the given Redis session on a pipelined connection, returning the results using a dedicated serializer.<T extends Closeable>
TexecuteWithStickyConnection
(RedisCallback<T> callback) Allocates and binds a newRedisConnection
to the actual return type of the method.Set time to live for givenkey
.default Boolean
Set time to live for givenkey
.default Boolean
Set the expiration for givenkey
as a date timestamp.Set the expiration for givenkey
as a date timestamp.Request information and statistics about connected clients.Get the time to live forkey
in seconds.Get the time to live forkey
in and convert it to the givenTimeUnit
.Determine if givenkey
exists.Find all keys matching the givenpattern
.void
killClient
(String host, int port) Closes a given client connection identified by ip:port given inclient
.Move givenkey
to database withindex
.void
multi()
Mark the start of a transaction block.Returns the cluster specific operations interface.Returns geospatial specific operations interface.<HK,
HV> HashOperations<K, HK, HV> Returns the operations performed on hash values.Returns the operations performed on list values.Returns the operations performed on set values.<HK,
HV> StreamOperations<K, HK, HV> Returns the operations performed on Streams.<HK,
HV> StreamOperations<K, HK, HV> opsForStream
(HashMapper<? super K, ? super HK, ? super HV> hashMapper) Returns the operations performed on Streams.Returns the operations performed on simple values (or Strings in Redis terminology).Returns the operations performed on zset values (also known as sorted sets).Remove the expiration from givenkey
.Return a random key from the keyspace.void
Rename keyoldKey
tonewKey
.renameIfAbsent
(K oldKey, K newKey) Rename keyoldKey
tonewKey
only ifnewKey
does not exist.void
Change redis replication setting to new master.void
Change server into master.default void
void
scan
(ScanOptions options) Use aCursor
to iterate over keys.Sort the elements forquery
.Sort the elements forquery
and store result instoreKey
.<T,
S> List<T> sort
(SortQuery<K> query, BulkMapper<T, S> bulkMapper, RedisSerializer<S> resultSerializer) <T> List<T>
sort
(SortQuery<K> query, BulkMapper<T, V> bulkMapper) Sort the elements forquery
applyingBulkMapper
.<T> List<T>
sort
(SortQuery<K> query, RedisSerializer<T> resultSerializer) Sort the elements forquery
applyingRedisSerializer
.Determine the type stored atkey
.unlink
(Collection<K> keys) Unlink thekeys
from the keyspace.Unlink thekey
from the keyspace.void
unwatch()
Flushes all the previouslywatch(Object)
keys.void
watch
(Collection<K> keys) Watch givenkeys
for modifications during transaction started withmulti()
.void
Watch givenkey
for 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:
- a result object returned by the action or null
-
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 object returned by the action or null
-
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:
- list of objects returned by the pipeline
-
executePipelined
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:
- list of objects returned by the pipeline
-
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:
- list of objects returned by the pipeline
-
executePipelined
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:
- list of objects returned by the pipeline
-
execute
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
@Nullable <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSerializer<T> resultSerializer, List<K> keys, Object... args) Executes the givenRedisScript
, using the providedRedisSerializer
s to serialize the script arguments and result.- Parameters:
script
- The script to executeargsSerializer
- TheRedisSerializer
to use for serializing argsresultSerializer
- TheRedisSerializer
to 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 newRedisConnection
to 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:
- Since:
- 1.8
-
copy
Copy givensourceKey
totargetKey
.- Parameters:
sourceKey
- must not be null.targetKey
- must not be null.replace
- whether the key was copied. null when used in pipeline / transaction.- Returns:
- Since:
- 2.6
- See Also:
-
hasKey
Determine if givenkey
exists.- Parameters:
key
- must not be null.- Returns:
- See Also:
-
countExistingKeys
Count the number ofkeys
that 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 thekey
from 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 thekeys
from 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
Find all keys matching the givenpattern
.- Parameters:
pattern
- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
scan
Use aCursor
to 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
Return a random key from the keyspace.- Returns:
- null no keys exist or when used in pipeline / transaction.
- See Also:
-
rename
Rename keyoldKey
tonewKey
.- Parameters:
oldKey
- must not be null.newKey
- must not be null.- See Also:
-
renameIfAbsent
Rename keyoldKey
tonewKey
only ifnewKey
does 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 givenkey
as 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 givenkey
as 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
-
persist
Remove the expiration from givenkey
.- Parameters:
key
- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
getExpire
Get the time to live forkey
in seconds.- Parameters:
key
- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
getExpire
Get the time to live forkey
in 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 givenkey
to 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
- Parameters:
key
- must not be null.value
- must not be null.timeToLive
-unit
- must not be null.- See Also:
-
restore
- 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
Sort the elements forquery
applyingRedisSerializer
.- Parameters:
query
- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
Sort the elements forquery
applyingBulkMapper
.- Parameters:
query
- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
@Nullable <T,S> List<T> sort(SortQuery<K> query, BulkMapper<T, S> bulkMapper, 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 forquery
and 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 givenkey
for modifications during transaction started withmulti()
.- Parameters:
key
- must not be null.- See Also:
-
watch
Watch givenkeys
for 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 providedRedisSerializer
to deserialize any results that are byte[]s or Collections of byte[]s. If a result is a Map, the providedRedisSerializer
will 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
- TheRedisSerializer
to use for deserializing the results of transaction exec- Returns:
- The deserialized results of transaction exec
-
getClientList
Request information and statistics about connected clients.- Returns:
List
ofRedisClientInfo
objects.- 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
ClusterOperations<K,V> opsForCluster()Returns the cluster specific operations interface.- Returns:
- never null.
- Since:
- 1.7
-
opsForGeo
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
HyperLogLogOperations<K,V> opsForHyperLogLog()- Returns:
- Since:
- 1.5
-
opsForList
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
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> StreamOperations<K,HK, opsForStreamHV> (HashMapper<? super K, ? super HK, ? super HV> hashMapper) Returns the operations performed on Streams.- Parameters:
hashMapper
- theHashMapper
to 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
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
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
RedisSerializer<?> getKeySerializer()- Returns:
- the key
RedisSerializer
.
-
getValueSerializer
RedisSerializer<?> getValueSerializer()- Returns:
- the value
RedisSerializer
.
-
getHashKeySerializer
RedisSerializer<?> getHashKeySerializer()- Returns:
- the hash key
RedisSerializer
.
-
getHashValueSerializer
RedisSerializer<?> getHashValueSerializer()- Returns:
- the hash value
RedisSerializer
.
-