1

Redis set数据类型命令使用及应用场景使用总结 - 香吧香

 1 year ago
source link: https://www.cnblogs.com/zjdxr-up/p/17018543.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

转载请注明出处:

目录

1.sadd 集合添加元素

sadd key value 

  添加元素示例:

127.0.0.1:6379> sadd action:10001 101 102 103 104
(integer) 4
127.0.0.1:6379> smembers action:10001
1) "101"
2) "102"
3) "103"
4) "104"
127.0.0.1:6379>

2.srem移除元素

srem key member
127.0.0.1:6379> srem action:10001 101
(integer) 1
127.0.0.1:6379> srem action:10001 106
(integer) 0
127.0.0.1:6379> 

  元素存在移除成功返回1,不存在移除返回0

3.smembers 获取key的所有元素

smembers key
127.0.0.1:6379> smembers action:10001
1) "101"
2) "102"
3) "103"
4) "104"

4.scard 获取key的个数

scard key

127.0.0.1:6379> scard action:10001
(integer) 4
127.0.0.1:6379>

  应用场景:多少人点赞,多少人评论,以及多少人关注等

5.sismember 判断member元素是否存在集合key中

sismember key member
127.0.0.1:6379> sismember action:10001 102
(integer) 1
127.0.0.1:6379> sismember action:10001 109
(integer) 0
127.0.0.1:6379>

  应用场景:检查用户是否点过赞或访问过

6.srandmember key count 从集合key中随机选出count个元素,元素不从key中删除

srandmember key count
127.0.0.1:6379> srandmember action:10001 1
1) "104"
127.0.0.1:6379> srandmember action:10001 2
1) "104"
2) "103"
127.0.0.1:6379>

  应用场景:随机抽取,并不从集合中移除随机抽取出的元素

7.spop 从集合key中随机选出count个元素,元素从key中删除

spop key count
127.0.0.1:6379> spop action:10001 1
1) "103"
127.0.0.1:6379> 

  应用场景: 活动抽奖,从参与活动的set集合中,随机抽取出一等奖,二等奖等; 游戏场景中 给用户分配分区

8.sinter 交集运算

sinter key [key...]
127.0.0.1:6379> smembers action:10001
1) "101"
2) "102"
3) "104"
127.0.0.1:6379> smembers action:10002
1) "102"
2) "104"
3) "106"
4) "108"
127.0.0.1:6379> sinter action:10001 action:10002
1) "102"
2) "104"
127.0.0.1:6379> 

  应用场景:共同的好友,爱好等

9.sinterstore 将交集的结果存入到新集合destination中  

sinterstore destination key [key....]
127.0.0.1:6379> sinterstore  common:action:10001 action:10001 action:10002
(integer) 2
127.0.0.1:6379> smembers common:action:10001
1) "102"
2) "104"
127.0.0.1:6379>

  应用场景:共同的好友,爱好等

10.sunion 并集运算

sunion key [key ....]
127.0.0.1:6379> sunion action:10001 action:10002
1) "101"
2) "102"
3) "104"
4) "106"
5) "108"
127.0.0.1:6379> 

  应用场景:获取集合间的所有元素

11.sunionstore 将并集结果存入新集合destionation中

sunionstore destionation key [key.....]
127.0.0.1:6379> sunionstore all:action:10001 action:10001 action:10002
(integer) 5
127.0.0.1:6379> smembers all:action:10001
1) "101"
2) "102"
3) "104"
4) "106"
5) "108"
127.0.0.1:6379> 

  应用场景:获取集合间的所有元素到新的集合

12.sdiff 差集运算

sdiff key [key....]

  差集比较的是 前面第一个key中的元素在后面集合元素中比较,后面集合中包含key的元素

127.0.0.1:6379> smembers action:10001
1) "101"
2) "102"
3) "104"
127.0.0.1:6379> smembers action:10002
1) "102"
2) "104"
3) "106"
4) "108"
127.0.0.1:6379> sdiff action:10001 action:10002
1) "101"
127.0.0.1:6379> 

  应用场景:可能认识的人

13.sdiffstore 将差集结果存入新的集合

sdiffstore destination key [key...]
127.0.0.1:6379> sdiffstore diff:action:10001 action:10001 action:10002
(integer) 1
127.0.0.1:6379> smembers diff:action:10001
1) "101"
127.0.0.1:6379> 

  应用场景:可能认识的人


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK