Redis.Commands.Hash (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis hash operations.

This module provides pure functions that return Redis command lists for hash data type operations, including setting and retrieving fields, atomic field increments, and hash scanning. All functions return a list of strings suitable for use with Redis.command/2 or Redis.pipeline/2.

Examples

# HSET with multiple fields at once
iex> Redis.Commands.Hash.hset("user:1", [{"name", "Alice"}, {"age", "30"}])
["HSET", "user:1", "name", "Alice", "age", "30"]

# Retrieve all fields and values
iex> Redis.Commands.Hash.hgetall("user:1")
["HGETALL", "user:1"]

# Atomically increment a numeric field
iex> Redis.Commands.Hash.hincrby("user:1", "login_count", 1)
["HINCRBY", "user:1", "login_count", "1"]

Summary

Functions

Builds an HDEL command to remove one or more fields from the hash at key. Returns the number of fields that were removed.

Builds an HEXPIRE command to set a timeout in seconds on one or more hash fields stored at key.

Builds an HEXPIREAT command to set an expiry on one or more hash fields at the given absolute Unix timestamp (seconds).

Builds an HEXPIRETIME command to retrieve the absolute Unix expiration timestamp (seconds) for each of the specified hash fields at key.

Builds an HGET command to retrieve the value of a single field in the hash stored at key. Returns nil when the field or key does not exist.

Builds an HGETALL command to retrieve all fields and values in the hash stored at key. The result from Redis is a flat list alternating between field names and their values.

Builds an HGETDEL command to atomically retrieve and delete one or more fields from the hash at key (Redis 8.0+).

Builds an HGETEX command to retrieve the values of one or more fields in the hash at key and optionally set or clear per-field expiration (Redis 8.0+).

Builds an HINCRBY command to atomically increment the integer value of field in the hash at key by amount. If the field does not exist, it is initialized to 0 before incrementing.

Deprecated: use hset/2 instead.

Builds an HPERSIST command to remove the expiration from one or more hash fields at key, making them persistent.

Builds an HPEXPIRE command to set a timeout in milliseconds on one or more hash fields stored at key.

Builds an HPEXPIREAT command to set an expiry on one or more hash fields at the given absolute Unix timestamp (milliseconds).

Builds an HPEXPIRETIME command to retrieve the absolute Unix expiration timestamp (milliseconds) for each of the specified hash fields at key.

Builds an HPTTL command to retrieve the remaining time-to-live in milliseconds for each of the specified hash fields at key.

Builds an HSCAN command to incrementally iterate over fields in the hash at key. Start with cursor 0 and use the cursor returned by Redis in subsequent calls until it returns 0.

Builds an HSET command to set one or more field-value pairs in the hash stored at key. Accepts a list of {field, value} tuples. Fields that already exist are overwritten.

Builds an HSETEX command to set one or more field-value pairs in the hash at key with a per-field expiration (Redis 8.0+).

Builds an HTTL command to retrieve the remaining time-to-live in seconds for each of the specified hash fields at key.

Functions

hdel(key, fields)

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

Builds an HDEL command to remove one or more fields from the hash at key. Returns the number of fields that were removed.

hexists(key, field)

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

hexpire(key, seconds, fields, opts \\ [])

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

Builds an HEXPIRE command to set a timeout in seconds on one or more hash fields stored at key.

Options

  • :nx - set expiry only when the field has no expiry
  • :xx - set expiry only when the field already has an expiry
  • :gt - set expiry only when the new expiry is greater than the current one
  • :lt - set expiry only when the new expiry is less than the current one

hexpireat(key, timestamp, fields, opts \\ [])

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

Builds an HEXPIREAT command to set an expiry on one or more hash fields at the given absolute Unix timestamp (seconds).

Accepts the same options as hexpire/4.

hexpiretime(key, fields)

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

Builds an HEXPIRETIME command to retrieve the absolute Unix expiration timestamp (seconds) for each of the specified hash fields at key.

hget(key, field)

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

Builds an HGET command to retrieve the value of a single field in the hash stored at key. Returns nil when the field or key does not exist.

hgetall(key)

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

Builds an HGETALL command to retrieve all fields and values in the hash stored at key. The result from Redis is a flat list alternating between field names and their values.

hgetdel(key, fields)

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

Builds an HGETDEL command to atomically retrieve and delete one or more fields from the hash at key (Redis 8.0+).

hgetex(key, fields, opts \\ [])

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

Builds an HGETEX command to retrieve the values of one or more fields in the hash at key and optionally set or clear per-field expiration (Redis 8.0+).

Options

  • :ex - set expiry in seconds
  • :px - set expiry in milliseconds
  • :exat - set expiry as a Unix timestamp in seconds
  • :pxat - set expiry as a Unix timestamp in milliseconds
  • :persist - remove existing expiry from the fields

hincrby(key, field, amount)

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

Builds an HINCRBY command to atomically increment the integer value of field in the hash at key by amount. If the field does not exist, it is initialized to 0 before incrementing.

hincrbyfloat(key, field, amount)

@spec hincrbyfloat(String.t(), String.t(), float()) :: [String.t()]

hkeys(key)

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

hlen(key)

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

hmget(key, fields)

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

hmset(key, pairs)

@spec hmset(String.t(), [{String.t(), String.t()}]) :: [String.t()]

Deprecated: use hset/2 instead.

hpersist(key, fields)

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

Builds an HPERSIST command to remove the expiration from one or more hash fields at key, making them persistent.

hpexpire(key, milliseconds, fields, opts \\ [])

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

Builds an HPEXPIRE command to set a timeout in milliseconds on one or more hash fields stored at key.

Accepts the same options as hexpire/4.

hpexpireat(key, timestamp_ms, fields, opts \\ [])

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

Builds an HPEXPIREAT command to set an expiry on one or more hash fields at the given absolute Unix timestamp (milliseconds).

Accepts the same options as hexpire/4.

hpexpiretime(key, fields)

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

Builds an HPEXPIRETIME command to retrieve the absolute Unix expiration timestamp (milliseconds) for each of the specified hash fields at key.

hpttl(key, fields)

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

Builds an HPTTL command to retrieve the remaining time-to-live in milliseconds for each of the specified hash fields at key.

hrandfield(key, opts \\ [])

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

hscan(key, cursor, opts \\ [])

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

Builds an HSCAN command to incrementally iterate over fields in the hash at key. Start with cursor 0 and use the cursor returned by Redis in subsequent calls until it returns 0.

Options

  • :match - glob-style pattern to filter field names
  • :count - hint for how many elements to return per call

hset(key, pairs)

@spec hset(String.t(), [{String.t(), String.t()}]) :: [String.t()]

Builds an HSET command to set one or more field-value pairs in the hash stored at key. Accepts a list of {field, value} tuples. Fields that already exist are overwritten.

hsetex(key, field_values, opts \\ [])

@spec hsetex(String.t(), [{String.t(), String.t()}], keyword()) :: [String.t()]

Builds an HSETEX command to set one or more field-value pairs in the hash at key with a per-field expiration (Redis 8.0+).

field_values is a list of {field, value} tuples.

Options

  • :ex - set expiry in seconds
  • :px - set expiry in milliseconds
  • :exat - set expiry as a Unix timestamp in seconds
  • :pxat - set expiry as a Unix timestamp in milliseconds

hsetnx(key, field, value)

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

hstrlen(key, field)

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

httl(key, fields)

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

Builds an HTTL command to retrieve the remaining time-to-live in seconds for each of the specified hash fields at key.

hvals(key)

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