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
Builds a DEL command to remove one or more keys.
Builds an EXPIRE command to set a key's time-to-live in seconds.
Builds a RENAME command to rename a key.
Builds a SCAN command to incrementally iterate over the keyspace.
Functions
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"]
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"]
@spec randomkey() :: [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"]
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"]