Class RedisTemplate<K,V>
- Type Parameters:
K- the Redis key type against which the template works (usually a String)V- the Redis value type against which the template works
- All Implemented Interfaces:
Aware, BeanClassLoaderAware, InitializingBean, RedisOperations<K,V>
- Direct Known Subclasses:
StringRedisTemplate
Performs automatic serialization/deserialization between the given objects and the underlying binary data in the
Redis store. By default, it uses Java serialization for its objects (through JdkSerializationRedisSerializer
). For String intensive operations consider the dedicated StringRedisTemplate.
The central method is execute(RedisCallback), supporting Redis access code implementing the
RedisCallback interface. It provides RedisConnection handling such that neither the
RedisCallback implementation nor the calling code needs to explicitly care about retrieving/closing Redis
connections, or handling Connection lifecycle exceptions. For typical single step actions, there are various
convenience methods.
Once configured, this class is thread-safe.
Note that while the template is generified, it is up to the serializers/deserializers to properly convert the given Objects to and from binary data.
This is the central class in Redis support.
- Author:
- Costin Leau, Christoph Strobl, Ninad Divadkar, Anqing Shao, Mark Paluch, Denis Zavedeev, ihaohong, Chen Li, Vedran Pavic
- See Also:
-
Field Summary
Fields inherited from class RedisAccessor
logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidboundGeoOps(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 channel, Object message) Publishes the given message to the given channel.Copy givensourceKeytotargetKey.countExistingKeys(Collection<K> keys) Count the number ofkeysthat exist.protected RedisConnectioncreateRedisConnectionProxy(RedisConnection connection) delete(Collection<K> keys) Delete givenkeys.Delete givenkey.voiddiscard()Discard all commands issued afterRedisOperations.multi().byte[]Executes the Redis dump command and returns the results.exec()Execute a transaction, using the defaultRedisSerializers to deserialize any results that are byte[]s or Collections or Maps of byte[]s or Tuples.exec(RedisSerializer<?> valueSerializer) Execute a transaction, using the providedRedisSerializerto deserialize any results that are byte[]s or Collections of byte[]s.execRaw()<T extends @Nullable Object>
Texecute(@NonNull RedisCallback<T> action) Executes the given action within a Redis connection.<T extends @Nullable Object>
Texecute(@NonNull RedisCallback<T> action, boolean exposeConnection) Executes the given action object within a connection, which can be exposed or not.<T extends @Nullable Object>
Texecute(@NonNull RedisCallback<T> action, boolean exposeConnection, boolean pipeline) Executes the given action object within a connection that can be exposed or not.<T extends @Nullable Object>
Texecute(@NonNull SessionCallback<T> session) Executes a Redis session.<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.executePipelined(@NonNull RedisCallback<?> action) Executes the given action object on a pipelined connection, returning the results.executePipelined(@NonNull RedisCallback<?> action, @Nullable 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, @Nullable 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.Set time to live for givenkey.@Nullable ExpireChanges.ExpiryChangeStateexpire(K key, Expiration expiration, ExpirationOptions options) Set the expiration for givenkey.Set the expiration for givenkeyas a date timestamp.Request information and statistics about connected clients.@Nullable RedisSerializer<?> Returns the default serializer used by this template.Get the time to live forkeyin seconds.Get the time to live forkeyin and convert it to the givenTimeUnit.Returns the hashKeySerializer.Returns the hashValueSerializer.Returns the key serializer used by this template.Returns the stringSerializer.Returns the value serializer used by this template.Determine if givenkeyexists.booleanReturns whether the default serializer should be used or not.booleanReturns whether the underlying RedisConnection should be directly exposed to the RedisCallback code, or rather a connection proxy (default behavior).Retrieve all keys matching the given pattern viaKEYScommand.voidkillClient(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.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.protected <T> @Nullable TpostProcessResult(@Nullable T result, RedisConnection conn, boolean existingConnection) protected RedisConnectionpreProcessConnection(RedisConnection connection, boolean existingConnection) Processes the connection (before any settings are executed on it).Return a random key from the keyspace.voidRename keyoldKeytonewKey.renameIfAbsent(K oldKey, K newKey) Rename keyoldKeytonewKeyonly ifnewKeydoes not exist.voidChange redis replication setting to new master.voidChange server into master.voidExecutes the Redis restore command.scan(ScanOptions options) Use aCursorto iterate over keys.voidsetBeanClassLoader(@Nullable ClassLoader classLoader) Sets theClassLoaderto be used for the defaultJdkSerializationRedisSerializerin case no otherRedisSerializeris explicitly set as the default one.voidsetDefaultSerializer(@Nullable RedisSerializer<?> serializer) Sets the default serializer to use for this template.voidsetEnableDefaultSerializer(boolean enableDefaultSerializer) Configure whether the default serializer should be used or not.voidsetEnableTransactionSupport(boolean enableTransactionSupport) Sets whether this template participates in ongoing transactions using MULTI...EXEC|DISCARD to keep track of operations.voidsetExposeConnection(boolean exposeConnection) Sets whether the underlying RedisConnection should be directly exposed to the RedisCallback code.voidsetHashKeySerializer(RedisSerializer<?> hashKeySerializer) Sets the hash key (or field) serializer to be used by this template.voidsetHashValueSerializer(RedisSerializer<?> hashValueSerializer) Sets the hash value serializer to be used by this template.voidsetKeySerializer(RedisSerializer<?> serializer) Sets the key serializer to be used by this template.voidsetScriptExecutor(ScriptExecutor<K> scriptExecutor) voidsetStringSerializer(RedisSerializer<String> stringSerializer) Sets the string value serializer to be used by this template (when the arguments or return types are always strings).voidsetValueSerializer(RedisSerializer<?> serializer) Sets the value serializer to be used by this template.Sort the elements forquery.Sort the elements forqueryand store result instoreKey.<T,S> List <T> sort(SortQuery<K> query, BulkMapper<T, S> bulkMapper, @Nullable RedisSerializer<S> resultSerializer) <T> List<T> sort(SortQuery<K> query, BulkMapper<T, V> bulkMapper) Sort the elements forqueryapplyingBulkMapper.<T> List<T> sort(SortQuery<K> query, @Nullable RedisSerializer<T> resultSerializer) Sort the elements forqueryapplyingRedisSerializer.Determine the type stored atkey.unlink(Collection<K> keys) Unlink thekeysfrom the keyspace.Unlink thekeyfrom the keyspace.voidunwatch()Flushes all the previouslyRedisOperations.watch(Object)keys.voidwatch(Collection<K> keys) Watch givenkeysfor modifications during transaction started withRedisOperations.multi().voidWatch givenkeyfor modifications during transaction started withRedisOperations.multi().Methods inherited from class RedisAccessor
getConnectionFactory, getRequiredConnectionFactory, setConnectionFactoryMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface RedisOperations
expiration, expire, expireAt, restore
-
Constructor Details
-
RedisTemplate
public RedisTemplate()Constructs a newRedisTemplateinstance.
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceInitializingBean- Overrides:
afterPropertiesSetin classRedisAccessor
-
isExposeConnection
public boolean isExposeConnection()Returns whether the underlying RedisConnection should be directly exposed to the RedisCallback code, or rather a connection proxy (default behavior).- Returns:
- true to expose the native Redis connection or false to provide a proxied connection to RedisCallback code.
-
setExposeConnection
public void setExposeConnection(boolean exposeConnection) Sets whether the underlying RedisConnection should be directly exposed to the RedisCallback code. By default, the connection is not exposed, and a proxy is used instead. This proxy suppresses potentially disruptive operations, such asquitanddisconnectcommands, ensuring that the connection remains stable during the callback execution. Defaults to proxy use.- Parameters:
exposeConnection- true to expose the actual Redis connection to RedisCallback code, allowing full access to Redis commands, including quit and disconnect. false to proxy connections that suppress the quit and disconnect commands, protecting the connection from being inadvertently closed during callback execution.
-
isEnableDefaultSerializer
public boolean isEnableDefaultSerializer()Returns whether the default serializer should be used or not.- Returns:
- true if the default serializer should be used; false otherwise.
-
setEnableDefaultSerializer
public void setEnableDefaultSerializer(boolean enableDefaultSerializer) Configure whether the default serializer should be used or not. If the default serializer is enabled, the template will use it to serialize and deserialize values. However, if the default serializer is disabled , any serializers that have not been explicitly set will remain null, and their corresponding values will neither be serialized nor deserialized. Defaults to true.- Parameters:
enableDefaultSerializer- true if the default serializer should be used; false otherwise.
-
setEnableTransactionSupport
public void setEnableTransactionSupport(boolean enableTransactionSupport) Sets whether this template participates in ongoing transactions using MULTI...EXEC|DISCARD to keep track of operations.- Parameters:
enableTransactionSupport- trueto participate in ongoing transactions; false to not track transactions.- Since:
- 1.3
- See Also:
-
setBeanClassLoader
Sets theClassLoaderto be used for the defaultJdkSerializationRedisSerializerin case no otherRedisSerializeris explicitly set as the default one.- Specified by:
setBeanClassLoaderin interfaceBeanClassLoaderAware- Parameters:
classLoader- can be null.- Since:
- 1.8
- See Also:
-
getDefaultSerializer
Returns the default serializer used by this template.- Returns:
- template default serializer.
-
setDefaultSerializer
Sets the default serializer to use for this template. All serializers (except thesetStringSerializer(RedisSerializer)) are initialized to this value unless explicitly set. Defaults toJdkSerializationRedisSerializer.- Parameters:
serializer- default serializer to use.
-
setKeySerializer
Sets the key serializer to be used by this template. Defaults togetDefaultSerializer().- Parameters:
serializer- the key serializer to be used by this template.
-
getKeySerializer
Returns the key serializer used by this template.- Specified by:
getKeySerializerin interfaceRedisOperations<K,V> - Returns:
- the key serializer used by this template.
-
setValueSerializer
Sets the value serializer to be used by this template. Defaults togetDefaultSerializer().- Parameters:
serializer- the value serializer to be used by this template.
-
getValueSerializer
Returns the value serializer used by this template.- Specified by:
getValueSerializerin interfaceRedisOperations<K,V> - Returns:
- the value serializer used by this template.
-
getHashKeySerializer
Returns the hashKeySerializer.- Specified by:
getHashKeySerializerin interfaceRedisOperations<K,V> - Returns:
- Returns the hashKeySerializer
-
setHashKeySerializer
Sets the hash key (or field) serializer to be used by this template. Defaults togetDefaultSerializer().- Parameters:
hashKeySerializer- The hashKeySerializer to set.
-
getHashValueSerializer
Returns the hashValueSerializer.- Specified by:
getHashValueSerializerin interfaceRedisOperations<K,V> - Returns:
- Returns the hashValueSerializer
-
setHashValueSerializer
Sets the hash value serializer to be used by this template. Defaults togetDefaultSerializer().- Parameters:
hashValueSerializer- The hashValueSerializer to set.
-
getStringSerializer
Returns the stringSerializer.- Returns:
- Returns the stringSerializer
-
setStringSerializer
Sets the string value serializer to be used by this template (when the arguments or return types are always strings). Defaults toStringRedisSerializer.- Parameters:
stringSerializer- The stringValueSerializer to set.- See Also:
-
setScriptExecutor
- Parameters:
scriptExecutor- TheScriptExecutorto use for executing Redis scripts
-
execute
Description copied from interface:RedisOperationsExecutes 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.- Specified by:
executein interfaceRedisOperations<K,V> - 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
public <T extends @Nullable Object> T execute(@NonNull RedisCallback<T> action, boolean exposeConnection) Executes the given action object within a connection, which can be exposed or not.- Type Parameters:
T- return type- Parameters:
action- callback object that specifies the Redis actionexposeConnection- whether to enforce exposure of the native Redis Connection to callback code- Returns:
- object returned by the action
-
execute
public <T extends @Nullable Object> T execute(@NonNull RedisCallback<T> action, boolean exposeConnection, boolean pipeline) Executes the given action object within a connection that can be exposed or not. Additionally, the connection can be pipelined. Note the results of the pipeline are discarded (making it suitable for write-only scenarios).- Type Parameters:
T- return type- Parameters:
action- callback object to executeexposeConnection- whether to enforce exposure of the native Redis Connection to callback codepipeline- whether to pipeline or not the connection for the execution- Returns:
- object returned by the action
-
execute
Description copied from interface:RedisOperationsExecutes a Redis session. Allows multiple operations to be executed in the same session enabling 'transactional' capabilities throughRedisOperations.multi()andRedisOperations.watch(Collection)operations.- Specified by:
executein interfaceRedisOperations<K,V> - Type Parameters:
T- return type- Parameters:
session- session callback. Must not be null.- Returns:
- result of the given
SessionCallback.execute(RedisOperations)invocation.
-
executePipelined
Description copied from interface:RedisOperationsExecutes 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.- Specified by:
executePipelinedin interfaceRedisOperations<K,V> - 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
public List<Object> executePipelined(@NonNull SessionCallback<?> session, @Nullable RedisSerializer<?> resultSerializer) Description copied from interface:RedisOperationsExecutes 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.- Specified by:
executePipelinedin interfaceRedisOperations<K,V> - 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.
-
executePipelined
Description copied from interface:RedisOperationsExecutes 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- Specified by:
executePipelinedin interfaceRedisOperations<K,V> - 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
public List<Object> executePipelined(@NonNull RedisCallback<?> action, @Nullable RedisSerializer<?> resultSerializer) Description copied from interface:RedisOperationsExecutes 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.- Specified by:
executePipelinedin interfaceRedisOperations<K,V> - 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.
-
execute
public <T extends @Nullable Object> T execute(@NonNull RedisScript<T> script, @NonNull List<@NonNull K> keys, @NonNull Object @NonNull ... args) Description copied from interface:RedisOperationsExecutes the givenRedisScript- Specified by:
executein interfaceRedisOperations<K,V> - 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
public <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) Description copied from interface:RedisOperationsExecutes the givenRedisScript, using the providedRedisSerializers to serialize the script arguments and result.- Specified by:
executein interfaceRedisOperations<K,V> - 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
Description copied from interface:RedisOperationsAllocates and binds a newRedisConnectionto the actual return type of the method. It is up to the caller to free resources after use.- Specified by:
executeWithStickyConnectionin interfaceRedisOperations<K,V> - Parameters:
callback- must not be null.- Returns:
- the
resultof the operation performed in the callback or null.
-
createRedisConnectionProxy
-
preProcessConnection
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) Processes the connection (before any settings are executed on it). Default implementation returns the connection as is.- Parameters:
connection- redis connection
-
postProcessResult
protected <T> @Nullable T postProcessResult(@Nullable T result, RedisConnection conn, boolean existingConnection) -
copy
Description copied from interface:RedisOperationsCopy givensourceKeytotargetKey.- Specified by:
copyin interfaceRedisOperations<K,V> - Parameters:
source- must not be null.target- 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.- See Also:
-
hasKey
Description copied from interface:RedisOperationsDetermine if givenkeyexists.- Specified by:
hasKeyin interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- Returns:
- true if key exists. null when used in pipeline / transaction.
- See Also:
-
countExistingKeys
Description copied from interface:RedisOperationsCount the number ofkeysthat exist.- Specified by:
countExistingKeysin interfaceRedisOperations<K,V> - 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.
- See Also:
-
delete
Description copied from interface:RedisOperationsDelete givenkey.- Specified by:
deletein interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- Returns:
- true if the key was removed.
- See Also:
-
delete
Description copied from interface:RedisOperationsDelete givenkeys.- Specified by:
deletein interfaceRedisOperations<K,V> - Parameters:
keys- must not be null.- Returns:
- The number of keys that were removed. null when used in pipeline / transaction.
- See Also:
-
unlink
Description copied from interface:RedisOperationsUnlink thekeyfrom the keyspace. Unlike withRedisOperations.delete(Object)the actual memory reclaiming here happens asynchronously.- Specified by:
unlinkin interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- Returns:
- The number of keys that were removed. null when used in pipeline / transaction.
- See Also:
-
unlink
Description copied from interface:RedisOperationsUnlink thekeysfrom the keyspace. Unlike withRedisOperations.delete(Collection)the actual memory reclaiming here happens asynchronously.- Specified by:
unlinkin interfaceRedisOperations<K,V> - Parameters:
keys- must not be null.- Returns:
- The number of keys that were removed. null when used in pipeline / transaction.
- See Also:
-
type
Description copied from interface:RedisOperationsDetermine the type stored atkey.- Specified by:
typein interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
keys
Description copied from interface:RedisOperationsRetrieve all keys matching the given pattern viaKEYScommand.IMPORTANT: This command is non-interruptible and scans the entire keyspace which may cause performance issues. Consider
RedisOperations.scan(ScanOptions)for large datasets.- Specified by:
keysin interfaceRedisOperations<K,V> - Parameters:
pattern- key pattern- Returns:
- set of matching keys, or null when used in pipeline / transaction
- See Also:
-
scan
Description copied from interface:RedisOperationsUse aCursorto iterate over keys.
Important: CallCloseableIterator.close()when done to avoid resource leaks.- Specified by:
scanin interfaceRedisOperations<K,V> - 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).
- See Also:
-
randomKey
Description copied from interface:RedisOperationsReturn a random key from the keyspace.- Specified by:
randomKeyin interfaceRedisOperations<K,V> - Returns:
- null no keys exist or when used in pipeline / transaction.
- See Also:
-
rename
Description copied from interface:RedisOperationsRename keyoldKeytonewKey.- Specified by:
renamein interfaceRedisOperations<K,V> - Parameters:
oldKey- must not be null.newKey- must not be null.- See Also:
-
renameIfAbsent
Description copied from interface:RedisOperationsRename keyoldKeytonewKeyonly ifnewKeydoes not exist.- Specified by:
renameIfAbsentin interfaceRedisOperations<K,V> - Parameters:
oldKey- must not be null.newKey- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
expire
Description copied from interface:RedisOperationsSet time to live for givenkey.- Specified by:
expirein interfaceRedisOperations<K,V> - Parameters:
key- must not be null.timeout-unit- must not be null.- Returns:
- null when used in pipeline / transaction.
-
expireAt
Description copied from interface:RedisOperationsSet the expiration for givenkeyas a date timestamp.- Specified by:
expireAtin interfaceRedisOperations<K,V> - Parameters:
key- must not be null.date- must not be null.- Returns:
- null when used in pipeline / transaction.
-
expire
public @Nullable ExpireChanges.ExpiryChangeState expire(K key, Expiration expiration, ExpirationOptions options) Description copied from interface:RedisOperationsSet the expiration for givenkey.- Specified by:
expirein interfaceRedisOperations<K,V> - 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.
- See Also:
-
persist
Description copied from interface:RedisOperationsRemove the expiration from givenkey.- Specified by:
persistin interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- Returns:
truewhen persisted successfully or null when used in pipeline / transaction.- See Also:
-
getExpire
Description copied from interface:RedisOperationsGet the time to live forkeyin seconds.- Specified by:
getExpirein interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- Returns:
- null when used in pipeline / transaction.
- See Also:
-
getExpire
Description copied from interface:RedisOperationsGet the time to live forkeyin and convert it to the givenTimeUnit.- Specified by:
getExpirein interfaceRedisOperations<K,V> - Parameters:
key- must not be null.timeUnit- must not be null.- Returns:
- null when used in pipeline / transaction.
-
move
Description copied from interface:RedisOperationsMove givenkeyto database withindex.- Specified by:
movein interfaceRedisOperations<K,V> - Parameters:
key- must not be null.dbIndex-- Returns:
- null when used in pipeline / transaction.
- See Also:
-
dump
Executes the Redis dump command and returns the results. Redis uses a non-standard serialization mechanism and includes checksum information, thus the raw bytes are returned as opposed to deserializing with valueSerializer. Use the return value of dump as the value argument to restore- Specified by:
dumpin interfaceRedisOperations<K,V> - Parameters:
key- The key to dump- Returns:
- results The results of the dump operation
- See Also:
-
restore
Executes the Redis restore command. The value passed in should be the exact serialized data returned fromdump(Object), since Redis uses a non-standard serialization mechanism.- Specified by:
restorein interfaceRedisOperations<K,V> - Parameters:
key- The key to restorevalue- The value to restore, as returned bydump(Object)timeToLive- An expiration for the restored key, or 0 for no expirationunit- The time unit for timeToLivereplace- use true to replace a potentially existing value instead of erroring.- Throws:
RedisSystemException- if the key you are attempting to restore already exists andreplaceis set to false.- See Also:
-
sort
Description copied from interface:RedisOperationsSort the elements forquery.- Specified by:
sortin interfaceRedisOperations<K,V> - Parameters:
query- must not be null.- Returns:
- the results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
Description copied from interface:RedisOperationsSort the elements forqueryapplyingRedisSerializer.- Specified by:
sortin interfaceRedisOperations<K,V> - Parameters:
query- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
Description copied from interface:RedisOperationsSort the elements forqueryapplyingBulkMapper.- Specified by:
sortin interfaceRedisOperations<K,V> - Parameters:
query- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
public <T,S> List<T> sort(SortQuery<K> query, BulkMapper<T, S> bulkMapper, @Nullable RedisSerializer<S> resultSerializer) Description copied from interface:RedisOperations- Specified by:
sortin interfaceRedisOperations<K,V> - Parameters:
query- must not be null.- Returns:
- the deserialized results of sort. null when used in pipeline / transaction.
- See Also:
-
sort
Description copied from interface:RedisOperationsSort the elements forqueryand store result instoreKey.- Specified by:
sortin interfaceRedisOperations<K,V> - Parameters:
query- must not be null.storeKey- must not be null.- Returns:
- number of values. null when used in pipeline / transaction.
- See Also:
-
watch
Description copied from interface:RedisOperationsWatch givenkeyfor modifications during transaction started withRedisOperations.multi().- Specified by:
watchin interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- See Also:
-
watch
Description copied from interface:RedisOperationsWatch givenkeysfor modifications during transaction started withRedisOperations.multi().- Specified by:
watchin interfaceRedisOperations<K,V> - Parameters:
keys- must not be null.- See Also:
-
unwatch
public void unwatch()Description copied from interface:RedisOperationsFlushes all the previouslyRedisOperations.watch(Object)keys.- Specified by:
unwatchin interfaceRedisOperations<K,V> - See Also:
-
multi
public void multi()Description copied from interface:RedisOperationsMark the start of a transaction block.
Commands will be queued and can then be executed by callingRedisOperations.exec()or rolled back usingRedisOperations.discard()- Specified by:
multiin interfaceRedisOperations<K,V> - See Also:
-
discard
public void discard()Description copied from interface:RedisOperationsDiscard all commands issued afterRedisOperations.multi().- Specified by:
discardin interfaceRedisOperations<K,V> - See Also:
-
exec
Execute a transaction, using the defaultRedisSerializers to deserialize any results that are byte[]s or Collections or Maps of byte[]s or Tuples. Other result types (Long, Boolean, etc) are left as-is in the converted results. If conversion of tx results has been disabled in theRedisConnectionFactory, the results of exec will be returned without deserialization. This check is mostly for backwards compatibility with 1.0.- Specified by:
execin interfaceRedisOperations<K,V> - Returns:
- The (possibly deserialized) results of transaction exec
- See Also:
-
exec
Description copied from interface:RedisOperationsExecute 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.- Specified by:
execin interfaceRedisOperations<K,V> - Parameters:
valueSerializer- TheRedisSerializerto use for deserializing the results of transaction exec- Returns:
- The deserialized results of transaction exec
-
execRaw
-
getClientList
Description copied from interface:RedisOperationsRequest information and statistics about connected clients.- Specified by:
getClientListin interfaceRedisOperations<K,V> - Returns:
ListofRedisClientInfoobjects.
-
killClient
Description copied from interface:RedisOperationsCloses a given client connection identified by ip:port given inclient.- Specified by:
killClientin interfaceRedisOperations<K,V> - Parameters:
host- of connection to close.port- of connection to close
-
replicaOf
Description copied from interface:RedisOperationsChange redis replication setting to new master.- Specified by:
replicaOfin interfaceRedisOperations<K,V> - Parameters:
host- must not be null.port-- See Also:
-
replicaOfNoOne
public void replicaOfNoOne()Description copied from interface:RedisOperationsChange server into master.- Specified by:
replicaOfNoOnein interfaceRedisOperations<K,V> - See Also:
-
convertAndSend
Description copied from interface:RedisOperationsPublishes the given message to the given channel.- Specified by:
convertAndSendin interfaceRedisOperations<K,V> - Parameters:
channel- 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
Description copied from interface:RedisOperationsReturns the cluster specific operations interface.- Specified by:
opsForClusterin interfaceRedisOperations<K,V> - Returns:
- never null.
-
opsForGeo
Description copied from interface:RedisOperationsReturns geospatial specific operations interface.- Specified by:
opsForGeoin interfaceRedisOperations<K,V> - Returns:
- never null.
-
boundGeoOps
Description copied from interface:RedisOperationsReturns geospatial specific operations interface bound to the given key.- Specified by:
boundGeoOpsin interfaceRedisOperations<K,V> - Parameters:
key- must not be null.- Returns:
- never null.
-
boundHashOps
Description copied from interface:RedisOperationsReturns the operations performed on hash values bound to the given key.- Specified by:
boundHashOpsin interfaceRedisOperations<K,V> - Type Parameters:
HK- hash key (or field) typeHV- hash value type- Parameters:
key- Redis key- Returns:
- hash operations bound to the given key.
-
opsForHash
Description copied from interface:RedisOperationsReturns the operations performed on hash values.- Specified by:
opsForHashin interfaceRedisOperations<K,V> - Type Parameters:
HK- hash key (or field) typeHV- hash value type- Returns:
- hash operations
-
opsForHyperLogLog
- Specified by:
opsForHyperLogLogin interfaceRedisOperations<K,V> - Returns:
- never null.
-
opsForList
Description copied from interface:RedisOperationsReturns the operations performed on list values.- Specified by:
opsForListin interfaceRedisOperations<K,V> - Returns:
- list operations
-
boundListOps
Description copied from interface:RedisOperationsReturns the operations performed on list values bound to the given key.- Specified by:
boundListOpsin interfaceRedisOperations<K,V> - Parameters:
key- Redis key- Returns:
- list operations bound to the given key
-
boundSetOps
Description copied from interface:RedisOperationsReturns the operations performed on set values bound to the given key.- Specified by:
boundSetOpsin interfaceRedisOperations<K,V> - Parameters:
key- Redis key- Returns:
- set operations bound to the given key
-
opsForSet
Description copied from interface:RedisOperationsReturns the operations performed on set values.- Specified by:
opsForSetin interfaceRedisOperations<K,V> - Returns:
- set operations
-
opsForStream
Description copied from interface:RedisOperationsReturns the operations performed on Streams.- Specified by:
opsForStreamin interfaceRedisOperations<K,V> - Returns:
- stream operations.
-
opsForStream
public <HK,HV> StreamOperations<K,HK, opsForStreamHV> (HashMapper<? super K, ? super HK, ? super HV> hashMapper) Description copied from interface:RedisOperationsReturns the operations performed on Streams.- Specified by:
opsForStreamin interfaceRedisOperations<K,V> - Parameters:
hashMapper- theHashMapperto use when convertingObjectRecord.- Returns:
- stream operations.
-
boundStreamOps
Description copied from interface:RedisOperationsReturns the operations performed on Streams bound to the given key.- Specified by:
boundStreamOpsin interfaceRedisOperations<K,V> - Returns:
- stream operations.
-
boundValueOps
Description copied from interface:RedisOperationsReturns the operations performed on simple values (or Strings in Redis terminology) bound to the given key.- Specified by:
boundValueOpsin interfaceRedisOperations<K,V> - Parameters:
key- Redis key- Returns:
- value operations bound to the given key
-
opsForValue
Description copied from interface:RedisOperationsReturns the operations performed on simple values (or Strings in Redis terminology).- Specified by:
opsForValuein interfaceRedisOperations<K,V> - Returns:
- value operations
-
boundZSetOps
Description copied from interface:RedisOperationsReturns the operations performed on zset values (also known as sorted sets) bound to the given key.- Specified by:
boundZSetOpsin interfaceRedisOperations<K,V> - Parameters:
key- Redis key- Returns:
- zset operations bound to the given key.
-
opsForZSet
Description copied from interface:RedisOperationsReturns the operations performed on zset values (also known as sorted sets).- Specified by:
opsForZSetin interfaceRedisOperations<K,V> - Returns:
- zset operations
-