Interface RedisOperations<K,V>

All Known Implementing Classes:
RedisTemplate, StringRedisTemplate

public interface RedisOperations<K,V>
Interface that specified a basic set of Redis operations, implemented by RedisTemplate. Not often used but a useful option for extensibility and testability (as it can be easily mocked or stubbed).
Author:
Costin Leau, Christoph Strobl, Ninad Divadkar, Mark Paluch, ihaohong, Todd Merrill, Chen Li, Vedran Pavic
  • Method Details

    • execute

      @Nullable <T> T execute(RedisCallback<T> action)
      Executes the given action within a Redis connection. Application exceptions thrown by the action object get propagated to the caller (can only be unchecked) whenever possible. Redis exceptions are transformed into appropriate DAO ones. Allows for returning a result object, that is a domain object or a collection of domain objects. Performs automatic serialization/deserialization for the given objects to and from binary data suitable for the Redis storage. Note: Callback code is not supposed to handle transactions itself! Use an appropriate transaction manager. Generally, callback code must not touch any Connection lifecycle methods, like close, to let the template do its work.
      Type Parameters:
      T - return type
      Parameters:
      action - callback object that specifies the Redis action. Must not be null.
      Returns:
      a result object returned by the action or null
    • execute

      @Nullable <T> T execute(SessionCallback<T> session)
      Executes a Redis session. Allows multiple operations to be executed in the same session enabling 'transactional' capabilities through multi() and watch(Collection) operations.
      Type Parameters:
      T - return type
      Parameters:
      session - session callback. Must not be null.
      Returns:
      result object returned by the action or null
    • executePipelined

      List<Object> executePipelined(RedisCallback<?> action)
      Executes the given action object on a pipelined connection, returning the results. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline. This method will use the default serializers to deserialize results
      Parameters:
      action - callback object to execute
      Returns:
      list of objects returned by the pipeline
    • executePipelined

      List<Object> executePipelined(RedisCallback<?> action, RedisSerializer<?> resultSerializer)
      Executes the given action object on a pipelined connection, returning the results using a dedicated serializer. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline.
      Parameters:
      action - callback object to execute
      resultSerializer - The Serializer to use for individual values or Collections of values. If any returned values are hashes, this serializer will be used to deserialize both the key and value
      Returns:
      list of objects returned by the pipeline
    • executePipelined

      List<Object> executePipelined(SessionCallback<?> session)
      Executes the given Redis session on a pipelined connection. Allows transactions to be pipelined. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline.
      Parameters:
      session - Session callback
      Returns:
      list of objects returned by the pipeline
    • executePipelined

      List<Object> executePipelined(SessionCallback<?> session, RedisSerializer<?> resultSerializer)
      Executes the given Redis session on a pipelined connection, returning the results using a dedicated serializer. Allows transactions to be pipelined. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline.
      Parameters:
      session - Session callback
      resultSerializer -
      Returns:
      list of objects returned by the pipeline
    • execute

      @Nullable <T> T execute(RedisScript<T> script, List<K> keys, Object... args)
      Executes the given RedisScript
      Parameters:
      script - The script to execute
      keys - Any keys that need to be passed to the script
      args - Any args that need to be passed to the script
      Returns:
      The return value of the script or null if RedisScript.getResultType() is null, likely indicating a throw-away status reply (i.e. "OK")
    • execute

      @Nullable <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSerializer<T> resultSerializer, List<K> keys, Object... args)
      Executes the given RedisScript, using the provided RedisSerializers to serialize the script arguments and result.
      Parameters:
      script - The script to execute
      argsSerializer - The RedisSerializer to use for serializing args
      resultSerializer - The RedisSerializer to use for serializing the script return value
      keys - Any keys that need to be passed to the script
      args - 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

      @Nullable <T extends Closeable> T executeWithStickyConnection(RedisCallback<T> callback)
      Allocates and binds a new RedisConnection to the actual return type of the method. It is up to the caller to free resources after use.
      Parameters:
      callback - must not be null.
      Returns:
      Since:
      1.8
    • copy

      @Nullable Boolean copy(K sourceKey, K targetKey, boolean replace)
      Copy given sourceKey to targetKey.
      Parameters:
      sourceKey - must not be null.
      targetKey - must not be null.
      replace - whether the key was copied. null when used in pipeline / transaction.
      Returns:
      Since:
      2.6
      See Also:
    • hasKey

      @Nullable Boolean hasKey(K key)
      Determine if given key exists.
      Parameters:
      key - must not be null.
      Returns:
      See Also:
    • countExistingKeys

      @Nullable Long countExistingKeys(Collection<K> keys)
      Count the number of keys that exist.
      Parameters:
      keys - must not be null.
      Returns:
      The number of keys existing among the ones specified as arguments. Keys mentioned multiple times and existing are counted multiple times.
      Since:
      2.1
      See Also:
    • delete

      @Nullable Boolean delete(K key)
      Delete given key.
      Parameters:
      key - must not be null.
      Returns:
      true if the key was removed.
      See Also:
    • delete

      @Nullable Long delete(Collection<K> keys)
      Delete given keys.
      Parameters:
      keys - must not be null.
      Returns:
      The number of keys that were removed. null when used in pipeline / transaction.
      See Also:
    • unlink

      @Nullable Boolean unlink(K key)
      Unlink the key from the keyspace. Unlike with delete(Object) the actual memory reclaiming here happens asynchronously.
      Parameters:
      key - must not be null.
      Returns:
      The number of keys that were removed. null when used in pipeline / transaction.
      Since:
      2.1
      See Also:
    • unlink

      @Nullable Long unlink(Collection<K> keys)
      Unlink the keys from the keyspace. Unlike with delete(Collection) the actual memory reclaiming here happens asynchronously.
      Parameters:
      keys - must not be null.
      Returns:
      The number of keys that were removed. null when used in pipeline / transaction.
      Since:
      2.1
      See Also:
    • type

      @Nullable DataType type(K key)
      Determine the type stored at key.
      Parameters:
      key - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • keys

      @Nullable Set<K> keys(K pattern)
      Find all keys matching the given pattern.
      Parameters:
      pattern - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • scan

      Cursor<K> scan(ScanOptions options)
      Use a Cursor to iterate over keys.
      Important: Call CloseableIterator.close() when done to avoid resource leaks.
      Parameters:
      options - must not be null.
      Returns:
      the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a try-with-resources clause).
      Since:
      2.7
      See Also:
    • randomKey

      @Nullable K randomKey()
      Return a random key from the keyspace.
      Returns:
      null no keys exist or when used in pipeline / transaction.
      See Also:
    • rename

      void rename(K oldKey, K newKey)
      Rename key oldKey to newKey.
      Parameters:
      oldKey - must not be null.
      newKey - must not be null.
      See Also:
    • renameIfAbsent

      @Nullable Boolean renameIfAbsent(K oldKey, K newKey)
      Rename key oldKey to newKey only if newKey does not exist.
      Parameters:
      oldKey - must not be null.
      newKey - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • expire

      @Nullable Boolean expire(K key, long timeout, TimeUnit unit)
      Set time to live for given key.
      Parameters:
      key - must not be null.
      timeout -
      unit - must not be null.
      Returns:
      null when used in pipeline / transaction.
    • expire

      @Nullable default Boolean expire(K key, Duration timeout)
      Set time to live for given key.
      Parameters:
      key - must not be null.
      timeout - must not be null.
      Returns:
      null when used in pipeline / transaction.
      Throws:
      IllegalArgumentException - if the timeout is null.
      Since:
      2.3
    • expireAt

      @Nullable Boolean expireAt(K key, Date date)
      Set the expiration for given key as a date timestamp.
      Parameters:
      key - must not be null.
      date - must not be null.
      Returns:
      null when used in pipeline / transaction.
    • expireAt

      @Nullable default Boolean expireAt(K key, Instant expireAt)
      Set the expiration for given key as a date timestamp.
      Parameters:
      key - must not be null.
      expireAt - must not be null.
      Returns:
      null when used in pipeline / transaction.
      Throws:
      IllegalArgumentException - if the instant is null or too large to represent as a Date.
      Since:
      2.3
    • persist

      @Nullable Boolean persist(K key)
      Remove the expiration from given key.
      Parameters:
      key - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • getExpire

      @Nullable Long getExpire(K key)
      Get the time to live for key in seconds.
      Parameters:
      key - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • getExpire

      @Nullable Long getExpire(K key, TimeUnit timeUnit)
      Get the time to live for key in and convert it to the given TimeUnit.
      Parameters:
      key - must not be null.
      timeUnit - must not be null.
      Returns:
      null when used in pipeline / transaction.
      Since:
      1.8
    • move

      @Nullable Boolean move(K key, int dbIndex)
      Move given key to database with index.
      Parameters:
      key - must not be null.
      dbIndex -
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • dump

      @Nullable byte[] dump(K key)
      Retrieve serialized version of the value stored at key.
      Parameters:
      key - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • restore

      default void restore(K key, byte[] value, long timeToLive, TimeUnit unit)
      Create key using the serializedValue, previously obtained using dump(Object).
      Parameters:
      key - must not be null.
      value - must not be null.
      timeToLive -
      unit - must not be null.
      See Also:
    • restore

      void restore(K key, byte[] value, long timeToLive, TimeUnit unit, boolean replace)
      Create key using the serializedValue, previously obtained using dump(Object).
      Parameters:
      key - must not be null.
      value - must not be null.
      timeToLive -
      unit - must not be null.
      replace - use true to replace a potentially existing value instead of erroring.
      Since:
      2.1
      See Also:
    • sort

      @Nullable List<V> sort(SortQuery<K> query)
      Sort the elements for query.
      Parameters:
      query - must not be null.
      Returns:
      the results of sort. null when used in pipeline / transaction.
      See Also:
    • sort

      @Nullable <T> List<T> sort(SortQuery<K> query, RedisSerializer<T> resultSerializer)
      Sort the elements for query applying RedisSerializer.
      Parameters:
      query - must not be null.
      Returns:
      the deserialized results of sort. null when used in pipeline / transaction.
      See Also:
    • sort

      @Nullable <T> List<T> sort(SortQuery<K> query, BulkMapper<T,V> bulkMapper)
      Sort the elements for query applying BulkMapper.
      Parameters:
      query - must not be null.
      Returns:
      the deserialized results of sort. null when used in pipeline / transaction.
      See Also:
    • sort

      @Nullable <T, S> List<T> sort(SortQuery<K> query, BulkMapper<T,S> bulkMapper, RedisSerializer<S> resultSerializer)
      Sort the elements for query applying BulkMapper and RedisSerializer.
      Parameters:
      query - must not be null.
      Returns:
      the deserialized results of sort. null when used in pipeline / transaction.
      See Also:
    • sort

      @Nullable Long sort(SortQuery<K> query, K storeKey)
      Sort the elements for query and store result in storeKey.
      Parameters:
      query - must not be null.
      storeKey - must not be null.
      Returns:
      number of values. null when used in pipeline / transaction.
      See Also:
    • watch

      void watch(K key)
      Watch given key for modifications during transaction started with multi().
      Parameters:
      key - must not be null.
      See Also:
    • watch

      void watch(Collection<K> keys)
      Watch given keys for modifications during transaction started with multi().
      Parameters:
      keys - must not be null.
      See Also:
    • unwatch

      void unwatch()
      Flushes all the previously watch(Object) keys.
      See Also:
    • multi

      void multi()
      Mark the start of a transaction block.
      Commands will be queued and can then be executed by calling exec() or rolled back using discard()
      See Also:
    • discard

      void discard()
      Discard all commands issued after multi().
      See Also:
    • exec

      List<Object> exec()
      Executes all queued commands in a transaction started with multi().
      If used along with watch(Object) the operation will fail if any of watched keys has been modified.
      Returns:
      List of replies for each executed command.
      See Also:
    • exec

      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. 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.
      Parameters:
      valueSerializer - The RedisSerializer to use for deserializing the results of transaction exec
      Returns:
      The deserialized results of transaction exec
    • getClientList

      @Nullable List<RedisClientInfo> getClientList()
      Request information and statistics about connected clients.
      Returns:
      List of RedisClientInfo objects.
      Since:
      1.3
    • killClient

      void killClient(String host, int port)
      Closes a given client connection identified by ip:port given in client.
      Parameters:
      host - of connection to close.
      port - of connection to close
      Since:
      1.3
    • replicaOf

      void replicaOf(String host, int port)
      Change redis replication setting to new master.
      Parameters:
      host - must not be null.
      port -
      Since:
      1.3
      See Also:
    • replicaOfNoOne

      void replicaOfNoOne()
      Change server into master.
      Since:
      1.3
      See Also:
    • convertAndSend

      @Nullable Long convertAndSend(String destination, Object message)
      Publishes the given message to the given channel.
      Parameters:
      destination - the channel to publish to, must not be null.
      message - message to publish.
      Returns:
      the number of clients that received the message. null when used in pipeline / transaction.
      See Also:
    • opsForCluster

      ClusterOperations<K,V> opsForCluster()
      Returns the cluster specific operations interface.
      Returns:
      never null.
      Since:
      1.7
    • opsForGeo

      GeoOperations<K,V> opsForGeo()
      Returns geospatial specific operations interface.
      Returns:
      never null.
      Since:
      1.8
    • boundGeoOps

      BoundGeoOperations<K,V> boundGeoOps(K key)
      Returns geospatial specific operations interface bound to the given key.
      Parameters:
      key - must not be null.
      Returns:
      never null.
      Since:
      1.8
    • opsForHash

      <HK, HV> HashOperations<K,HK,HV> opsForHash()
      Returns the operations performed on hash values.
      Type Parameters:
      HK - hash key (or field) type
      HV - hash value type
      Returns:
      hash operations
    • boundHashOps

      <HK, HV> BoundHashOperations<K,HK,HV> boundHashOps(K key)
      Returns the operations performed on hash values bound to the given key.
      Type Parameters:
      HK - hash key (or field) type
      HV - hash value type
      Parameters:
      key - Redis key
      Returns:
      hash operations bound to the given key.
    • opsForHyperLogLog

      HyperLogLogOperations<K,V> opsForHyperLogLog()
      Returns:
      Since:
      1.5
    • opsForList

      ListOperations<K,V> opsForList()
      Returns the operations performed on list values.
      Returns:
      list operations
    • boundListOps

      BoundListOperations<K,V> boundListOps(K key)
      Returns the operations performed on list values bound to the given key.
      Parameters:
      key - Redis key
      Returns:
      list operations bound to the given key
    • opsForSet

      SetOperations<K,V> opsForSet()
      Returns the operations performed on set values.
      Returns:
      set operations
    • boundSetOps

      BoundSetOperations<K,V> boundSetOps(K key)
      Returns the operations performed on set values bound to the given key.
      Parameters:
      key - Redis key
      Returns:
      set operations bound to the given key
    • opsForStream

      <HK, HV> StreamOperations<K,HK,HV> opsForStream()
      Returns the operations performed on Streams.
      Returns:
      stream operations.
      Since:
      2.2
    • opsForStream

      <HK, HV> StreamOperations<K,HK,HV> opsForStream(HashMapper<? super K,? super HK,? super HV> hashMapper)
      Returns the operations performed on Streams.
      Parameters:
      hashMapper - the HashMapper to use when converting ObjectRecord.
      Returns:
      stream operations.
      Since:
      2.2
    • boundStreamOps

      <HK, HV> BoundStreamOperations<K,HK,HV> boundStreamOps(K key)
      Returns the operations performed on Streams bound to the given key.
      Returns:
      stream operations.
      Since:
      2.2
    • opsForValue

      ValueOperations<K,V> opsForValue()
      Returns the operations performed on simple values (or Strings in Redis terminology).
      Returns:
      value operations
    • boundValueOps

      BoundValueOperations<K,V> boundValueOps(K key)
      Returns the operations performed on simple values (or Strings in Redis terminology) bound to the given key.
      Parameters:
      key - Redis key
      Returns:
      value operations bound to the given key
    • opsForZSet

      ZSetOperations<K,V> opsForZSet()
      Returns the operations performed on zset values (also known as sorted sets).
      Returns:
      zset operations
    • boundZSetOps

      BoundZSetOperations<K,V> boundZSetOps(K key)
      Returns the operations performed on zset values (also known as sorted sets) bound to the given key.
      Parameters:
      key - Redis key
      Returns:
      zset operations bound to the given key.
    • getKeySerializer

      RedisSerializer<?> getKeySerializer()
      Returns:
      the key RedisSerializer.
    • getValueSerializer

      RedisSerializer<?> getValueSerializer()
      Returns:
      the value RedisSerializer.
    • getHashKeySerializer

      RedisSerializer<?> getHashKeySerializer()
      Returns:
      the hash key RedisSerializer.
    • getHashValueSerializer

      RedisSerializer<?> getHashValueSerializer()
      Returns:
      the hash value RedisSerializer.