K
- the Redis key type against which the template works (usually a String)V
- the Redis value type against which the template workspublic class RedisTemplate<K,V> extends RedisAccessor implements RedisOperations<K,V>
JdkSerializationRedisSerializer
). For String intensive operations consider the dedicated StringRedisTemplate
.
The central method is execute, 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.StringRedisTemplate
logger
Constructor and Description |
---|
RedisTemplate()
Constructs a new
RedisTemplate instance. |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
<HK,HV> BoundHashOperations<K,HK,HV> |
boundHashOps(K key)
Returns the operations performed on hash values bound to the given key.
|
BoundListOperations<K,V> |
boundListOps(K key)
Returns the operations performed on list values bound to the given key.
|
BoundSetOperations<K,V> |
boundSetOps(K key)
Returns the operations performed on set values bound to the given key.
|
BoundValueOperations<K,V> |
boundValueOps(K key)
Returns the operations performed on simple values (or Strings in Redis terminology) bound to the given key.
|
BoundZSetOperations<K,V> |
boundZSetOps(K key)
Returns the operations performed on zset values (also known as sorted sets) bound to the given key.
|
void |
convertAndSend(String channel,
Object message) |
protected RedisConnection |
createRedisConnectionProxy(RedisConnection pm) |
void |
delete(Collection<K> keys) |
void |
delete(K key) |
void |
discard() |
byte[] |
dump(K key)
Executes the Redis dump command and returns the results.
|
List<Object> |
exec()
Execute a transaction, using the default
RedisSerializer s to deserialize any results that are byte[]s or
Collections or Maps of byte[]s or Tuples. |
List<Object> |
exec(RedisSerializer<?> valueSerializer)
Execute a transaction, using the provided
RedisSerializer to deserialize any results that are byte[]s or
Collections of byte[]s. |
protected List<Object> |
execRaw() |
<T> T |
execute(RedisCallback<T> action)
Executes the given action within a Redis connection.
|
<T> T |
execute(RedisCallback<T> action,
boolean exposeConnection)
Executes the given action object within a connection, which can be exposed or not.
|
<T> T |
execute(RedisCallback<T> action,
boolean exposeConnection,
boolean pipeline)
Executes the given action object within a connection that can be exposed or not.
|
<T> T |
execute(RedisScript<T> script,
List<K> keys,
Object... args)
Executes the given
RedisScript |
<T> T |
execute(RedisScript<T> script,
RedisSerializer<?> argsSerializer,
RedisSerializer<T> resultSerializer,
List<K> keys,
Object... args)
Executes the given
RedisScript , using the provided RedisSerializer s to serialize the script
arguments and result. |
<T> T |
execute(SessionCallback<T> session)
Executes a Redis session.
|
List<Object> |
executePipelined(RedisCallback<?> action)
Executes the given action object on a pipelined connection, returning the results.
|
List<Object> |
executePipelined(RedisCallback<?> action,
RedisSerializer<?> resultSerializer)
Executes the given action object on a pipelined connection, returning the results using a dedicated serializer.
|
List<Object> |
executePipelined(SessionCallback<?> session)
Executes the given Redis session on a pipelined connection.
|
List<Object> |
executePipelined(SessionCallback<?> session,
RedisSerializer<?> resultSerializer)
Executes the given Redis session on a pipelined connection, returning the results using a dedicated serializer.
|
Boolean |
expire(K key,
long timeout,
TimeUnit unit) |
Boolean |
expireAt(K key,
Date date) |
List<RedisClientInfo> |
getClientList()
/** Request information and statistics about connected clients.
|
RedisSerializer<?> |
getDefaultSerializer()
Returns the default serializer used by this template.
|
Long |
getExpire(K key) |
Long |
getExpire(K key,
TimeUnit timeUnit) |
RedisSerializer<?> |
getHashKeySerializer()
Returns the hashKeySerializer.
|
RedisSerializer<?> |
getHashValueSerializer()
Returns the hashValueSerializer.
|
RedisSerializer<?> |
getKeySerializer()
Returns the key serializer used by this template.
|
RedisSerializer<String> |
getStringSerializer()
Returns the stringSerializer.
|
RedisSerializer<?> |
getValueSerializer()
Returns the value serializer used by this template.
|
Boolean |
hasKey(K key) |
boolean |
isEnableDefaultSerializer() |
boolean |
isExposeConnection()
Returns whether to expose the native Redis connection to RedisCallback code, or rather a connection proxy (the
default).
|
Set<K> |
keys(K pattern) |
void |
killClient(String host,
int port)
Closes a given client connection identified by ip:port given in
client . |
Boolean |
move(K key,
int dbIndex) |
void |
multi()
'
|
<HK,HV> HashOperations<K,HK,HV> |
opsForHash()
Returns the operations performed on hash values.
|
HyperLogLogOperations<K,V> |
opsForHyperLogLog() |
ListOperations<K,V> |
opsForList()
Returns the operations performed on list values.
|
SetOperations<K,V> |
opsForSet()
Returns the operations performed on set values.
|
ValueOperations<K,V> |
opsForValue()
Returns the operations performed on simple values (or Strings in Redis terminology).
|
ZSetOperations<K,V> |
opsForZSet()
Returns the operations performed on zset values (also known as sorted sets).
|
Boolean |
persist(K key) |
protected <T> T |
postProcessResult(T result,
RedisConnection conn,
boolean existingConnection) |
protected RedisConnection |
preProcessConnection(RedisConnection connection,
boolean existingConnection)
Processes the connection (before any settings are executed on it).
|
K |
randomKey() |
void |
rename(K oldKey,
K newKey) |
Boolean |
renameIfAbsent(K oldKey,
K newKey) |
void |
restore(K key,
byte[] value,
long timeToLive,
TimeUnit unit)
Executes the Redis restore command.
|
void |
setDefaultSerializer(RedisSerializer<?> serializer)
Sets the default serializer to use for this template.
|
void |
setEnableDefaultSerializer(boolean enableDefaultSerializer) |
void |
setEnableTransactionSupport(boolean enableTransactionSupport)
If set to
true RedisTemplate will use MULTI...EXEC|DISCARD to keep track of operations. |
void |
setExposeConnection(boolean exposeConnection)
Sets whether to expose the Redis connection to
RedisCallback code. |
void |
setHashKeySerializer(RedisSerializer<?> hashKeySerializer)
Sets the hash key (or field) serializer to be used by this template.
|
void |
setHashValueSerializer(RedisSerializer<?> hashValueSerializer)
Sets the hash value serializer to be used by this template.
|
void |
setKeySerializer(RedisSerializer<?> serializer)
Sets the key serializer to be used by this template.
|
void |
setScriptExecutor(ScriptExecutor<K> scriptExecutor) |
void |
setStringSerializer(RedisSerializer<String> stringSerializer)
Sets the string value serializer to be used by this template (when the arguments or return types are always
strings).
|
void |
setValueSerializer(RedisSerializer<?> serializer)
Sets the value serializer to be used by this template.
|
void |
slaveOf(String host,
int port)
Change redis replication setting to new master.
|
void |
slaveOfNoOne()
Change server into master.
|
List<V> |
sort(SortQuery<K> query) |
<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) |
Long |
sort(SortQuery<K> query,
K storeKey) |
<T> List<T> |
sort(SortQuery<K> query,
RedisSerializer<T> resultSerializer) |
DataType |
type(K key) |
void |
unwatch() |
void |
watch(Collection<K> keys) |
void |
watch(K key) |
getConnectionFactory, setConnectionFactory
public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
afterPropertiesSet
in class RedisAccessor
public <T> T execute(RedisCallback<T> action)
RedisOperations
execute
in interface RedisOperations<K,V>
T
- return typeaction
- callback object that specifies the Redis actionpublic <T> T execute(RedisCallback<T> action, boolean exposeConnection)
T
- return typeaction
- callback object that specifies the Redis actionexposeConnection
- whether to enforce exposure of the native Redis Connection to callback codepublic <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline)
T
- return typeaction
- 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 executionpublic <T> T execute(SessionCallback<T> session)
RedisOperations
RedisOperations.multi()
and RedisOperations.watch(Collection)
operations.execute
in interface RedisOperations<K,V>
T
- return typesession
- session callbackpublic List<Object> executePipelined(SessionCallback<?> session)
RedisOperations
executePipelined
in interface RedisOperations<K,V>
session
- Session callbackpublic List<Object> executePipelined(SessionCallback<?> session, RedisSerializer<?> resultSerializer)
RedisOperations
executePipelined
in interface RedisOperations<K,V>
session
- Session callbackpublic List<Object> executePipelined(RedisCallback<?> action)
RedisOperations
executePipelined
in interface RedisOperations<K,V>
action
- callback object to executepublic List<Object> executePipelined(RedisCallback<?> action, RedisSerializer<?> resultSerializer)
RedisOperations
executePipelined
in interface RedisOperations<K,V>
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 valuepublic <T> T execute(RedisScript<T> script, List<K> keys, Object... args)
RedisOperations
RedisScript
execute
in interface RedisOperations<K,V>
script
- The script to executekeys
- Any keys that need to be passed to the scriptargs
- Any args that need to be passed to the scriptRedisScript.getResultType()
is null, likely indicating a
throw-away status reply (i.e. "OK")public <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSerializer<T> resultSerializer, List<K> keys, Object... args)
RedisOperations
RedisScript
, using the provided RedisSerializer
s to serialize the script
arguments and result.execute
in interface RedisOperations<K,V>
script
- The script to executeargsSerializer
- The RedisSerializer
to use for serializing argsresultSerializer
- The RedisSerializer
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 scriptRedisScript.getResultType()
is null, likely indicating a
throw-away status reply (i.e. "OK")protected RedisConnection createRedisConnectionProxy(RedisConnection pm)
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection)
connection
- redis connectionprotected <T> T postProcessResult(T result, RedisConnection conn, boolean existingConnection)
public boolean isExposeConnection()
public void setExposeConnection(boolean exposeConnection)
RedisCallback
code. Default is "false": a proxy will be
returned, suppressing quit and disconnect calls.exposeConnection
- public boolean isEnableDefaultSerializer()
public void setEnableDefaultSerializer(boolean enableDefaultSerializer)
enableDefaultSerializer
- Whether or not the default serializer should be used. If not, any serializers not
explicilty set will remain null and values will not be serialized or deserialized.public RedisSerializer<?> getDefaultSerializer()
public void setDefaultSerializer(RedisSerializer<?> serializer)
setStringSerializer(RedisSerializer)
) are initialized to this value unless explicitly set. Defaults to
JdkSerializationRedisSerializer
.serializer
- default serializer to usepublic void setKeySerializer(RedisSerializer<?> serializer)
getDefaultSerializer()
.serializer
- the key serializer to be used by this template.public RedisSerializer<?> getKeySerializer()
getKeySerializer
in interface RedisOperations<K,V>
public void setValueSerializer(RedisSerializer<?> serializer)
getDefaultSerializer()
.serializer
- the value serializer to be used by this template.public RedisSerializer<?> getValueSerializer()
getValueSerializer
in interface RedisOperations<K,V>
public RedisSerializer<?> getHashKeySerializer()
getHashKeySerializer
in interface RedisOperations<K,V>
public void setHashKeySerializer(RedisSerializer<?> hashKeySerializer)
getDefaultSerializer()
.hashKeySerializer
- The hashKeySerializer to set.public RedisSerializer<?> getHashValueSerializer()
getHashValueSerializer
in interface RedisOperations<K,V>
public void setHashValueSerializer(RedisSerializer<?> hashValueSerializer)
getDefaultSerializer()
.hashValueSerializer
- The hashValueSerializer to set.public RedisSerializer<String> getStringSerializer()
public void setStringSerializer(RedisSerializer<String> stringSerializer)
StringRedisSerializer
.stringSerializer
- The stringValueSerializer to set.ValueOperations.get(Object, long, long)
public void setScriptExecutor(ScriptExecutor<K> scriptExecutor)
scriptExecutor
- The ScriptExecutor
to use for executing Redis scriptspublic List<Object> exec()
RedisSerializer
s 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 the RedisConnectionFactory
, the results of exec
will be returned without deserialization. This check is mostly for backwards compatibility with 1.0.exec
in interface RedisOperations<K,V>
public List<Object> exec(RedisSerializer<?> valueSerializer)
RedisOperations
RedisSerializer
to deserialize any results that are byte[]s or
Collections of byte[]s. If a result is a Map, the provided RedisSerializer
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.exec
in interface RedisOperations<K,V>
valueSerializer
- The RedisSerializer
to use for deserializing the results of transaction execpublic void delete(K key)
delete
in interface RedisOperations<K,V>
public void delete(Collection<K> keys)
delete
in interface RedisOperations<K,V>
public Boolean expire(K key, long timeout, TimeUnit unit)
expire
in interface RedisOperations<K,V>
public Boolean expireAt(K key, Date date)
expireAt
in interface RedisOperations<K,V>
public void convertAndSend(String channel, Object message)
convertAndSend
in interface RedisOperations<K,V>
public Long getExpire(K key, TimeUnit timeUnit)
getExpire
in interface RedisOperations<K,V>
public K randomKey()
randomKey
in interface RedisOperations<K,V>
public void rename(K oldKey, K newKey)
rename
in interface RedisOperations<K,V>
public Boolean renameIfAbsent(K oldKey, K newKey)
renameIfAbsent
in interface RedisOperations<K,V>
public byte[] dump(K key)
dump
in interface RedisOperations<K,V>
key
- The key to dumppublic void restore(K key, byte[] value, long timeToLive, TimeUnit unit)
dump(Object)
, since Redis uses a non-standard serialization mechanism.restore
in interface RedisOperations<K,V>
key
- The key to restorevalue
- The value to restore, as returned by dump(Object)
timeToLive
- An expiration for the restored key, or 0 for no expirationunit
- The time unit for timeToLiveRedisSystemException
- if the key you are attempting to restore already exists.public void multi()
RedisOperations
multi
in interface RedisOperations<K,V>
public void discard()
discard
in interface RedisOperations<K,V>
public void watch(K key)
watch
in interface RedisOperations<K,V>
public void watch(Collection<K> keys)
watch
in interface RedisOperations<K,V>
public void unwatch()
unwatch
in interface RedisOperations<K,V>
public <T> List<T> sort(SortQuery<K> query, RedisSerializer<T> resultSerializer)
sort
in interface RedisOperations<K,V>
public <T> List<T> sort(SortQuery<K> query, BulkMapper<T,V> bulkMapper)
sort
in interface RedisOperations<K,V>
public <T,S> List<T> sort(SortQuery<K> query, BulkMapper<T,S> bulkMapper, RedisSerializer<S> resultSerializer)
sort
in interface RedisOperations<K,V>
public Long sort(SortQuery<K> query, K storeKey)
sort
in interface RedisOperations<K,V>
public BoundValueOperations<K,V> boundValueOps(K key)
RedisOperations
boundValueOps
in interface RedisOperations<K,V>
key
- Redis keypublic ValueOperations<K,V> opsForValue()
RedisOperations
opsForValue
in interface RedisOperations<K,V>
public ListOperations<K,V> opsForList()
RedisOperations
opsForList
in interface RedisOperations<K,V>
public BoundListOperations<K,V> boundListOps(K key)
RedisOperations
boundListOps
in interface RedisOperations<K,V>
key
- Redis keypublic BoundSetOperations<K,V> boundSetOps(K key)
RedisOperations
boundSetOps
in interface RedisOperations<K,V>
key
- Redis keypublic SetOperations<K,V> opsForSet()
RedisOperations
opsForSet
in interface RedisOperations<K,V>
public BoundZSetOperations<K,V> boundZSetOps(K key)
RedisOperations
boundZSetOps
in interface RedisOperations<K,V>
key
- Redis keypublic ZSetOperations<K,V> opsForZSet()
RedisOperations
opsForZSet
in interface RedisOperations<K,V>
public HyperLogLogOperations<K,V> opsForHyperLogLog()
opsForHyperLogLog
in interface RedisOperations<K,V>
public <HK,HV> BoundHashOperations<K,HK,HV> boundHashOps(K key)
RedisOperations
boundHashOps
in interface RedisOperations<K,V>
HK
- hash key (or field) typeHV
- hash value typekey
- Redis keypublic <HK,HV> HashOperations<K,HK,HV> opsForHash()
RedisOperations
opsForHash
in interface RedisOperations<K,V>
HK
- hash key (or field) typeHV
- hash value typepublic void killClient(String host, int port)
RedisOperations
client
.killClient
in interface RedisOperations<K,V>
host
- of connection to close.port
- of connection to closepublic List<RedisClientInfo> getClientList()
RedisOperations
getClientList
in interface RedisOperations<K,V>
List
of RedisClientInfo
objects.public void slaveOf(String host, int port)
RedisOperations
slaveOf
in interface RedisOperations<K,V>
public void slaveOfNoOne()
RedisOperations
slaveOfNoOne
in interface RedisOperations<K,V>
public void setEnableTransactionSupport(boolean enableTransactionSupport)
true
RedisTemplate
will use MULTI...EXEC|DISCARD to keep track of operations.enableTransactionSupport
-