目录
一、Set类型使用
1.1、SADD key member [member.....]
1.2、SMEMBERS key
1.3、SISMEMBER key member
1.4、SREM key member [member]
1.5、scard
1.6、srandmember key [数字]
1.7、spop key [数字]
1.8、smove key1 key2
1.9、集合运算
1.9.1、集合的差集运算A-B
1.9.2、集合的并集运算A∪B
1.9.2、集合的交集运算A∩B
一、Set类型使用
1.1、SADD key member [member.....]
添加元素
127.0.0.1:6379> sadd set1 1 1 2 2 3 3 4
(integer) 4
1.2、SMEMBERS key
遍历集合中的所有元素
127.0.0.1:6379> smembers set1
1) "1"
2) "2"
3) "3"
4) "4"
1.3、SISMEMBER key member
判断元素是否在集合中
127.0.0.1:6379> sismember set1 1
(integer) 1
127.0.0.1:6379> sismember set1 5
(integer) 0
1.4、SREM key member [member]
删除元素
127.0.0.1:6379> srem set1 1
(integer) 1
127.0.0.1:6379> srem set1 5
(integer) 0
1.5、scard
获取集合里面的元素个数
127.0.0.1:6379> smembers set1
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> scard set1
(integer) 3
1.6、srandmember key [数字]
从集合中随机展现设置的数字个数元素,元素不删除
127.0.0.1:6379> srandmember set1 2
1) "1"
2) "5"
127.0.0.1:6379> smembers set1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
1.7、spop key [数字]
从集合中随机弹出一个元素,出一个删一个
127.0.0.1:6379> spop set1 1
1) "2"
127.0.0.1:6379> spop set1 2
1) "1"
2) "4"
127.0.0.1:6379> smembers set1
1) "3"
2) "5"
3) "6"
1.8、smove key1 key2
将key1里已存在的某个值赋给key2
127.0.0.1:6379> smembers set1
1) "3"
2) "5"
3) "6"
127.0.0.1:6379> smembers set2
1) "7"
2) "8"
3) "9"
127.0.0.1:6379> smove set1 set2 3
(integer) 1
127.0.0.1:6379> smembers set2
1) "3"
2) "7"
3) "8"
4) "9"
1.9、集合运算
1.9.1、集合的差集运算A-B
sdiff key [key...]
127.0.0.1:6379> smembers set1
1) "c"
2) "1"
3) "b"
4) "a"
5) "2"
127.0.0.1:6379> smembers set2
1) "3"
2) "2"
3) "1"
4) "a"
5) "z"
127.0.0.1:6379> sdiff set1 set2
1) "c"
2) "b"
1.9.2、集合的并集运算A∪B
sunion key [key...]
127.0.0.1:6379> sunion set1 set2
1) "z"
2) "c"
3) "3"
4) "b"
5) "a"
6) "1"
7) "2"
1.9.2、集合的交集运算A∩B
sinter key [key...]
127.0.0.1:6379> sinter set1 set2
1) "1"
2) "a"
3) "2"
sintercard numkeys key [key...] [limit limit]
redis7新命令,它不返回结果集,而只返回结果的基数。返回由所有给定集合的交集产生的集合的基数。
127.0.0.1:6379> sintercard 2 set1 set2
(integer) 3
127.0.0.1:6379> sintercard 2 set1 set2 limit 1
(integer) 1
127.0.0.1:6379> sintercard 2 set1 set2 limit 2
(integer) 2
127.0.0.1:6379> sintercard 2 set1 set2 limit 4
(integer) 3