A set type is an unordered collection that performs operations such as adding, deleting, or judging the existence of an element in that type. Because the internal redis is implemented using a hash table with a null value, the operation has a time complexity of O (1) and a maximum of 2^32-1 strings stored.
Duplicate elements are not allowed in the set set, and another advantage of the set type in function is that it can be set, intersect, and set in difference compared to the list type.
Sadd
129.223.248.154:6379> Sadd Students Tim
(integer) 1
129.223.248.154:6379> Sadd students Tim Ben
(integer) 1
Srem
129.223.248.154:6379> Srem Student Tim
(integer) 0
129.223.248.154:6379> Srem Students Tim
(integer) 1
Smembers, Sismember
129.223.248.154:6379> smembers Students
1) "Ben"
129.223.248.154:6379> Sismember students Ben
(integer) 1
129.223.248.154:6379> Sismember Students Tim
(integer) 0
Sdiff difference Set
129.223.248.154:6379> sadd Setdiffa 1 2 3
(integer) 3
129.223.248.154:6379> Sadd SETDIFFB 2 3 4
(integer) 3
129.223.248.154:6379> sdiff Setdiffa SETDIFFB
1) "1"
Sinter intersection
129.223.248.154:6379> sadd Setintera 1 2 3
(integer) 3
129.223.248.154:6379> Sadd Setinterb 2 3 4
(integer) 3
129.223.248.154:6379> sinter setintera setinterb
1) "2"
2) "3"
Sunion and set
129.223.248.154:6379> sadd Setuniona 1 2 3
(integer) 3
129.223.248.154:6379> Sadd setunionb 2 4 6
(integer) 3
129.223.248.154:6379> sunion Setuniona setunionb
1) "1"
2) "2"
3) "3"
4) "4"
5) "6"
SCard Total number of collections
129.223.248.154:6379> SCard Students
(integer) 1
129.223.248.154:6379> smembers Students
1) "Ben"
129.223.248.154:6379> Srandmember Students
"Ben."
129.223.248.154:6379> Sadd Students Mike
(integer) 1
129.223.248.154:6379> Srandmember Students
"Ben."
129.223.248.154:6379> Srandmember Students
"Mike."
129.223.248.154:6379> Srandmember Students
"Mike."
129.223.248.154:6379> Sadd Memebrs a b C
(integer) 3
129.223.248.154:6379> Srandmember Students 2
1) "Ben"
2) "Mike"
Srandmember
129.223.248.154:6379> Sadd letters a b C
(integer) 3
129.223.248.154:6379> srandmember Letters 2
1) "B"
2) "C"
Spop
129.223.248.154:6379> Spop Letters
"B"
129.223.248.154:6379> smembers Letters
1) "A"
2) "C"
129.223.248.154:6379>
This article is from the "Software Design and Development" blog, please be sure to keep this source http://yuanzhitang.blog.51cto.com/2769219/1789613
[Redis Chapter 7] Set