English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Sets (Set)

Redis's Set is an unordered collection of String type. Set members are unique, which means that duplicate data cannot appear in the set.

Redis sets are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1)

The maximum number of members in the set is 232  - 1 (4294967295, each set can store4With over 0 billion members).

Online examples

redis 127.0.0.1:6379> SADD w3codeboxkey redis
(integer) 1
redis 127.0.0.1:6379> SADD w3codeboxkey mongodb
(integer) 1
redis 127.0.0.1:6379> SADD w3codeboxkey mysql
(integer) 1
redis 127.0.0.1:6379> SADD w3codeboxkey mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS w3codeboxkey
1) "mysql"
2) "mongodb"
3) "redis"

In the above example, we use SADD The command adds to the set named w3codeboxkey Three elements inserted into the set.

Redis set commands

The following table lists the basic Redis set commands:

Serial numberCommand and description
1SADD key member1 [member2]
Add one or more members to the set
2SCARD key
Get the number of members in the set
3SDIFF key1 [key2]
Return the difference between the first set and other sets
4SDIFFSTORE destination key1 [key2]
Return the difference set of all given sets and store it in destination
5SINTER key1 [key2]
Return the intersection of all given sets
6SINTERSTORE destination key1 [key2]
Return the intersection of all given sets and store it in destination
7SISMEMBER key member
Determine whether the member element is a member of the set key
8SMEMBERS key
Return all members of the set
9SMOVE source destination member
Move the member element from the source set to the destination set
10SPOP key
Remove and return a random element from the set
11SRANDMEMBER key [count]
Return one or more random numbers from the set
12SREM key member1 [member2]
Remove one or more members from the set
13SUNION key1 [key2]
Return the union of all given sets
14SUNIONSTORE destination key1 [key2]
The union of all given sets is stored in the destination set
15SSCAN key cursor [MATCH pattern] [COUNT count]
Iterate over elements in a set