Set
Set中的值不能重复
基础
127.0.0.1:6379> sadd set1 hello #set集合添加元素
(integer) 1
127.0.0.1:6379> sadd set1 world
(integer) 1
127.0.0.1:6379> sadd set1 666
(integer) 1
127.0.0.1:6379> smembers set1 #查看指定set的所有值
1) "hello"
2) "666"
3) "world"
127.0.0.1:6379> sismember set1 666 #判断某个值在不在set集合中
(integer) 1
127.0.0.1:6379> sismember set1 nihao
(integer) 0
127.0.0.1:6379> scard set1 #获取set集合中的元素个数
(integer) 3
srem
127.0.0.1:6379> smembers set1
1) "hello"
2) "666"
3) "world"
127.0.0.1:6379> srem set1 hello #删除指定元素
(integer) 1
127.0.0.1:6379> smembers set1
1) "666"
2) "world"
SRANDMEMBER
127.0.0.1:6379> SRANDMEMBER set1 #随机获取一条数据
"world"
127.0.0.1:6379> SRANDMEMBER set1
"666"
127.0.0.1:6379> SRANDMEMBER set1
"world"
spop
127.0.0.1:6379> SMEMBERS set1
1) "666"
2) "world"
127.0.0.1:6379> spop set1 #随机删除一条数据
"666"
127.0.0.1:6379> SMEMBERS set1
1) "world"
smove
127.0.0.1:6379> SMEMBERS myset
1) "zhaoliu"
2) "lisi"
3) "zhangsan"
4) "wangwu"
127.0.0.1:6379> smove myset newset lisi #把myset集合中的lisi移动到newset集合中(没有创建)
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "zhaoliu"
2) "zhangsan"
3) "wangwu"
127.0.0.1:6379> SMEMBERS newset
1) "lisi"
差集、交集、并集
127.0.0.1:6379> SMEMBERS set1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SMEMBERS set2
1) "3"
2) "4"
3) "5"
127.0.0.1:6379> SDIFF set1 set2 #差集
1) "1"
2) "2"
127.0.0.1:6379> SINTER set1 set2 #交集
1) "3"
127.0.0.1:6379> SUNION set1 set2 #并集
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"