[Redis Chapter 7] Set
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Redis Chapter 7] Set相关的知识,希望对你有一定的参考价值。
Set类型是一个没有排序的集合,可以在该类型那个执行添加、删除或判断某一元素是否存在等操作。由于Redis的内部是使用值为空的hash table实现的,所以操作的时间复杂度为O(1), 最多存储2^32-1个字符串。
Set集合中不允许出现重复的元素,和List类型相比,Set类型在功能上另一个优势是可以进行并集,交集,差集运算。
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 差集
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 交集
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 并集
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 集合总数
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>
本文出自 “软件设计与开发” 博客,请务必保留此出处http://yuanzhitang.blog.51cto.com/2769219/1789613
以上是关于[Redis Chapter 7] Set的主要内容,如果未能解决你的问题,请参考以下文章