Set
-
Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。(无序不重复)
-
集合对象的编码可以是 intset 或者 hashtable。
-
Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。
案例
127.0.0.1:6379> SADD myset hello #set集合中添加元素
(integer) 1
127.0.0.1:6379> SADD myset lili
(integer) 1
127.0.0.1:6379> SADD myset love ll
(integer) 2
127.0.0.1:6379> SMEMBERS myset #查看指定set的所有值
1) "love"
2) "lili"
3) "ll"
4) "hello"
127.0.0.1:6379> SISMEMBER myset hello #判断某一个值是否存在set中
(integer) 1
127.0.0.1:6379> SISMEMBER myset lili
(integer) 1
- scard
127.0.0.1:6379> scard myset #获取set集合中元素的个数
(integer) 4
- SREM
127.0.0.1:6379> SREM myset hello #移除set中的指定元素
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "love"
2) "lili"
3) "ll"
- SMEMBERS
127.0.0.1:6379> SMEMBERS myset #随机抽取set中的一个元素
1) "love"
2) "lili"
3) "ll"
127.0.0.1:6379> SRANDMEMBER myset
"love"
127.0.0.1:6379> SRANDMEMBER myset
"ll"
127.0.0.1:6379> SRANDMEMBER myset
"love"
127.0.0.1:6379> SRANDMEMBER myset
"ll"
127.0.0.1:6379> SRANDMEMBER myset 2 #随机抽取指定个数的元素
1) "lili"
2) "ll"
127.0.0.1:6379> SRANDMEMBER myset 2
1) "love"
2) "ll"
- SPOP
127.0.0.1:6379> SMEMBERS myset
1) "love"
2) "lili"
3) "ll"
127.0.0.1:6379> SPOP myset #随机删除set集合中的一个元素
"ll"
127.0.0.1:6379> SPOP myset
"love"
127.0.0.1:6379> SMEMBERS myset
1) "lili"
- SMOVE
127.0.0.1:6379> SADD myset hello
(integer) 1
127.0.0.1:6379> SADD myset hello1
(integer) 1
127.0.0.1:6379> SADD myset hello2
(integer) 1
127.0.0.1:6379> SADD myset hello3
(integer) 1
127.0.0.1:6379> SADD myset2 set2
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> SMOVE myset myset2 hello #将一个指定的值移动到另外以一个set
(integer) 1
127.0.0.1:6379> smembers myset
1) "hello3"
2) "hello1"
3) "hello2"
127.0.0.1:6379> smembers myset2
1) "hello"
2) "set2"
- SDIFF&SINTER&sunion
127.0.0.1:6379> SADD key1 a
(integer) 1
127.0.0.1:6379> SADD key1 b
(integer) 1
127.0.0.1:6379> SADD key1 c
(integer) 1
127.0.0.1:6379> SADD key2 c
(integer) 1
127.0.0.1:6379> SADD key2 d
(integer) 1
127.0.0.1:6379> SADD key2 e
(integer) 1
127.0.0.1:6379> SDIFF key1 key2 #差集,a和b做差集 = a有的b没有,前边集合和后边集合作比较,找出前边集合和后边集合不同的
1) "a"
2) "b"
127.0.0.1:6379> SINTER key1 key2 #交集
1) "c"
127.0.0.1:6379> sunion key1 key2 #并集
1) "d"
2) "b"
3) "c"
4) "a"
5) "e"
标签:set,SADD,0.1,myset,Redis,6379,127.0,integer
From: https://www.cnblogs.com/OpenSourceSite/p/18230096