A Redis Set is an unordered collection of type String. Collection members are unique, which means that duplicate data cannot appear in the collection.
Collections in Redis are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).
The maximum number of members in a collection is 232 - 1 (4294967295, each collection can store more than 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 above example we inserted three elements into the collection named runoobkey via the SADD command .
Redis collection commands
The following table lists the basic commands for Redis collections:
serial number | Command and Description |
---|---|
1 | SADD key member1 [member2] Add one or more members to the set |
2 | SCARD key to get the number of members of the set |
3 | SDIFF key1[key2] returns the difference of all sets given |
4 | SDIFFSTORE destination key1 [key2] returns the difference of all sets given and stored in destination |
5 | SINTER key1[key2] returns the intersection of all sets given |
6 | SINTERSTORE destination key1 [key2] returns the intersection of all sets given and stored in destination |
7 | SISMEMBER key member Determines whether the member element is a member of the set key |
8 | SMEMBERS key returns all members in the set |
9 | SMOVE source destination member moves the member element from the source collection to the destination collection |
10 | SPOP key removes and returns a random element in the collection |
11 | SRANDMEMBER key [count] returns one or more random numbers in the collection |
12 | SREM key member1 [member2] removes one or more members from the set |
13 | SUNION key1[key2] returns the union of all the given sets |
14 | SUNIONSTORE destination key1 [key2] The union of all the given sets is stored in the destination set |
15 | SSCAN key cursor [MATCH pattern] [COUNT count] Iterates over the elements in the collection |