redis学习-五大数据类型-Set

Set

Set中的值是不能重复的,并且无序
127.0.0.1:6379> sadd myset one   //set集合中添加元素
(integer) 1
127.0.0.1:6379> sadd myset two
(integer) 1
127.0.0.1:6379> sadd myset three
(integer) 1
127.0.0.1:6379> smembers myset    //查看set集合中的元素
1) "three"
2) "one"
3) "two"
127.0.0.1:6379> sismember myset one   //判断每一个值是否在set集合中
(integer) 1
127.0.0.1:6379> sismember myset four
(integer) 0
127.0.0.1:6379> scard myset    //返回set元素的个数
(integer) 3
127.0.0.1:6379> srem myset three    //移除set中的指定元素
(integer) 1
127.0.0.1:6379> smembers myset
1) "one"
2) "two"
##############
//随机数
127.0.0.1:6379> SMEMBERS myset
1) "one"
2) "two"
3) "four"
4) "six"
5) "three"
6) "five"
127.0.0.1:6379> SRANDMEMBER myset    //随机获取一个元素
"four"
127.0.0.1:6379> SRANDMEMBER myset
"five"
127.0.0.1:6379> SRANDMEMBER myset 2
1) "three"
2) "six"
127.0.0.1:6379> spop myset      //随机移除一个元素
"five"
127.0.0.1:6379> SMEMBERS myset
1) "one"
2) "six"
3) "two"
4) "four"
5) "three"
#################
//将一个指定的值移动到另一个set中
127.0.0.1:6379> smove myset myset2 one
(integer) 1
127.0.0.1:6379> smove myset myset2 two
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "six"
2) "four"
3) "three"
127.0.0.1:6379> SMEMBERS myset2
1) "one"
2) "two"
#################
//交集,并集,差集
127.0.0.1:6379> SMEMBERS set1
1) "a"
2) "c"
3) "b"
127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
127.0.0.1:6379> SDIFF set1 set2    //差集
1) "a"
2) "b"
127.0.0.1:6379> SINTER set1 set2    //交集
1) "c"
127.0.0.1:6379> SUNION set1 set2    //并集
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"