Redis.Commands.Key (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis key management operations.

Provides pure functions that build command lists for key-level operations such as deleting keys, setting expiration, querying TTL, scanning the keyspace, checking existence, renaming, and copying keys. Each function returns a plain list of strings suitable for passing to Redis.command/2 or Redis.pipeline/2.

These functions contain no connection or networking logic -- they only construct the Redis protocol command as a list.

Examples

Delete one or more keys:

iex> Redis.Commands.Key.del(["session:1", "session:2"])
["DEL", "session:1", "session:2"]

Set a TTL and then read it back:

iex> Redis.Commands.Key.expire("session:1", 300)
["EXPIRE", "session:1", "300"]
iex> Redis.Commands.Key.ttl("session:1")
["TTL", "session:1"]

Scan the keyspace with a pattern and count hint:

iex> Redis.Commands.Key.scan(0, match: "user:*", count: 100)
["SCAN", "0", "MATCH", "user:*", "COUNT", "100"]

Summary

Functions

copy(source, destination, opts \\ [])

@spec copy(String.t(), String.t(), keyword()) :: [String.t()]

del(keys)

@spec del([String.t()]) :: [String.t()]

Builds a DEL command to remove one or more keys.

Returns the command list for deleting the given keys. Redis returns the number of keys that were removed.

Example

iex> Redis.Commands.Key.del(["key1", "key2"])
["DEL", "key1", "key2"]

dump(key)

@spec dump(String.t()) :: [String.t()]

exists(keys)

@spec exists([String.t()]) :: [String.t()]

expire(key, seconds, opts \\ [])

@spec expire(String.t(), integer(), keyword()) :: [String.t()]

Builds an EXPIRE command to set a key's time-to-live in seconds.

Supports the :nx option to set the expiry only when the key has no existing expiry.

Examples

iex> Redis.Commands.Key.expire("session:1", 3600)
["EXPIRE", "session:1", "3600"]

iex> Redis.Commands.Key.expire("session:1", 3600, nx: true)
["EXPIRE", "session:1", "3600", "NX"]

expireat(key, timestamp, opts \\ [])

@spec expireat(String.t(), integer(), keyword()) :: [String.t()]

expiretime(key)

@spec expiretime(String.t()) :: [String.t()]

keys(pattern)

@spec keys(String.t()) :: [String.t()]

migrate(host, port, key, dest_db, timeout, opts \\ [])

@spec migrate(String.t(), integer(), String.t(), integer(), integer(), keyword()) :: [
  String.t()
]

object_encoding(key)

@spec object_encoding(String.t()) :: [String.t()]

object_freq(key)

@spec object_freq(String.t()) :: [String.t()]

object_idletime(key)

@spec object_idletime(String.t()) :: [String.t()]

persist(key)

@spec persist(String.t()) :: [String.t()]

pexpire(key, milliseconds, opts \\ [])

@spec pexpire(String.t(), integer(), keyword()) :: [String.t()]

pexpireat(key, timestamp, opts \\ [])

@spec pexpireat(String.t(), integer(), keyword()) :: [String.t()]

pexpiretime(key)

@spec pexpiretime(String.t()) :: [String.t()]

pttl(key)

@spec pttl(String.t()) :: [String.t()]

randomkey()

@spec randomkey() :: [String.t()]

rename(key, newkey)

@spec rename(String.t(), String.t()) :: [String.t()]

Builds a RENAME command to rename a key.

Example

iex> Redis.Commands.Key.rename("old_key", "new_key")
["RENAME", "old_key", "new_key"]

renamenx(key, newkey)

@spec renamenx(String.t(), String.t()) :: [String.t()]

restore(key, ttl, serialized_value, opts \\ [])

@spec restore(String.t(), integer(), String.t(), keyword()) :: [String.t()]

scan(cursor, opts \\ [])

@spec scan(
  integer(),
  keyword()
) :: [String.t()]

Builds a SCAN command to incrementally iterate over the keyspace.

Accepts options for pattern matching (:match), iteration count hint (:count), and key type filtering (:type).

Examples

iex> Redis.Commands.Key.scan(0)
["SCAN", "0"]

iex> Redis.Commands.Key.scan(0, match: "user:*", count: 50, type: "string")
["SCAN", "0", "MATCH", "user:*", "COUNT", "50", "TYPE", "string"]

sort(key, opts \\ [])

@spec sort(
  String.t(),
  keyword()
) :: [String.t()]

sort_ro(key, opts \\ [])

@spec sort_ro(
  String.t(),
  keyword()
) :: [String.t()]

touch(keys)

@spec touch([String.t()]) :: [String.t()]

ttl(key)

@spec ttl(String.t()) :: [String.t()]

type(key)

@spec type(String.t()) :: [String.t()]

unlink(keys)

@spec unlink([String.t()]) :: [String.t()]

wait(numreplicas, timeout)

@spec wait(integer(), integer()) :: [String.t()]