Redis-数据结构(字符串集合set)

Set和List类型不同的是,Set集合中不允许出现重复的元素

Set可包含的最大元素数量是4294967295


字符串集合(set)类型:


sadd 添加set元素

127.0.0.1:6379> sadd set1 a b c
(integer) 3



smembers 查看指定key集合元素

127.0.0.1:6379> smembers set1
1) "b"
2) "a"
3) "c"



srem 删除key集合中的元素

127.0.0.1:6379> smembers set1
1) "b"
2) "a"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> srem set1 a d
(integer) 2
127.0.0.1:6379> smembers set1
1) "b"
2) "e"
3) "c"



sismember 判断某个元素是否存在 返回1表示存在 返回0表示不存在

127.0.0.1:6379> smembers set1
1) "b"
2) "e"
3) "c"
127.0.0.1:6379> sismember set1 b
(integer) 1
127.0.0.1:6379> sismember set1 a
(integer) 0



sdiff 计算差集

127.0.0.1:6379> smembers set1
1) "b"
2) "e"
3) "c"
127.0.0.1:6379> smembers set2
1) "b"
2) "h"
3) "d"
4) "g"
5) "f"
6) "a"
7) "e"
8) "c"
127.0.0.1:6379> sdiff set2 set1
1) "f"
2) "g"
3) "h"
4) "d"
5) "a"



sinter 计算交集

127.0.0.1:6379> smembers set1
1) "b"
2) "e"
3) "c"
127.0.0.1:6379> smembers set2
1) "b"
2) "h"
3) "d"
4) "g"
5) "f"
6) "a"
7) "e"
8) "c"

127.0.0.1:6379> sinter set1 set2
1) "b"
2) "e"
3) "c"



sunion计算并集

127.0.0.1:6379> smembers set1
1) "b"
2) "e"
3) "c"
127.0.0.1:6379> smembers set2
1) "b"
2) "h"
3) "d"
4) "g"
5) "f"
6) "a"
7) "e"
8) "c"

127.0.0.1:6379> sunion set1 set2
1) "h"
2) "d"
3) "a"
4) "f"
5) "g"
6) "e"
7) "c"
8) "b"



scard 计算元素总数

127.0.0.1:6379> scard set1
(integer) 3
127.0.0.1:6379> scard set2
(integer) 8



srandmember 随机取一个或多个元素

127.0.0.1:6379> srandmember set1
"c"
127.0.0.1:6379> srandmember set1
"e"
127.0.0.1:6379> srandmember set1 2
1) "e"
2) "c"



sdiffstore 把差集结果存储到新集合中

127.0.0.1:6379> smembers set1
1) "b"
2) "e"
3) "c"

127.0.0.1:6379> smembers set2
1) "b"
2) "h"
3) "d"
4) "g"
5) "f"
6) "a"
7) "e"
8) "c"
127.0.0.1:6379> sdiffstore set3 set2 set1
(integer) 5
127.0.0.1:6379> smembers set3
1) "f"
2) "g"
3) "h"
4) "d"
5) "a"



sunionstore 把并集结果存储到新集合中

127.0.0.1:6379> smembers set3
1) "f"
2) "g"
3) "h"
4) "d"
5) "a"
127.0.0.1:6379> smembers set1
1) "b"
2) "e"
3) "c"
127.0.0.1:6379> sunionstore set4 set3 set1
(integer) 8
127.0.0.1:6379> smembers set4
1) "b"
2) "h"
3) "d"
4) "e"
5) "f"
6) "g"
7) "a"
8) "c"


你可能感兴趣的:(Redis)