Redis's set is an unordered collection of type string. A collection member is unique, which means that duplicate data cannot appear in the collection.
The collection in Redis is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1).
The maximum number of members in the collection is 232-1 (4294967295, each of which can store 40多亿个 members).
Instance
- Redis 127.0. 0.1:6379> sadd w3ckey redis
- (integer) 1
- Redis 127.0. 0.1:6379> sadd w3ckey mongodb
- (integer) 1
- Redis 127.0. 0.1:6379> sadd w3ckey mysql
- (integer) 1
- Redis 127.0. 0.1:6379> sadd w3ckey mysql
- (integer) 0
- Redis 127.0. 0.1:6379> smembers w3ckey
- 1) "MySQL"
- 2) "MongoDB"
- 3) "Redis"
In the above example we insert three elements into the collection named W3ckey with the sadd command.
Redis Collection Commands
The following table lists the Redis collection basic commands:
Serial Number |
Command and Description |
1 |
Sadd key Member1 [member2] adds one or more members to the collection |
2 |
SCard Key gets the number of members of the collection |
3 |
Sdiff Key1 [Key2] Returns the difference set for all sets given |
4 |
Sdiffstore destination Key1 [Key2] Returns the difference set for all sets and is 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 collection key |
8 |
Smembers Key returns all members in the collection |
9 |
Smove Source Destination member moves the member element from the source collection to the destination collection |
10 |
SPOP key to remove and return a random element from 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 collection |
13 |
Sunion Key1 [Key2] Returns the set of all the given sets |
14 |
Sunionstore destination Key1 [Key2] The aggregation of all given collections is stored in the destination collection |
15 |
Sscan key cursor [MATCH pattern] [count count] elements in the iteration collection |
Redis Collection (SET)