Sets
Sets are unordered collections of unique strings. They support O(1) membership checks and set-theoretic operations like union, intersection, and difference.
SADD / SREM / SMEMBERS
Add members to a set, remove members, or retrieve all members. SADD ignores members that already exist.
SADD key member [member ...]
SREM key member [member ...]
SMEMBERS key> SADD tags:post:1 "rust" "database" "oss"
(integer) 3
> SADD tags:post:1 "rust"
(integer) 0
> SREM tags:post:1 "oss"
(integer) 1
> SMEMBERS tags:post:1
1) "rust"
2) "database"SISMEMBER / SMISMEMBER / SCARD
Check if one or more members exist in a set, or get the total number of members.
> SISMEMBER tags:post:1 "rust"
(integer) 1
> SISMEMBER tags:post:1 "python"
(integer) 0
> SMISMEMBER tags:post:1 "rust" "python" "database"
1) (integer) 1
2) (integer) 0
3) (integer) 1
> SCARD tags:post:1
(integer) 2SUNION / SINTER / SDIFF
Compute the union, intersection, or difference of multiple sets. Results are returned without modifying the source sets.
> SADD skills:alice "go" "rust" "sql"
(integer) 3
> SADD skills:bob "rust" "python" "sql"
(integer) 3
> SINTER skills:alice skills:bob
1) "rust"
2) "sql"
> SUNION skills:alice skills:bob
1) "go"
2) "rust"
3) "sql"
4) "python"
> SDIFF skills:alice skills:bob
1) "go"SUNIONSTORE / SINTERSTORE / SDIFFSTORE / SINTERCARD
Store the result of set operations into a destination key. SINTERCARD returns just the cardinality of the intersection without materializing the result.
> SINTERSTORE skills:common skills:alice skills:bob
(integer) 2
> SMEMBERS skills:common
1) "rust"
2) "sql"
> SINTERCARD 2 skills:alice skills:bob
(integer) 2SPOP / SRANDMEMBER / SMOVE
SPOP removes and returns random members. SRANDMEMBER returns random members without removing them. SMOVE atomically moves a member from one set to another.
> SADD lottery "alice" "bob" "carol" "dave"
(integer) 4
> SPOP lottery
"carol"
> SRANDMEMBER lottery 2
1) "alice"
2) "dave"
> SMOVE skills:alice skills:bob "go"
(integer) 1Command Reference
| Command | Description |
|---|---|
| SADD | Add one or more members to a set |
| SREM | Remove one or more members |
| SMEMBERS | Get all members |
| SISMEMBER | Check if a member exists |
| SMISMEMBER | Check if multiple members exist |
| SCARD | Get the number of members |
| SPOP | Remove and return random members |
| SRANDMEMBER | Return random members without removing |
| SMOVE | Move a member from one set to another |
| SUNION | Return the union of multiple sets |
| SINTER | Return the intersection of multiple sets |
| SDIFF | Return the difference of multiple sets |
| SUNIONSTORE | Store union result in a key |
| SINTERSTORE | Store intersection result in a key |
| SDIFFSTORE | Store difference result in a key |
| SINTERCARD | Return the cardinality of the intersection |
| SSCAN | Incrementally iterate over members |