|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.data.redis.core.RedisAccessor
org.springframework.data.redis.core.RedisTemplate<K,V>
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>
Helper class that simplifies Redis data access code.
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 (throughJdkSerializationRedisSerializer
). 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
Field Summary |
---|
Fields inherited from class org.springframework.data.redis.core.RedisAccessor |
---|
logger |
Constructor Summary | |
---|---|
RedisTemplate()
Constructs a new RedisTemplate instance. |
Method Summary | ||
---|---|---|
void |
afterPropertiesSet()
|
|
|
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()
|
|
|
execute(RedisCallback<T> action)
Executes the given action within a Redis connection. |
|
|
execute(RedisCallback<T> action,
boolean exposeConnection)
Executes the given action object within a connection, which can be exposed or not. |
|
|
execute(RedisCallback<T> action,
boolean exposeConnection,
boolean pipeline)
Executes the given action object within a connection that can be exposed or not. |
|
|
execute(RedisScript<T> script,
List<K> keys,
Object... args)
Executes the given RedisScript |
|
|
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. |
|
|
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)
|
|
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)
|
|
Boolean |
move(K key,
int dbIndex)
|
|
void |
multi()
' |
|
|
opsForHash()
Returns the operations performed on hash values. |
|
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
|
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 |
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. |
|
List<V> |
sort(SortQuery<K> query)
|
|
|
sort(SortQuery<K> query,
BulkMapper<T,S> bulkMapper,
RedisSerializer<S> resultSerializer)
|
|
|
sort(SortQuery<K> query,
BulkMapper<T,V> bulkMapper)
|
|
Long |
sort(SortQuery<K> query,
K storeKey)
|
|
|
sort(SortQuery<K> query,
RedisSerializer<T> resultSerializer)
|
|
DataType |
type(K key)
|
|
void |
unwatch()
|
|
void |
watch(Collection<K> keys)
|
|
void |
watch(K key)
|
Methods inherited from class org.springframework.data.redis.core.RedisAccessor |
---|
getConnectionFactory, setConnectionFactory |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public RedisTemplate()
RedisTemplate
instance.
Method Detail |
---|
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 action
public <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 code
public <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 execution
public <T> T execute(SessionCallback<T> session)
RedisOperations
RedisOperations.multi()
and RedisOperations.watch(Collection)
operations.
execute
in interface RedisOperations<K,V>
T
- return typesession
- session callback
public List<Object> executePipelined(SessionCallback<?> session)
RedisOperations
executePipelined
in interface RedisOperations<K,V>
session
- Session callback
public List<Object> executePipelined(SessionCallback<?> session, RedisSerializer<?> resultSerializer)
RedisOperations
executePipelined
in interface RedisOperations<K,V>
session
- Session callback
public List<Object> executePipelined(RedisCallback<?> action)
RedisOperations
executePipelined
in interface RedisOperations<K,V>
action
- callback object to execute
public 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 value
public <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 script
RedisScript.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 script
RedisScript.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 exec
protected List<Object> execRaw()
public void delete(K key)
delete
in interface RedisOperations<K,V>
public void delete(Collection<K> keys)
delete
in interface RedisOperations<K,V>
public Boolean hasKey(K key)
hasKey
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)
getExpire
in interface RedisOperations<K,V>
public Long getExpire(K key, TimeUnit timeUnit)
getExpire
in interface RedisOperations<K,V>
public Set<K> keys(K pattern)
keys
in interface RedisOperations<K,V>
public Boolean persist(K key)
persist
in interface RedisOperations<K,V>
public Boolean move(K key, int dbIndex)
move
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 DataType type(K key)
type
in interface RedisOperations<K,V>
public byte[] dump(K key)
dump
in interface RedisOperations<K,V>
key
- The key to dump
public 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 timeToLive
RedisSystemException
- 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 List<V> sort(SortQuery<K> query)
sort
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 key
public 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 key
public BoundSetOperations<K,V> boundSetOps(K key)
RedisOperations
boundSetOps
in interface RedisOperations<K,V>
key
- Redis key
public 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 key
public ZSetOperations<K,V> opsForZSet()
RedisOperations
opsForZSet
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 key
public <HK,HV> HashOperations<K,HK,HV> opsForHash()
RedisOperations
opsForHash
in interface RedisOperations<K,V>
HK
- hash key (or field) typeHV
- hash value type
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |