Redis集合-Set

时间:2021-06-01 20:40:14

sadd 向一个Set中添加数据

127.0.0.1:6379> sadd set01 1 1 2 2 3 3
(integer) 3
127.0.0.1:6379> SMEMBERS set01 (使用semebers 取出Set中所有数据)
1) "1"
2) "2"
3) "3"
127.0.0.1:6379>

sismember 判断一个set中是否包含某个值

127.0.0.1:6379> SISMEMBER set01 x
(integer) 0
127.0.0.1:6379> SISMEMBER set01 1
(integer) 1
127.0.0.1:6379>

scard 获取集合中的元素个数

127.0.0.1:6379> scard set01
(integer) 3
127.0.0.1:6379>

srem key value 删除集合中的元素

127.0.0.1:6379> srem set01 3
(integer) 1
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"
127.0.0.1:6379>

srandmember key 某个整数(随机出几个数)

127.0.0.1:6379> SRANDMEMBER set01 3 随机出三个整数(砸金蛋)
1) "1"
2) "6"
3) "5"
127.0.0.1:6379>

spop key 随机出栈

127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"
3) "4"
4) "5"
5) "6"
6) "7"
127.0.0.1:6379> SPOP set01
"2"
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379>

smove key1 key2 在key1里某个值  (将key1中的某个值移动到key2中)

127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> SMOVE set01 set02 5
(integer) 1
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "4"
3) "6"
4) "7"
127.0.0.1:6379> SMEMBERS set02
1) "5"
127.0.0.1:6379>

差集 sdiff set01 set02 (在第一个set里面而不再后边任何一个set里面的项)

127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "4"
3) "6"
4) "7"
127.0.0.1:6379> SMEMBERS set02
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> SDIFF set01 set02
1) "6"
2) "7"
127.0.0.1:6379>

交集 sinter set01 set02

127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "4"
3) "6"
4) "7"
127.0.0.1:6379> SMEMBERS set02
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> SINTER set01 set02
1) "1"
2) "4"

并集 sunion set01 set02

127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "4"
3) "6"
4) "7"
127.0.0.1:6379> SMEMBERS set02
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> SUNION set01 set02
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
127.0.0.1:6379>