127.0.0.1:6379> keys *
1) "k6"
2) "k3"
3) "k4"
4) "k5"
5) "k2"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> sadd k1 v1 v2 v3 ---- 向集合k1添加多个值,如果值存在,不会重复添加,并且集合无序
(integer) 3
127.0.0.1:6379> smembers k1 --- 输出集合k1的值【无序输出】
1) "v1"
2) "v3"
3) "v2"
127.0.0.1:6379> sismember k1 v2 ---- 判断值v2是否存在集合K1中,存在返回1,不存在返回0
(integer) 1
127.0.0.1:6379> sismember k1 v21
(integer) 0
127.0.0.1:6379> scard k1 ---- 获取集合k1的值的个数
(integer) 3
127.0.0.1:6379> srem k1 v1 v2 ---- 删除集合k1中的多个值
(integer) 2
127.0.0.1:6379> smembers k1
1) "v3"
127.0.0.1:6379> srem k1 v22 ---- 如果删除的值不存在集合k1中,则返回0
(integer) 0
127.0.0.1:6379> smembers k1
1) "v3"
127.0.0.1:6379> sadd k2 v4 v5 v2 v1
(integer) 4
127.0.0.1:6379> smembers k2
1) "v1"
2) "v5"
3) "v4"
4) "v2"
127.0.0.1:6379> spop k2 ---- 随机删除集合k1中的值
"v2"
127.0.0.1:6379> smembers k2
1) "v1"
2) "v5"
3) "v4"
127.0.0.1:6379> spop k2
"v1"
127.0.0.1:6379> smembers k2
1) "v5"
2) "v4"
127.0.0.1:6379> srandmember k2 1 ---- 随机获取集合k1中的N个值,不会做删除 *** 作
1) "v5"
127.0.0.1:6379> srandmember k2 1
1) "v4"
127.0.0.1:6379> srandmember k2 3
1) "v5"
2) "v4"
127.0.0.1:6379> smembers k2
1) "v5"
2) "v4"
127.0.0.1:6379> smembers k1
1) "v3"
127.0.0.1:6379> smove k2 k1 v6 --- 把值V6从集合K2中移到K1中,k2会删除v6的值,k1会添加v6的值,如果V6存在集合k1中,则不做任何处理,因为集合是不重复的
(integer) 0
127.0.0.1:6379> smembers k1
1) "v3"
127.0.0.1:6379> smove k2 k1 v6
(integer) 0
127.0.0.1:6379> smembers k1
1) "v3"
127.0.0.1:6379> smembers k2
1) "v5"
2) "v4"
127.0.0.1:6379> smove k2 k1 v4
(integer) 1
127.0.0.1:6379> smembers k2
1) "v5"
127.0.0.1:6379> smembers k1
1) "v3"
2) "v4"
127.0.0.1:6379> sadd k3 v1 v2 v3 v4 v2 ---- 向集合k3添加多个值,如果值存在,不会重复添加,并且集合无序
(integer) 4
127.0.0.1:6379> smembers k3
1) "v1"
2) "v3"
3) "v4"
4) "v2"
127.0.0.1:6379> smembers k1
1) "v3"
2) "v4"
127.0.0.1:6379> sismember k3 v3
(integer) 1
127.0.0.1:6379> sismember k3 v22
(integer) 0
127.0.0.1:6379> sinter k2 k3
(empty array)
127.0.0.1:6379> smembers k1
1) "v3"
2) "v4"
127.0.0.1:6379> smembers k3
1) "v2"
2) "v4"
3) "v1"
4) "v3"
127.0.0.1:6379> smembers k2
1) "v5"
127.0.0.1:6379> sinter k1 k3 --- 获取集合k1和k3的交集
1) "v3"
2) "v4"
127.0.0.1:6379> sunion k2 k3 --- 获取集合k2和k3的并集
1) "v1"
2) "v5"
3) "v4"
4) "v2"
5) "v3"
127.0.0.1:6379> sdiff k3 k2 --- 获取集合k3和k2的差集【存在k3但不在k2中的值】
1) "v1"
2) "v3"
3) "v4"
4) "v2"
127.0.0.1:6379> sdiff k3 k1
1) "v1"
2) "v2"
127.0.0.1:6379>
集合的数据结构是dict字典,字典是用哈希表实现的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)