Set(集合)
set的值是不可重复的!
127.0.0.1:6379> SADD myset "lixin,hello" #set集合中添加匀速
(integer) 1
127.0.0.1:6379> SADD myset "lilin,hello"
(integer) 1
127.0.0.1:6379> SMEMBERS myset #查看指定set的所有值
1) "lixin,hello"
2) "lilin,hello"
127.0.0.1:6379> SISMEMBER myset lixin,hello #判断某一个值是不是在set集合中!
(integer) 1
127.0.0.1:6379> SISMEMBER myset lilin,hello
(integer) 1
127.0.0.1:6379> scard myset #获取set集合中的内容元素个数
(integer) 2
127.0.0.1:6379> SADD myset "hello"
(integer) 1
127.0.0.1:6379> SADD myset "world"
(integer) 1
127.0.0.1:6379> scard myset
(integer) 4
127.0.0.1:6379>
REM : 移动set集合中的指定元素
127.0.0.1:6379> SREM myset hello
(integer) 1
127.0.0.1:6379> scard myset
(integer) 3
127.0.0.1:6379> SMEMBERS myset
1) "lixin,hello"
2) "world"
3) "lilin,hello"
127.0.0.1:6379>
Set无序不重复集合,抽随机
SRANDMEMBER :随机抽选出一个元素
127.0.0.1:6379> SRANDMEMBER myset
"lixin,hello"
127.0.0.1:6379> SRANDMEMBER myset
"lilin,hello"
127.0.0.1:6379> SRANDMEMBER myset
"lilin,hello"
127.0.0.1:6379>
127.0.0.1:6379> SRANDMEMBER myset
"lixin,hello"
127.0.0.1:6379> SRANDMEMBER myset
"world"
SPOP:删除指定的key,随机删除key!
127.0.0.1:6379> SMEMBERS myset
1) "lixin,hello"
2) "world"
3) "lilin,hello"
127.0.0.1:6379> spop myset #随机删除一些set集合中的元素!
"lilin,hello"
127.0.0.1:6379> spop myset
"world"
127.0.0.1:6379> SMEMBERS myset
1) "lixin,hello"
127.0.0.1:6379>
将一个指定的值,移动到另外一个
127.0.0.1:6379> SADD myset "a"
(integer) 1
127.0.0.1:6379> SADD myset "b"
(integer) 1
127.0.0.1:6379> SADD myset "c"
(integer) 1
127.0.0.1:6379> SADD myset "e"
(integer) 1
127.0.0.1:6379> SADD myset2 "e"
(integer) 1
127.0.0.1:6379> smove myset myset2 "a" #将一个指定的值,移动到另一个set集合!
(integer) 1
127.0.0.1:6379> SMEMBERS mysetw
(empty array)
127.0.0.1:6379> SMEMBERS myset2
1) "a"
2) "e"
127.0.0.1:6379> SMEMBERS myset
1) "c"
2) "b"
3) "e"
127.0.0.1:6379>
- 微博,B站,共同关注
- 数字集合类:
- 交集 SUNION
- 并集 SINTER
- 差集 SDIFF
127.0.0.1:6379> SMEMBERS myset
1) "c"
2) "b"
3) "e"
127.0.0.1:6379> SADD myset3 "e"
(integer) 1
127.0.0.1:6379> SADD myset3 "f"
(integer) 1
127.0.0.1:6379> SADD myset3 "g"
(integer) 1
127.0.0.1:6379> SDIFF myset myset3
1) "b"
2) "c"
127.0.0.1:6379> sinter myset myset3
1) "e"
127.0.0.1:6379>