Interface RedisKeyCommands

All Known Subinterfaces:
DefaultedRedisClusterConnection, DefaultedRedisConnection, RedisClusterConnection, RedisCommands, RedisConnection, RedisConnectionUtils.RedisConnectionProxy, StringRedisConnection
All Known Implementing Classes:
AbstractRedisConnection, DefaultStringRedisConnection, JedisClusterConnection, JedisConnection, LettuceClusterConnection, LettuceConnection

@NullUnmarked public interface RedisKeyCommands
Key-specific commands supported by Redis.
Author:
Costin Leau, Christoph Strobl, Mark Paluch, ihaohong
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default Boolean
    applyExpiration(byte @NonNull [] key, @NonNull Expiration expiration, @NonNull ExpirationOptions options)
     
    copy(byte @NonNull [] sourceKey, byte @NonNull [] targetKey, boolean replace)
    Copy given sourceKey to targetKey.
    del(byte @NonNull [] @NonNull ... keys)
    Delete given keys.
    byte[]
    dump(byte @NonNull [] key)
    Retrieve serialized version of the value stored at key.
    encodingOf(byte @NonNull [] key)
    Get the type of internal representation used for storing the value at the given key.
    default Boolean
    exists(byte @NonNull [] key)
    Determine if given key exists.
    exists(byte @NonNull [] @NonNull ... keys)
    Count how many of the given keys exist.
    default Boolean
    expire(byte @NonNull [] key, long seconds)
    Set time to live for given key in seconds.
    expire(byte @NonNull [] key, long seconds, @NonNull ExpirationOptions.Condition condition)
    Set time to live for given key in seconds.
    default Boolean
    expire(byte @NonNull [] key, @NonNull Duration duration)
    Set time to live for given key using seconds precision.
    default Boolean
    expireAt(byte @NonNull [] key, long unixTime)
    Set the expiration for given key as a UNIX timestamp.
    expireAt(byte @NonNull [] key, long unixTime, @NonNull ExpirationOptions.Condition condition)
    Set the expiration for given key as a UNIX timestamp.
    default Boolean
    expireAt(byte @NonNull [] key, @NonNull Instant unixTime)
    Set the expiration for given key as a UNIX timestamp in seconds precision.
    idletime(byte @NonNull [] key)
    Get the Duration since the object stored at the given key is idle.
    Set<byte @NonNull []>
    keys(byte @NonNull [] pattern)
    Retrieve all keys matching the given pattern.
    move(byte @NonNull [] key, int dbIndex)
    Move given key to database with index.
    persist(byte @NonNull [] key)
    Remove the expiration from given key.
    default Boolean
    pExpire(byte @NonNull [] key, long millis)
    Set time to live for given key in milliseconds.
    pExpire(byte @NonNull [] key, long millis, @NonNull ExpirationOptions.Condition condition)
    Set time to live for given key in milliseconds.
    default Boolean
    pExpire(byte @NonNull [] key, @NonNull Duration duration)
    Set time to live for given key using milliseconds precision.
    default Boolean
    pExpireAt(byte @NonNull [] key, long unixTimeInMillis)
    Set the expiration for given key as a UNIX timestamp in milliseconds.
    pExpireAt(byte @NonNull [] key, long unixTimeInMillis, @NonNull ExpirationOptions.Condition condition)
    Set the expiration for given key as a UNIX timestamp in milliseconds.
    default Boolean
    pExpireAt(byte @NonNull [] key, @NonNull Instant unixTime)
    Set the expiration for given key as a UNIX timestamp in milliseconds precision.
    pTtl(byte @NonNull [] key)
    Get the precise time to live for key in milliseconds.
    pTtl(byte @NonNull [] key, @NonNull TimeUnit timeUnit)
    Get the precise time to live for key in and convert it to the given TimeUnit.
    byte[]
    Return a random key from the keyspace.
    refcount(byte @NonNull [] key)
    Get the number of references of the value associated with the specified key.
    void
    rename(byte @NonNull [] oldKey, byte @NonNull [] newKey)
    Rename key oldKey to newKey.
    renameNX(byte @NonNull [] oldKey, byte @NonNull [] newKey)
    Rename key oldKey to newKey only if newKey does not exist.
    default void
    restore(byte @NonNull [] key, long ttlInMillis, byte @NonNull [] serializedValue)
    Create key using the serializedValue, previously obtained using dump(byte[]).
    void
    restore(byte @NonNull [] key, long ttlInMillis, byte @NonNull [] serializedValue, boolean replace)
    Create key using the serializedValue, previously obtained using dump(byte[]).
    default Cursor<byte @NonNull []>
    scan(@NonNull KeyScanOptions options)
    Use a Cursor to iterate over keys.
    Cursor<byte @NonNull []>
    scan(@Nullable ScanOptions options)
    Use a Cursor to iterate over keys.
    List<byte @NonNull []>
    sort(byte @NonNull [] key, @Nullable SortParameters params)
    Sort the elements for key.
    sort(byte @NonNull [] key, @Nullable SortParameters params, byte @NonNull [] storeKey)
    Sort the elements for key and store result in storeKey.
    touch(byte @NonNull [] @NonNull ... keys)
    Alter the last access time of given key(s).
    ttl(byte @NonNull [] key)
    Get the time to live for key in seconds.
    ttl(byte @NonNull [] key, @NonNull TimeUnit timeUnit)
    Get the time to live for key in and convert it to the given TimeUnit.
    type(byte @NonNull [] key)
    Determine the type stored at key.
    unlink(byte @NonNull [] @NonNull ... keys)
    Unlink the keys from the keyspace.
  • Method Details

    • copy

      Boolean copy(byte @NonNull [] sourceKey, byte @NonNull [] targetKey, boolean replace)
      Copy given sourceKey to targetKey.
      Parameters:
      sourceKey - must not be null.
      targetKey - must not be null.
      replace - whether to replace existing keys.
      Returns:
      null when used in pipeline / transaction.
      Since:
      2.6
      See Also:
    • exists

      default Boolean exists(byte @NonNull [] 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:
    • exists

      Long exists(byte @NonNull [] @NonNull ... keys)
      Count how many of the given keys exist. Providing the very same key more than once also counts multiple times.
      Parameters:
      keys - must not be null.
      Returns:
      the number of keys existing among the ones specified as arguments. null when used in pipeline / transaction.
      Since:
      2.1
    • del

      Long del(byte @NonNull [] @NonNull ... 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

      Long unlink(byte @NonNull [] @NonNull ... keys)
      Unlink the keys from the keyspace. Unlike with del(byte[]...) the actual memory reclaiming here happens asynchronously.
      Parameters:
      keys - must not be null.
      Returns:
      null when used in pipeline / transaction.
      Since:
      2.1
      See Also:
    • type

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

      Long touch(byte @NonNull [] @NonNull ... keys)
      Alter the last access time of given key(s).
      Parameters:
      keys - must not be null.
      Returns:
      null when used in pipeline / transaction.
      Since:
      2.1
      See Also:
    • keys

      Set<byte @NonNull []> keys(byte @NonNull [] pattern)
      Retrieve all keys matching the given pattern.

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

      Parameters:
      pattern - must not be null.
      Returns:
      empty Set if no match found. null when used in pipeline / transaction.
      See Also:
    • scan

      default Cursor<byte @NonNull []> scan(@NonNull KeyScanOptions options)
      Use a Cursor to iterate over keys.
      Parameters:
      options - must not be null.
      Returns:
      never null.
      Since:
      2.4
      See Also:
    • scan

      Cursor<byte @NonNull []> scan(@Nullable ScanOptions options)
      Use a Cursor to iterate over keys.
      Parameters:
      options - can be null.
      Returns:
      never null.
      Since:
      1.4
      See Also:
    • randomKey

      byte[] randomKey()
      Return a random key from the keyspace.
      Returns:
      null if no keys available or when used in pipeline or transaction.
      See Also:
    • rename

      void rename(byte @NonNull [] oldKey, byte @NonNull [] newKey)
      Rename key oldKey to newKey.
      Parameters:
      oldKey - must not be null.
      newKey - must not be null.
      See Also:
    • renameNX

      Boolean renameNX(byte @NonNull [] oldKey, byte @NonNull [] 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:
    • applyExpiration

      default Boolean applyExpiration(byte @NonNull [] key, @NonNull Expiration expiration, @NonNull ExpirationOptions options)
      Parameters:
      key - must not be null.
      expiration - the Expiration to apply.
      options - additional options to be sent along with the command.
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • expire

      default Boolean expire(byte @NonNull [] key, long seconds)
      Set time to live for given key in seconds.
      Parameters:
      key - must not be null.
      seconds -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      See Also:
    • expire

      Boolean expire(byte @NonNull [] key, long seconds, @NonNull ExpirationOptions.Condition condition)
      Set time to live for given key in seconds.
      Parameters:
      key - must not be null.
      seconds -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • expire

      default Boolean expire(byte @NonNull [] key, @NonNull Duration duration)
      Set time to live for given key using seconds precision.
      Parameters:
      key - must not be null.
      duration -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • pExpire

      default Boolean pExpire(byte @NonNull [] key, long millis)
      Set time to live for given key in milliseconds.
      Parameters:
      key - must not be null.
      millis -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      See Also:
    • pExpire

      Boolean pExpire(byte @NonNull [] key, long millis, @NonNull ExpirationOptions.Condition condition)
      Set time to live for given key in milliseconds.
      Parameters:
      key - must not be null.
      millis -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • pExpire

      default Boolean pExpire(byte @NonNull [] key, @NonNull Duration duration)
      Set time to live for given key using milliseconds precision.
      Parameters:
      key - must not be null.
      duration -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • expireAt

      default Boolean expireAt(byte @NonNull [] key, long unixTime)
      Set the expiration for given key as a UNIX timestamp.
      Parameters:
      key - must not be null.
      unixTime -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      See Also:
    • expireAt

      Boolean expireAt(byte @NonNull [] key, long unixTime, @NonNull ExpirationOptions.Condition condition)
      Set the expiration for given key as a UNIX timestamp.
      Parameters:
      key - must not be null.
      unixTime -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • expireAt

      default Boolean expireAt(byte @NonNull [] key, @NonNull Instant unixTime)
      Set the expiration for given key as a UNIX timestamp in seconds precision.
      Parameters:
      key - must not be null.
      unixTime -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • pExpireAt

      default Boolean pExpireAt(byte @NonNull [] key, long unixTimeInMillis)
      Set the expiration for given key as a UNIX timestamp in milliseconds.
      Parameters:
      key - must not be null.
      unixTimeInMillis -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      See Also:
    • pExpireAt

      Boolean pExpireAt(byte @NonNull [] key, long unixTimeInMillis, @NonNull ExpirationOptions.Condition condition)
      Set the expiration for given key as a UNIX timestamp in milliseconds.
      Parameters:
      key - must not be null.
      unixTimeInMillis -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • pExpireAt

      default Boolean pExpireAt(byte @NonNull [] key, @NonNull Instant unixTime)
      Set the expiration for given key as a UNIX timestamp in milliseconds precision.
      Parameters:
      key - must not be null.
      unixTime -
      Returns:
      null when used in pipeline / transaction. true if the timeout was set or false if the timeout was not set; for example, the key doesn't exist, or the operation was skipped because of the provided arguments.
      Since:
      3.5
      See Also:
    • persist

      Boolean persist(byte @NonNull [] key)
      Remove the expiration from given key.
      Parameters:
      key - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • move

      Boolean move(byte @NonNull [] 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:
    • ttl

      Long ttl(byte @NonNull [] 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:
    • ttl

      Long ttl(byte @NonNull [] 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
      See Also:
    • pTtl

      Long pTtl(byte @NonNull [] key)
      Get the precise time to live for key in milliseconds.
      Parameters:
      key - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • pTtl

      Long pTtl(byte @NonNull [] key, @NonNull TimeUnit timeUnit)
      Get the precise 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
      See Also:
    • sort

      List<byte @NonNull []> sort(byte @NonNull [] key, @Nullable SortParameters params)
      Sort the elements for key.
      Parameters:
      key - must not be null.
      params - can be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • sort

      Long sort(byte @NonNull [] key, @Nullable SortParameters params, byte @NonNull [] storeKey)
      Sort the elements for key and store result in storeKey.
      Parameters:
      key - must not be null.
      params - can be null.
      storeKey - must not be null.
      Returns:
      null when used in pipeline / transaction.
      See Also:
    • dump

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

      default void restore(byte @NonNull [] key, long ttlInMillis, byte @NonNull [] serializedValue)
      Create key using the serializedValue, previously obtained using dump(byte[]).
      Parameters:
      key - must not be null.
      ttlInMillis -
      serializedValue - must not be null.
      See Also:
    • restore

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

      ValueEncoding encodingOf(byte @NonNull [] key)
      Get the type of internal representation used for storing the value at the given key.
      Parameters:
      key - must not be null.
      Returns:
      ValueEncoding.RedisValueEncoding.VACANT if key does not exist or null when used in pipeline / transaction.
      Throws:
      IllegalArgumentException - if key is null.
      Since:
      2.1
      See Also:
    • idletime

      Duration idletime(byte @NonNull [] key)
      Get the Duration since the object stored at the given key is idle.
      Parameters:
      key - must not be null.
      Returns:
      null if key does not exist or when used in pipeline / transaction.
      Throws:
      IllegalArgumentException - if key is null.
      Since:
      2.1
      See Also:
    • refcount

      Long refcount(byte @NonNull [] key)
      Get the number of references of the value associated with the specified key.
      Parameters:
      key - must not be null.
      Returns:
      null if key does not exist or when used in pipeline / transaction.
      Throws:
      IllegalArgumentException - if key is null.
      Since:
      2.1
      See Also: