org.springframework.data.redis.core
Class RedisTemplate<K,V>

java.lang.Object
  extended by org.springframework.data.redis.core.RedisAccessor
      extended by org.springframework.data.redis.core.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:
InitializingBean, RedisOperations<K,V>
Direct Known Subclasses:
StringRedisTemplate

public class RedisTemplate<K,V>
extends RedisAccessor
implements RedisOperations<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 (through 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.

See Also:
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()
           
<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()
           
 List<Object> exec()
           
<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(SessionCallback<T> session)
          Executes a Redis session.
 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)
           
 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 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()
          '
<HK,HV> HashOperations<K,HK,HV>
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
<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 setDefaultSerializer(RedisSerializer<?> serializer)
          Sets the default serializer to use for this template.
 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 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)
           
<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)
           
 
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

RedisTemplate

public RedisTemplate()
Constructs a new RedisTemplate instance.

Method Detail

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class RedisAccessor

execute

public <T> T execute(RedisCallback<T> action)
Description copied from interface: RedisOperations
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.

Specified by:
execute in interface RedisOperations<K,V>
Type Parameters:
T - return type
Parameters:
action - callback object that specifies the Redis action
Returns:
a result object returned by the action or null

execute

public <T> T execute(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 action
exposeConnection - whether to enforce exposure of the native Redis Connection to callback code
Returns:
object returned by the action

execute

public <T> T execute(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 execute
exposeConnection - whether to enforce exposure of the native Redis Connection to callback code
pipeline - whether to pipeline or not the connection for the execution
Returns:
object returned by the action

execute

public <T> T execute(SessionCallback<T> session)
Description copied from interface: RedisOperations
Executes a Redis session. Allows multiple operations to be executed in the same session enabling 'transactional' capabilities through RedisOperations.multi() and RedisOperations.watch(Collection) operations.

Specified by:
execute in interface RedisOperations<K,V>
Type Parameters:
T - return type
Parameters:
session - session callback
Returns:
result object returned by the action or null

createRedisConnectionProxy

protected RedisConnection createRedisConnectionProxy(RedisConnection pm)

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> T postProcessResult(T result,
                                  RedisConnection conn,
                                  boolean existingConnection)

isExposeConnection

public boolean isExposeConnection()
Returns whether to expose the native Redis connection to RedisCallback code, or rather a connection proxy (the default).

Returns:
whether to expose the native Redis connection or not

setExposeConnection

public void setExposeConnection(boolean exposeConnection)
Sets whether to expose the Redis connection to RedisCallback code. Default is "false": a proxy will be returned, suppressing quit and disconnect calls.

Parameters:
exposeConnection -

getDefaultSerializer

public RedisSerializer<?> getDefaultSerializer()
Returns the default serializer used by this template.

Returns:
template default serializer

setDefaultSerializer

public void setDefaultSerializer(RedisSerializer<?> serializer)
Sets the default serializer to use for this template. All serializers (expect the setStringSerializer(RedisSerializer)) are initialized to this value unless explicitly set. Defaults to JdkSerializationRedisSerializer.

Parameters:
serializer - default serializer to use

setKeySerializer

public void setKeySerializer(RedisSerializer<?> serializer)
Sets the key serializer to be used by this template. Defaults to getDefaultSerializer().

Parameters:
serializer - the key serializer to be used by this template.

getKeySerializer

public RedisSerializer<?> getKeySerializer()
Returns the key serializer used by this template.

Specified by:
getKeySerializer in interface RedisOperations<K,V>
Returns:
the key serializer used by this template.

setValueSerializer

public void setValueSerializer(RedisSerializer<?> serializer)
Sets the value serializer to be used by this template. Defaults to getDefaultSerializer().

Parameters:
serializer - the value serializer to be used by this template.

getValueSerializer

public RedisSerializer<?> getValueSerializer()
Returns the value serializer used by this template.

Specified by:
getValueSerializer in interface RedisOperations<K,V>
Returns:
the value serializer used by this template.

getHashKeySerializer

public RedisSerializer<?> getHashKeySerializer()
Returns the hashKeySerializer.

Returns:
Returns the hashKeySerializer

setHashKeySerializer

public void setHashKeySerializer(RedisSerializer<?> hashKeySerializer)
Sets the hash key (or field) serializer to be used by this template. Defaults to getDefaultSerializer().

Parameters:
hashKeySerializer - The hashKeySerializer to set.

getHashValueSerializer

public RedisSerializer<?> getHashValueSerializer()
Returns the hashValueSerializer.

Returns:
Returns the hashValueSerializer

setHashValueSerializer

public void setHashValueSerializer(RedisSerializer<?> hashValueSerializer)
Sets the hash value serializer to be used by this template. Defaults to getDefaultSerializer().

Parameters:
hashValueSerializer - The hashValueSerializer to set.

getStringSerializer

public RedisSerializer<String> getStringSerializer()
Returns the stringSerializer.

Returns:
Returns the stringSerializer

setStringSerializer

public 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). Defaults to StringRedisSerializer.

Parameters:
stringSerializer - The stringValueSerializer to set.
See Also:
ValueOperations.get(Object, long, long)

exec

public List<Object> exec()
Specified by:
exec in interface RedisOperations<K,V>

delete

public void delete(K key)
Specified by:
delete in interface RedisOperations<K,V>

delete

public void delete(Collection<K> keys)
Specified by:
delete in interface RedisOperations<K,V>

hasKey

public Boolean hasKey(K key)
Specified by:
hasKey in interface RedisOperations<K,V>

expire

public Boolean expire(K key,
                      long timeout,
                      TimeUnit unit)
Specified by:
expire in interface RedisOperations<K,V>

expireAt

public Boolean expireAt(K key,
                        Date date)
Specified by:
expireAt in interface RedisOperations<K,V>

convertAndSend

public void convertAndSend(String channel,
                           Object message)
Specified by:
convertAndSend in interface RedisOperations<K,V>

getExpire

public Long getExpire(K key)
Specified by:
getExpire in interface RedisOperations<K,V>

keys

public Set<K> keys(K pattern)
Specified by:
keys in interface RedisOperations<K,V>

persist

public Boolean persist(K key)
Specified by:
persist in interface RedisOperations<K,V>

move

public Boolean move(K key,
                    int dbIndex)
Specified by:
move in interface RedisOperations<K,V>

randomKey

public K randomKey()
Specified by:
randomKey in interface RedisOperations<K,V>

rename

public void rename(K oldKey,
                   K newKey)
Specified by:
rename in interface RedisOperations<K,V>

renameIfAbsent

public Boolean renameIfAbsent(K oldKey,
                              K newKey)
Specified by:
renameIfAbsent in interface RedisOperations<K,V>

type

public DataType type(K key)
Specified by:
type in interface RedisOperations<K,V>

multi

public void multi()
Description copied from interface: RedisOperations
'

Specified by:
multi in interface RedisOperations<K,V>

discard

public void discard()
Specified by:
discard in interface RedisOperations<K,V>

watch

public void watch(K key)
Specified by:
watch in interface RedisOperations<K,V>

watch

public void watch(Collection<K> keys)
Specified by:
watch in interface RedisOperations<K,V>

unwatch

public void unwatch()
Specified by:
unwatch in interface RedisOperations<K,V>

sort

public List<V> sort(SortQuery<K> query)
Specified by:
sort in interface RedisOperations<K,V>

sort

public <T> List<T> sort(SortQuery<K> query,
                        RedisSerializer<T> resultSerializer)
Specified by:
sort in interface RedisOperations<K,V>

sort

public <T> List<T> sort(SortQuery<K> query,
                        BulkMapper<T,V> bulkMapper)
Specified by:
sort in interface RedisOperations<K,V>

sort

public <T,S> List<T> sort(SortQuery<K> query,
                          BulkMapper<T,S> bulkMapper,
                          RedisSerializer<S> resultSerializer)
Specified by:
sort in interface RedisOperations<K,V>

sort

public Long sort(SortQuery<K> query,
                 K storeKey)
Specified by:
sort in interface RedisOperations<K,V>

boundValueOps

public BoundValueOperations<K,V> boundValueOps(K key)
Description copied from interface: RedisOperations
Returns the operations performed on simple values (or Strings in Redis terminology) bound to the given key.

Specified by:
boundValueOps in interface RedisOperations<K,V>
Parameters:
key - Redis key
Returns:
value operations bound to the given key

opsForValue

public ValueOperations<K,V> opsForValue()
Description copied from interface: RedisOperations
Returns the operations performed on simple values (or Strings in Redis terminology).

Specified by:
opsForValue in interface RedisOperations<K,V>
Returns:
value operations

opsForList

public ListOperations<K,V> opsForList()
Description copied from interface: RedisOperations
Returns the operations performed on list values.

Specified by:
opsForList in interface RedisOperations<K,V>
Returns:
list operations

boundListOps

public BoundListOperations<K,V> boundListOps(K key)
Description copied from interface: RedisOperations
Returns the operations performed on list values bound to the given key.

Specified by:
boundListOps in interface RedisOperations<K,V>
Parameters:
key - Redis key
Returns:
list operations bound to the given key

boundSetOps

public BoundSetOperations<K,V> boundSetOps(K key)
Description copied from interface: RedisOperations
Returns the operations performed on set values bound to the given key.

Specified by:
boundSetOps in interface RedisOperations<K,V>
Parameters:
key - Redis key
Returns:
set operations bound to the given key

opsForSet

public SetOperations<K,V> opsForSet()
Description copied from interface: RedisOperations
Returns the operations performed on set values.

Specified by:
opsForSet in interface RedisOperations<K,V>
Returns:
set operations

boundZSetOps

public BoundZSetOperations<K,V> boundZSetOps(K key)
Description copied from interface: RedisOperations
Returns the operations performed on zset values (also known as sorted sets) bound to the given key.

Specified by:
boundZSetOps in interface RedisOperations<K,V>
Parameters:
key - Redis key
Returns:
zset operations bound to the given key.

opsForZSet

public ZSetOperations<K,V> opsForZSet()
Description copied from interface: RedisOperations
Returns the operations performed on zset values (also known as sorted sets).

Specified by:
opsForZSet in interface RedisOperations<K,V>
Returns:
zset operations

boundHashOps

public <HK,HV> BoundHashOperations<K,HK,HV> boundHashOps(K key)
Description copied from interface: RedisOperations
Returns the operations performed on hash values bound to the given key.

Specified by:
boundHashOps in interface RedisOperations<K,V>
Type Parameters:
HK - hash key (or field) type
HV - hash value type
Parameters:
key - Redis key
Returns:
hash operations bound to the given key.

opsForHash

public <HK,HV> HashOperations<K,HK,HV> opsForHash()
Description copied from interface: RedisOperations
Returns the operations performed on hash values.

Specified by:
opsForHash in interface RedisOperations<K,V>
Type Parameters:
HK - hash key (or field) type
HV - hash value type
Returns:
hash operations