Redis五种数据类型之 SETS类型
SETS集合 是string类型的无序集合,通过hashtable实现,CRD操作复杂度为0(1),可取并交差集。
下面切入正题咯:
常用命令总结(自用总结 不喜勿喷):
1.sadd方法
向名称为key的set中添加元素,添加相同元素时不成功喔,集合中不允许有重复的值
eg:
127.0.0.1:6379> sadd testset "harbor"
(integer) 1
127.0.0.1:6379> sadd testset "harborChung"
(integer) 1
127.0.0.1:6379> sadd testset "harborchung"
(integer) 1
127.0.0.1:6379> sadd testset "Chung"
(integer) 1
127.0.0.1:6379> sadd testset "chung"
(integer) 1
2.semembers方法
查看指定集合的元素
eg:
127.0.0.1:6379> sadd testset "chung"
(integer) 0
3.srem方法
删除名称为key的set中的元素
eg:
127.0.0.1:6379> smembers testset
1) "harborchung"
2) "harbor"
3) "harborChung"
4) "chung"
5) "Chung"
127.0.0.1:6379> srem testset "harbor"
(integer) 1
127.0.0.1:6379> smembers testset
1) "harborChung"
2) "chung"
3) "Chung"
4) “harborchung”
4.spop 方法
随机返回并删除名称为key的set中的一个元素,返回值为删除的元素值
eg:
127.0.0.1:6379> spop testset
"chung"
127.0.0.1:6379> spop testset
"Chung"
127.0.0.1:6379> smembers testset
1) "harborChung"
2) "harborchung"
5.sdiff方法
返回所有给定key与第一个key的差集
以在前面的集合名称为标准 取差集
eg:
127.0.0.1:6379> sadd set1 "harbor"
(integer) 1
127.0.0.1:6379> sadd set1 "harborChung"
(integer) 1
127.0.0.1:6379> sadd set2 "harborChung"
(integer) 1
127.0.0.1:6379> sadd set2 "Chung"
(integer) 1
127.0.0.1:6379> sdiff set1 set2
1) "harbor"
127.0.0.1:6379> sdiff set2 set2
(empty list or set)
127.0.0.1:6379> sdiff set2 set1
1) “Chung"
6.sdiffstore方法
返回所有给定key与第一个key的差集,并将其赋予给另一个key中
eg:
127.0.0.1:6379> sdiffstore set2 set1 set3
(integer) 1
127.0.0.1:6379> smembers set3
1)“harborchung"
7.sinter方法
返回所有给定key的交集
eg:
127.0.0.1:6379> sinter set1 set2
1) "harborChung"
2) "harbor"
8.sinterstore方法
返回所有给定key的交集并将结果存到另一个集合当中
eg:
127.0.0.1:6379> sinterstore set1 set2 set4
1) "harborChung"
2) “harbor”
127.0.0.1:6379> smembers set4
1) "harborChung"
2) “harbor”
9.sunion方法
返回所有给定key的并集
eg:
127.0.0.1:6379> smembers set1
1) "harbor"
2) "chong"
3) "zhong"
127.0.0.1:6379> smembers set2
1) "harbor"
2) "harborChung"
127.0.0.1:6379> sunion set1 set2
1) "harborChung"
2) "chong"
3) "harbor"
4) "zhong"
10.sunionstore方法
返回所有给定key的并集并将结果存到另一个集合当中
eg:
127.0.0.1:6379> sunionstore set1 set2 set3
(integer) 4
127.0.0.1:6379> smembers set13
1) "harborChung"
2) "chong"
3) "harbor"
4) “zhong"
11.smove方法
从第一个key对应的set中移除member并添加到第二个集合中
eg:
127.0.0.1:6379> sadd setO1 "one"
(integer) 1
127.0.0.1:6379> sadd setO1 "two"
(integer) 1
127.0.0.1:6379> sadd setO2 "two"
(integer) 1
127.0.0.1:6379> sadd setO2 "three"
(integer) 1
127.0.0.1:6379> smove setO2 set03 "three"
(integer) 1
127.0.0.1:6379> smembers set03
1) “three"
12.scard方法
查看集合元素个数
eg:
127.0.0.1:6379> smembers set03
1) "three"
127.0.0.1:6379> scard set03
(integer) 1
127.0.0.1:6379> scard setO2
(integer) 1
127.0.0.1:6379> scard setO1
(integer) 2
127.0.0.1:6379> smembers setO2
1) "two"
127.0.0.1:6379> smembers setO1
1) "one"
2) "two"
13.sismember方法
判断某个元素是否是该集合的元素,返回值1代表是0代表不是
eg:
127.0.0.1:6379> sismember setO1 one
(integer) 1
127.0.0.1:6379> sismember setO1 hoho
(integer) 0
14.srandmember方法
随机返回名称为key的set的一个元素,不删除元素
eg:
127.0.0.1:6379> srandmember setO1
"two"
127.0.0.1:6379> srandmember setO1
"one"
127.0.0.1:6379> srandmember setO1
"two"
127.0.0.1:6379> srandmember setO1
"one"
127.0.0.1:6379> srandmember setO1
"one"
15.SPOP方法
随机返回名称为key的set的一个元素并删除元素
127.0.0.1:6379> SPOP setO1
"two"
127.0.0.1:6379> smembers setO1
“one"