目录
Redis数据类型之无序集合
Redis 的 Set 是 string 类型的无序集合。集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。集合中最大的成员数为 2^32 - 1 (4294967295, 每个集合可存储40多亿个成员)。
查看命令帮助
127.0.0.1:6379> help @set
SADD key member [member ...]
summary: Add one or more members to a set
since: 1.0.0
SCARD key
summary: Get the number of members in a set
since: 1.0.0
SDIFF key [key ...]
summary: Subtract multiple sets
since: 1.0.0
SDIFFSTORE destination key [key ...]
summary: Subtract multiple sets and store the resulting set in a key
since: 1.0.0
SINTER key [key ...]
summary: Intersect multiple sets
since: 1.0.0
SINTERSTORE destination key [key ...]
summary: Intersect multiple sets and store the resulting set in a key
since: 1.0.0
SISMEMBER key member
summary: Determine if a given value is a member of a set
since: 1.0.0
SMEMBERS key
summary: Get all the members in a set
since: 1.0.0
SMOVE source destination member
summary: Move a member from one set to another
since: 1.0.0
SPOP key [count]
summary: Remove and return one or multiple random members from a set
since: 1.0.0
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
since: 1.0.0
SREM key member [member ...]
summary: Remove one or more members from a set
since: 1.0.0
SSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate Set elements
since: 2.8.0
SUNION key [key ...]
summary: Add multiple sets
since: 1.0.0
SUNIONSTORE destination key [key ...]
summary: Add multiple sets and store the resulting set in a key
since: 1.0.0
增加数据
给集合新增成员,若集合不存在则创建集合并新增成员
SADD
127.0.0.1:6379> SADD myset 1
(integer) 1
127.0.0.1:6379> SADD myset 2
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"
查看数据
SMEMBERS
127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"
删除数据
SREM
127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> SREM myset 3 4
(integer) 2
127.0.0.1:6379> SMEMBERS myset
1) "1"
2) "2"
3) "5"
移动数据
SMOVE
127.0.0.1:6379> SADD myset1 1 2 3 4 5 6
(integer) 6
127.0.0.1:6379> SADD myset2 noe two three
(integer) 3
127.0.0.1:6379> SMOVE myset1 myset2 4
(integer) 1
127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
127.0.0.1:6379> SMEMBERS myset2
1) "4"
2) "three"
3) "two"
4) "noe"
返回集合中成员的个数
SCARD
127.0.0.1:6379> SMEMBERS myset2
1) "4"
2) "three"
3) "two"
4) "noe"
127.0.0.1:6379> SCARD myset2
(integer) 4
随机返回指定个数的数据
SRANDMEMBER
127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
127.0.0.1:6379> SRANDMEMBER myset1 2
1) "6"
2) "5"
127.0.0.1:6379> SRANDMEMBER myset1 3
1) "2"
2) "1"
3) "3"
127.0.0.1:6379> SRANDMEMBER myset1 3
1) "2"
2) "5"
3) "3"
判断对象是否存在于集合中
SISMEMBER
127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "5"
5) "6"
127.0.0.1:6379> SISMEMBER myset1 3
(integer) 1
127.0.0.1:6379> SISMEMBER myset1 9
(integer) 0
随机返回并删除一个成员
SPOP
127.0.0.1:6379> SADD myset3 q w e r t f g h
(integer) 8
127.0.0.1:6379> SMEMBERS myset3
1) "f"
2) "w"
3) "g"
4) "r"
5) "t"
6) "h"
7) "q"
8) "e"
127.0.0.1:6379> SPOP myset3
"q"
127.0.0.1:6379> SMEMBERS myset3
1) "f"
2) "w"
3) "g"
4) "r"
5) "t"
6) "h"
7) "e"
返回多个集合的交集
SINTER
127.0.0.1:6379> SADD myset4 1 2 3 4 5
(integer) 5
127.0.0.1:6379> SADD myset5 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset6 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SINTER myset4 myset5 myset6
1) "5"
将多个集合的交集结果保存为一个新的集合
SINTERSTORE
127.0.0.1:6379> SADD myset2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset3 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SINTERSTORE mysetnew myset2 myset3
(integer) 3
127.0.0.1:6379> SMEMBERS mysetnew
1) "5"
2) "6"
3) "7"
127.0.0.1:6379> SMEMBERS myset2
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> SMEMBERS myset3
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
返回多个集合的并集
SUNION
127.0.0.1:6379> SADD myset4 1 2 3 4 5
(integer) 5
127.0.0.1:6379> SADD myset5 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset6 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SUNION myset4 myset5 myset6
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
将多个集合的并集的结果保存为一个新的集合
SUNIONSTORE
127.0.0.1:6379> SADD myset2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SADD myset3 5 6 7 8 9
(integer) 5
127.0.0.1:6379> SUNIONSTORE mysetnew myset2 myset3
(integer) 7
127.0.0.1:6379> SMEMBERS mysetnew
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
6) "8"
7) "9"
127.0.0.1:6379> SMEMBERS myset2
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> SMEMBERS myset3
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
返回多个集合的差集
以最左边的为主集合,返回差集
SDIFF
127.0.0.1:6379> SMEMBERS myset2
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> SMEMBERS myset3
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
127.0.0.1:6379> SDIFF myset2 myset3
1) "3"
2) "4"
127.0.0.1:6379> SDIFF myset3 myset2
1) "8"
2) "9"
将多个集合的差集的结果保存为一个新的集合
SDIFFSTORE
127.0.0.1:6379> SDIFF myset2 myset3
1) "3"
2) "4"
127.0.0.1:6379> SDIFFSTORE mysetnew myset2 myset3
(integer) 2
127.0.0.1:6379> SMEMBERS mysetnew
1) "3"
2) "4"
标签:127.0,0.1,数据类型,Redis,无序,6379,key,integer,SMEMBERS
From: https://www.cnblogs.com/c-moon/p/16797211.html