Redis.Commands.SortedSet (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis sorted set operations.

This module provides pure functions that build Redis ZSET (sorted set) command lists. Sorted sets associate a floating-point score with each unique member, keeping the collection ordered by score. They are the backbone of leaderboards, priority queues, rate limiters, and time-series indexes in Redis.

Every function returns a plain list of strings (a command). To execute a command, pass the result to Redis.command/2; to batch several commands in a single round trip, use Redis.pipeline/2.

Examples

Adding scored members and querying by rank:

iex> Redis.command(conn, Redis.Commands.SortedSet.zadd("leaderboard", [{100, "alice"}, {200, "bob"}]))
{:ok, 2}

iex> Redis.command(conn, Redis.Commands.SortedSet.zrange("leaderboard", "0", "-1", withscores: true))
{:ok, ["alice", "100", "bob", "200"]}

Range queries by score:

iex> Redis.command(conn, Redis.Commands.SortedSet.zrangebyscore("leaderboard", "150", "+inf", withscores: true))
{:ok, ["bob", "200"]}

Incrementing scores for a leaderboard:

iex> Redis.command(conn, Redis.Commands.SortedSet.zincrby("leaderboard", 50, "alice"))
{:ok, "150"}

Summary

Functions

Builds a ZADD command to add members with scores to the sorted set at key.

Builds a ZINCRBY command to increment the score of member in the sorted set at key by increment.

Builds a ZRANGE command to return members between min and max positions.

Builds a ZRANGEBYSCORE command to return members with scores between min and max (inclusive by default).

Builds a ZRANK command to return the 0-based rank of member in the sorted set at key, ordered from lowest to highest score.

Builds a ZSCORE command to return the score of member in the sorted set at key.

Functions

bzmpop(timeout, numkeys, keys, direction, opts \\ [])

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

bzpopmax(keys, timeout)

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

bzpopmin(keys, timeout)

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

zadd(key, score_members, opts \\ [])

@spec zadd(String.t(), [{float() | integer(), String.t()}], keyword()) :: [String.t()]

Builds a ZADD command to add members with scores to the sorted set at key.

score_members is a list of {score, member} tuples. Supports the following options:

  • :nx - only add new members, never update existing ones
  • :xx - only update existing members, never add new ones
  • :gt - only update when the new score is greater than the current score
  • :lt - only update when the new score is less than the current score

zcard(key)

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

zcount(key, min, max)

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

zdiff(numkeys, keys, opts \\ [])

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

zdiffstore(destination, numkeys, keys)

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

zincrby(key, increment, member)

@spec zincrby(String.t(), float() | integer(), String.t()) :: [String.t()]

Builds a ZINCRBY command to increment the score of member in the sorted set at key by increment.

If the member does not exist it is added with increment as its score. Returns the new score as a string.

zinter(numkeys, keys, opts \\ [])

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

zintercard(numkeys, keys, opts \\ [])

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

zinterstore(destination, numkeys, keys, opts \\ [])

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

zlexcount(key, min, max)

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

zmpop(numkeys, keys, direction, opts \\ [])

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

zmscore(key, members)

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

zpopmax(key, count \\ nil)

@spec zpopmax(String.t(), integer() | nil) :: [String.t()]

zpopmin(key, count \\ nil)

@spec zpopmin(String.t(), integer() | nil) :: [String.t()]

zrandmember(key, opts \\ [])

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

zrange(key, min, max, opts \\ [])

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

Builds a ZRANGE command to return members between min and max positions.

By default the range is by rank (0-based index). Options:

  • :rev - return elements in reverse order (highest to lowest)
  • :withscores - include scores alongside members in the reply
  • :limit - a {offset, count} tuple to paginate results

zrangebylex(key, min, max, opts \\ [])

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

zrangebyscore(key, min, max, opts \\ [])

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

Builds a ZRANGEBYSCORE command to return members with scores between min and max (inclusive by default).

Use "-inf" and "+inf" for open-ended ranges, or prefix a bound with "(" for an exclusive boundary (e.g. "(100"). Options:

  • :withscores - include scores in the reply
  • :limit - a {offset, count} tuple to paginate results

zrangestore(dst, src, min, max, opts \\ [])

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

zrank(key, member)

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

Builds a ZRANK command to return the 0-based rank of member in the sorted set at key, ordered from lowest to highest score.

Returns nil if the member does not exist. See zrevrank/2 for the reverse ordering.

zrem(key, members)

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

zremrangebylex(key, min, max)

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

zremrangebyrank(key, start, stop)

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

zremrangebyscore(key, min, max)

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

zrevrange(key, start, stop, opts \\ [])

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

zrevrangebylex(key, max, min, opts \\ [])

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

zrevrangebyscore(key, max, min, opts \\ [])

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

zrevrank(key, member)

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

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

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

zscore(key, member)

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

Builds a ZSCORE command to return the score of member in the sorted set at key.

Returns nil if the member or key does not exist.

zunion(numkeys, keys, opts \\ [])

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

zunionstore(destination, numkeys, keys, opts \\ [])

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