Interface RedisOperations<K,V>

All Known Implementing Classes:
RedisTemplate, StringRedisTemplate

@NullUnmarked 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).

Redis command methods are exempted from the default non-nullable return value assumption as nullness depends not only on the command but also on the connection state. Methods invoked during a transaction or while pipelining are required to return null at the time invoking a command as the response is not available until the transaction is executed or the pipeline is closed. To avoid excessive null checks in calling code and to not express a faulty assumption of non-nullness, all command interfaces are annotated with @NullUnmarked.

Author:
Costin Leau, Christoph Strobl, Ninad Divadkar, Mark Paluch, ihaohong, Todd Merrill, Chen Li, Vedran Pavic, Marcin Grzejszczak
  • Method Details

    • execute

      <T extends @Nullable Object> T execute(@NonNull 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:
      result of the given RedisCallback.doInRedis(RedisConnection) invocation.
    • execute

      <T extends @Nullable Object> T execute(@NonNull 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 of the given SessionCallback.execute(RedisOperations) invocation.
    • executePipelined

      @NonNull List<Object> executePipelined(@NonNull 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:
      pipeline results of the given RedisCallback.doInRedis(RedisConnection) invocation. Results are collected from RedisConnection calls, RedisCallback.doInRedis(RedisConnection) itself must return null.
    • executePipelined

      @NonNull List<Object> executePipelined(@NonNull RedisCallback<?> action, @NonNull 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:
      pipeline results of the given RedisCallback.doInRedis(RedisConnection) invocation. Results are collected from RedisConnection calls, RedisCallback.doInRedis(RedisConnection) itself must return null.
    • executePipelined

      @NonNull List<Object> executePipelined(@NonNull 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:
      pipeline results of the given SessionCallback.execute(RedisOperations) invocation. Results are collected from RedisOperations calls, SessionCallback.execute(RedisOperations) itself must return null.
    • executePipelined

      @NonNull List<Object> executePipelined(@NonNull SessionCallback<?> session, @NonNull 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:
      pipeline results of the given SessionCallback.execute(RedisOperations) invocation. Results are collected from RedisOperations calls, SessionCallback.execute(RedisOperations) itself must return null.
    • execute

      <T extends @Nullable Object> T execute(@NonNull RedisScript<T> script, @NonNull List<@NonNull K> keys, @NonNull Object @NonNull ... 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

      <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)
      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

      <T extends Closeable> T executeWithStickyConnection(@NonNull 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:
      the result of the operation performed in the callback or null.
      Since:
      1.8
    • copy

      Boolean copy(@NonNull K sourceKey, @NonNull 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:
      true when copied successfully or null when used in pipeline / transaction.
      Since:
      2.6
      See Also:
    • hasKey

      Boolean hasKey(@NonNull K key)
      Determine if given key exists.
      Parameters:
      key - must not be null.
      Returns:
      true if key exists. null when used in pipeline / transaction.
      See Also:
    • countExistingKeys

      Long countExistingKeys(@NonNull Collection<@NonNull 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

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

      Long delete(@NonNull Collection<@NonNull 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

      Boolean unlink(@NonNull 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

      Long unlink(@NonNull Collection<@NonNull 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

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

      Set<@NonNull K> keys(@NonNull K pattern)
      Retrieve all keys matching the given pattern via KEYS command.

      IMPORTANT: This command is non-interruptible and scans the entire keyspace which may cause performance issues. Consider scan(ScanOptions) for large datasets.

      Parameters:
      pattern - key pattern
      Returns:
      set of matching keys, or null when used in pipeline / transaction
      See Also:
    • scan

      @NonNull Cursor<@NonNull K> scan(@NonNull 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

      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(@NonNull K oldKey, @NonNull K newKey)
      Rename key oldKey to newKey.
      Parameters:
      oldKey - must not be null.
      newKey - must not be null.
      See Also:
    • renameIfAbsent

      Boolean renameIfAbsent(@NonNull K oldKey, @NonNull 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

      Boolean expire(@NonNull K key, long timeout, @NonNull 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

      default Boolean expire(@NonNull K key, @NonNull 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

      Boolean expireAt(@NonNull K key, @NonNull 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

      default Boolean expireAt(@NonNull K key, @NonNull 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
    • expire

      ExpireChanges.ExpiryChangeState expire(@NonNull K key, @NonNull Expiration expiration, @NonNull ExpirationOptions options)
      Set the expiration for given key.
      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.
      Throws:
      IllegalArgumentException - any of the required arguments is null.
      Since:
      3.5
      See Also:
    • expiration

      default @NonNull BoundKeyExpirationOperations expiration(@NonNull K key)
      Returns a bound operations object to perform expiration operations on the bound key.
      Returns:
      the bound operations object to perform operations on the hash field expiration.
      Since:
      3.5
    • persist

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

      Long getExpire(@NonNull 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

      Long getExpire(@NonNull K key, @NonNull 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

      Boolean move(@NonNull 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

      byte[] dump(@NonNull 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(@NonNull K key, byte @NonNull [] value, long timeToLive, @NonNull 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(@NonNull K key, byte @NonNull [] value, long timeToLive, @NonNull 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

      List<V> sort(@NonNull SortQuery<@NonNull 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

      <T> List<T> sort(@NonNull SortQuery<@NonNull K> query, @NonNull 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

      <T> List<T> sort(@NonNull SortQuery<@NonNull K> query, @NonNull 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

      <T, S> List<T> sort(SortQuery<@NonNull K> query, @NonNull BulkMapper<T,S> bulkMapper, @NonNull 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

      Long sort(@NonNull SortQuery<@NonNull K> query, @NonNull 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(@NonNull K key)
      Watch given key for modifications during transaction started with multi().
      Parameters:
      key - must not be null.
      See Also:
    • watch

      void watch(@NonNull Collection<@NonNull 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

      @NonNull 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

      @NonNull List<Object> exec(@NonNull 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

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

      void killClient(@NonNull 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(@NonNull 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

      Long convertAndSend(@NonNull String destination, @NonNull 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

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

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

      @NonNull BoundGeoOperations<K,V> boundGeoOps(@NonNull 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> @NonNull 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> @NonNull BoundHashOperations<K,HK,HV> boundHashOps(@NonNull 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

      @NonNull HyperLogLogOperations<K,V> opsForHyperLogLog()
      Returns:
      never null.
      Since:
      1.5
    • opsForList

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

      @NonNull 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

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

      @NonNull BoundSetOperations<K,V> boundSetOps(@NonNull 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> @NonNull StreamOperations<K,HK,HV> opsForStream()
      Returns the operations performed on Streams.
      Returns:
      stream operations.
      Since:
      2.2
    • opsForStream

      <HK, HV> @NonNull StreamOperations<K,HK,HV> opsForStream(@NonNull 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> @NonNull BoundStreamOperations<K,HK,HV> boundStreamOps(@NonNull K key)
      Returns the operations performed on Streams bound to the given key.
      Returns:
      stream operations.
      Since:
      2.2
    • opsForValue

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

      @NonNull BoundValueOperations<K,V> boundValueOps(@NonNull 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

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

      @NonNull BoundZSetOperations<K,V> boundZSetOps(@NonNull 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

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

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

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

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