redis

Redis学习笔记系列(六)——基本数据类型之无序集合

2020-10-08  本文已影响0人  复旦猿

5.4 集合(set)

5.4.1 SADD
127.0.0.1:6379> SADD set a b c d
(integer) 4
127.0.0.1:6379> SMEMBERS set
1) "c"
2) "b"
3) "d"
4) "a"
5.4.2 SPOP
127.0.0.1:6379> SPOP set 3
1) "d"
2) "a"
3) "c"
127.0.0.1:6379> SPOP set 5
1) "b"
5.4.3 SMOVE
127.0.0.1:6379> SADD set a b c d
(integer) 4
127.0.0.1:6379> SMOVE set set1 a 
(integer) 1
127.0.0.1:6379> SMOVE set set1 d
(integer) 1
127.0.0.1:6379> SMOVE set set1 e
(integer) 0
127.0.0.1:6379> SMEMBERS set
1) "c"
2) "b"
127.0.0.1:6379> SMEMBERS set1
1) "d"
2) "a"
5.4.4 SMEMBERS / SISMEMBER
127.0.0.1:6379> SMEMBERS set1
1) "d"
2) "a"
127.0.0.1:6379> SMEMBERS set
1) "c"
2) "b"
127.0.0.1:6379> SISMEMBER set e
(integer) 0
127.0.0.1:6379> SISMEMBER set c
(integer) 1
5.4.5 SDIFF / SDIFFSTORE
127.0.0.1:6379> SADD set1 a b c d 
(integer) 4
127.0.0.1:6379> SADD set2 b d e f
(integer) 4
127.0.0.1:6379> SDIFF set1 set2
1) "c"
2) "a"
127.0.0.1:6379> SDIFFSTORE set3 set1 set2
(integer) 2
127.0.0.1:6379> SMEMBERS set3
1) "c"
2) "a"
5.4.6 SUNION / SUNIONSTORE
127.0.0.1:6379> SUNION set1 set2
1) "c"
2) "e"
3) "f"
4) "a"
5) "b"
6) "d"
127.0.0.1:6379> SUNIONSTORE set3 set1 set2
(integer) 6
127.0.0.1:6379> SMEMBERS set3
1) "c"
2) "e"
3) "f"
4) "a"
5) "b"
6) "d"
5.4.7 SINTER / SINTERSTORE
127.0.0.1:6379> SINTER set1
1) "c"
2) "b"
3) "d"
4) "a"
127.0.0.1:6379> SINTER set1 set2
1) "b"
2) "d" 
127.0.0.1:6379> SINTERSTORE set3 set1 set2
(integer) 2
127.0.0.1:6379> SMEMBERS set3
1) "b"
2) "d"
5.4.8 SCARD

SCARD命令用于返回集合的基数(cardinality,即集合中元素的个数),若集合不存在则将返回0。
SCARD key

127.0.0.1:6379> SCARD set1
(integer) 4
127.0.0.1:6379> SCARD set4
(integer) 0
5.4.9 SRANDMEMBER
127.0.0.1:6379> SMEMBERS set1
1) "c"
2) "b"
3) "d"
4) "a"
127.0.0.1:6379> SRANDMEMBER set1 2
1) "b"
2) "c"
127.0.0.1:6379> SRANDMEMBER set1 2
1) "a"
2) "d"
127.0.0.1:6379> SRANDMEMBER set1 -3
1) "d"
2) "c"
3) "d"
127.0.0.1:6379> SRANDMEMBER set1 -3
1) "c"
2) "c"
3) "c"
127.0.0.1:6379> SRANDMEMBER set1 0
(empty array)
5.4.10 SSCAN
127.0.0.1:6379> SSCAN set1 0
1) "0"
2) 1) "c"
   2) "b"
   3) "d"
   4) "a"
5.4.11 SREM
127.0.0.1:6379> SSCAN set1 0
1) "0"
2) 1) "c"
   2) "b"
   3) "d"
   4) "a"
127.0.0.1:6379> SREM set1 a b c
(integer) 3
127.0.0.1:6379> SMEMBERS set1
1) "d"
127.0.0.1:6379> SREM set1 a b c e
(integer) 0
127.0.0.1:6379> SMEMBERS set1
1) "d"
127.0.0.1:6379> SREM set1 a b c d
(integer) 1
127.0.0.1:6379> SMEMBERS set1
(empty array) 

写在最后

如果你觉得我写的文章帮到了你,欢迎点赞、评论、分享、赞赏哦,你们的鼓励是我不断创作的动力~

上一篇下一篇

猜你喜欢

热点阅读