Redis Set
Redis Set is an unordered collection of string types. Set members are unique, which means duplicate data cannot appear in the set.
The encoding of a set object can be intset or hashtable.
Sets in Redis are implemented using hashtables, so the complexity for adding, deleting, and searching is O(1).
The maximum number of members in a set is 2^32 - 1 (4294967295).
Example
redis 127.0.0.1:6379> SADD tutorialprokey redis
(integer) 1
redis 127.0.0.1:6379> SADD tutorialprokey mongodb
(integer) 1
redis 127.0.0.1:6379> SADD tutorialprokey mysql
(integer) 1
redis 127.0.0.1:6379> SADD tutorialprokey mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS tutorialprokey
1) "mysql"
2) "mongodb"
3) "redis"
In the above example, we inserted three elements into the set named tutorialprokey using the SADD command.
Redis Set Commands
The following table lists the basic Redis set commands:
Number | Command and Description |
---|---|
1 | SADD key member1 [member2] <br>Add one or more members to a set |
2 | SCARD key <br>Get the number of members in a set |
3 | SDIFF key1 [key2] <br>Return the difference between the first set and other sets |
4 | SDIFFSTORE destination key1 [key2] <br>Store the difference of the given sets in destination |
5 | SINTER key1 [key2] <br>Return the intersection of the given sets |
6 | SINTERSTORE destination key1 [key2] <br>Store the intersection of the given sets in destination |
7 | SISMEMBER key member <br>Determine if a member is a member of the set key |
8 | SMEMBERS key <br>Return all members in a set |
9 | SMOVE source destination member <br>Move a member from the source set to the destination set |
10 | SPOP key <br>Remove and return a random element from the set |
11 | SRANDMEMBER key [count] <br>Return one or more random members from a set |
12 | SREM key member1 [member2] <br>Remove one or more members from a set |
13 | SUNION key1 [key2] <br>Return the union of the given sets |
14 | SUNIONSTORE destination key1 [key2] <br>Store the union of the given sets in destination |
15 | SSCAN key cursor [MATCH pattern] [COUNT count] <br>Iterate over the elements in a set |