Redis.Commands.Set (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis set operations.

This module provides pure functions that build Redis SET command lists. Sets are unordered collections of unique strings, useful for membership tracking, tagging, and computing intersections, unions, and differences across collections.

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 members and retrieving a set:

iex> Redis.command(conn, Redis.Commands.Set.sadd("tags", ["elixir", "redis", "otp"]))
{:ok, 3}

iex> Redis.command(conn, Redis.Commands.Set.smembers("tags"))
{:ok, ["elixir", "redis", "otp"]}

Set operations -- intersection and union:

iex> Redis.pipeline(conn, [
...>   Redis.Commands.Set.sadd("set:a", ["1", "2", "3"]),
...>   Redis.Commands.Set.sadd("set:b", ["2", "3", "4"]),
...>   Redis.Commands.Set.sinter(["set:a", "set:b"]),
...>   Redis.Commands.Set.sunion(["set:a", "set:b"])
...> ])
{:ok, [3, 3, ["2", "3"], ["1", "2", "3", "4"]]}

Removing members:

iex> Redis.command(conn, Redis.Commands.Set.srem("tags", ["otp"]))
{:ok, 1}

Summary

Functions

Builds a SADD command to add one or more members to the set at key.

Builds a SDIFF command to return members in the first set that are not in any of the subsequent sets listed in keys.

Builds a SINTER command to return the intersection of all sets in keys.

Builds a SISMEMBER command to test whether member belongs to the set at key.

Builds a SMEMBERS command to return all members of the set at key.

Builds a SREM command to remove one or more members from the set at key.

Builds a SUNION command to return the union of all sets in keys.

Functions

sadd(key, members)

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

Builds a SADD command to add one or more members to the set at key.

Returns the number of members that were added (excluding duplicates).

scard(key)

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

sdiff(keys)

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

Builds a SDIFF command to return members in the first set that are not in any of the subsequent sets listed in keys.

sdiffstore(destination, keys)

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

sinter(keys)

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

Builds a SINTER command to return the intersection of all sets in keys.

Only members present in every listed set are returned.

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

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

sinterstore(destination, keys)

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

sismember(key, member)

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

Builds a SISMEMBER command to test whether member belongs to the set at key.

Returns 1 if the member exists, 0 otherwise.

smembers(key)

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

Builds a SMEMBERS command to return all members of the set at key.

For large sets, consider sscan/3 instead to iterate incrementally.

smismember(key, members)

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

smove(source, destination, member)

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

spop(key, count \\ nil)

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

srandmember(key, count \\ nil)

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

srem(key, members)

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

Builds a SREM command to remove one or more members from the set at key.

Returns the number of members that were actually removed.

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

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

sunion(keys)

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

Builds a SUNION command to return the union of all sets in keys.

sunionstore(destination, keys)

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