Redis Set
A Redis Set is an unordered collection of String type. Set members are unique, which means duplicate data cannot appear in a set.
The encoding of a set object can be intset or hashtable.
In Redis, a set is implemented via a hash table, so the complexity of add, delete, and lookup operations is O(1).
The maximum number of members in a set is 2^32 - 1 (4294967295, each set can store over 4 billion members).
Example
redis 127.0.0.1:6379> SADD runoobkey redis
(integer) 1
redis 127.0.0.1:6379> SADD runoobkey mongodb
(integer) 1
redis 127.0.0.1:6379> SADD runoobkey mysql
(integer) 1
redis 127.0.0.1:6379> SADD runoobkey mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS runoobkey
1) "mysql"
2) "mongodb"
3) "redis"
In the example above we used SADD command to insert three elements into the set named runoobkey .
Redis Set Commands
The table below lists the basic Redis set commands:
| No. | Command and Description |
|---|---|
| 1 | SADD key member1 [member2] |
| Add one or more members to the set | |
| 2 | SCARD key |
| Get the number of members in the set | |
| 3 | SDIFF key1 [key2] |
| Return the difference between the first set and other sets. | |
| 4 | SDIFFSTORE destination key1 [key2] |
| Return the difference of all given sets and store it in destination | |
| 5 | SINTER key1 [key2] |
| Return the intersection of all given sets | |
| 6 | SINTERSTORE destination key1 [key2] |
| Return the intersection of all given sets and store it in destination | |
| 7 | SISMEMBER key member |
| Check whether the member element is a member of the set key | |
| 8 | SMEMBERS key |
| Return all members of the set | |
| 9 | SMOVE source destination member |
| Move the member element from the source set to the destination set | |
| 10 | SPOP key |
| Remove and return a random element from the set | |
| 11 | SRANDMEMBER key [count] |
| Return one or more random members from the set | |
| 12 | SREM key member1 [member2] |
| Remove one or more members from the set | |
| 13 | SUNION key1 [key2] |
| Return the union of all given sets | |
| 14 | SUNIONSTORE destination key1 [key2] |
| Store the union of all given sets in the destination set | |
| 15 | SSCAN key cursor [MATCH pattern] [COUNT count] |
| Iterate over the elements in the set |