Redis.Commands.String (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis string operations.

This module provides pure functions that return Redis command lists for string data type operations, including getting/setting values, atomic counters, and bulk operations. All functions return a list of strings suitable for use with Redis.command/2 or Redis.pipeline/2.

Examples

# SET with expiry and NX (only if key does not exist)
iex> Redis.Commands.String.set("session:abc", "user_1", ex: 3600, nx: true)
["SET", "session:abc", "user_1", "EX", "3600", "NX"]

# GET a value
iex> Redis.Commands.String.get("session:abc")
["GET", "session:abc"]

# Atomic counter increment
iex> Redis.Commands.String.incr("page:views")
["INCR", "page:views"]

# Fetch multiple keys at once
iex> Redis.Commands.String.mget(["key1", "key2", "key3"])
["MGET", "key1", "key2", "key3"]

Summary

Functions

Builds an APPEND command to append value to the string already stored at key. If the key does not exist, it is created with value as its content. Returns the length of the string after the append operation.

Builds a GETDEL command with conditional deletion (Redis 8.0+). Retrieves the value at key and deletes it only if the condition is met.

Builds a DIGEST command to return the hash digest of the value stored at key (Redis 8.0+).

Builds a GET command to retrieve the value stored at key.

Builds a GETEX command to retrieve the value at key and optionally set or clear its expiration.

Builds an INCR command to atomically increment the integer value at key by 1.

Builds an INCRBY command to atomically increment the integer value at key by amount. The amount may be negative to decrement.

Builds an MGET command to retrieve the values of multiple keys in one call.

Builds an MSETEX command to atomically set multiple key-value pairs with a shared expiration (Redis 8.0+).

Builds a SET command to store value at key.

Deprecated: use getrange/3 instead.

Functions

append(key, value)

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

Builds an APPEND command to append value to the string already stored at key. If the key does not exist, it is created with value as its content. Returns the length of the string after the append operation.

decr(key)

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

decrby(key, amount)

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

delex(key, opts \\ [])

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

Builds a GETDEL command with conditional deletion (Redis 8.0+). Retrieves the value at key and deletes it only if the condition is met.

Options

  • :ifeq - delete only if the current value equals the given string
  • :ifne - delete only if the current value does not equal the given string

digest(key)

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

Builds a DIGEST command to return the hash digest of the value stored at key (Redis 8.0+).

get(key)

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

Builds a GET command to retrieve the value stored at key.

Returns nil from Redis when the key does not exist.

getdel(key)

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

getex(key, opts \\ [])

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

Builds a GETEX command to retrieve the value at key and optionally set or clear its expiration.

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 the existing expiry

getrange(key, start, stop)

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

getset(key, value)

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

incr(key)

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

Builds an INCR command to atomically increment the integer value at key by 1.

If the key does not exist, it is initialized to 0 before incrementing.

incrby(key, amount)

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

Builds an INCRBY command to atomically increment the integer value at key by amount. The amount may be negative to decrement.

incrbyfloat(key, amount)

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

lcs(key1, key2, opts \\ [])

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

mget(keys)

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

Builds an MGET command to retrieve the values of multiple keys in one call.

Returns a list of values in the same order as the requested keys. Keys that do not exist produce nil in the corresponding position.

mset(pairs)

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

msetex(kv_pairs, opts \\ [])

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

Builds an MSETEX command to atomically set multiple key-value pairs with a shared expiration (Redis 8.0+).

kv_pairs is a list of {key, 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

msetnx(pairs)

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

psetex(key, milliseconds, value)

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

set(key, value, opts \\ [])

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

Builds a SET command to store value at key.

Options

  • :ex - set expiry in seconds
  • :px - set expiry in milliseconds
  • :nx - only set if the key does not already exist
  • :xx - only set if the key already exists
  • :get - return the old value stored at the key
  • :ifeq - only set if the current value equals the given string (Redis 8.0+)
  • :ifne - only set if the current value does not equal the given string (Redis 8.0+)

setex(key, seconds, value)

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

setnx(key, value)

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

setrange(key, offset, value)

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

strlen(key)

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

substr(key, start, stop)

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

Deprecated: use getrange/3 instead.