总目录:https://www.jianshu.com/p/e406a9bc93a9
数据库 - 子目录:https://www.jianshu.com/p/4499e57a5604
set
redis的set可以理解成是java的hashset,放弃有序性来换得效率。
set类型
- 新的存储需求:存储大量的数据,在查询方面提供更高的效率
- 需要的存储结构:能保存大量的数据,高效的存储结构,便于查询
基础操作
- 添加数据
sadd key membre1 member2...
![](https://img.haomeiwen.com/i20155953/84b09b65119e40b7.png)
- 获取所有数据
smembers key
![](https://img.haomeiwen.com/i20155953/1787e95069e7bbfd.png)
- 删除数据
srem key member1 member2...
![](https://img.haomeiwen.com/i20155953/e2c08aaf29300306.png)
- 获取集合元素个数
scard key
![](https://img.haomeiwen.com/i20155953/7689b46a6dc010c4.png)
- 判断某元素是否在集合内
sismembers key member
![](https://img.haomeiwen.com/i20155953/a912c14bb756821d.png)
扩展操作
- 随机获得集合内指定数量的数据
srandmember key count
![](https://img.haomeiwen.com/i20155953/ab6d4bd707269409.png)
- 随机返回一条数据并删除
spop key
![](https://img.haomeiwen.com/i20155953/469a1ad6257df9e0.png)
![](https://img.haomeiwen.com/i20155953/7d925bfff20acbf2.png)
- 求两个集合的交并补集
sinter key1 key2
sunion key1 key2
sdiff key1 key2
![](https://img.haomeiwen.com/i20155953/9aa6896cfd685b45.png)
- 求两个集合的交并补集并保存到指定集合内
sinterstore destiantion key1 key2
sunionstore destiantion key1 key2
sdiffstore destiantion key1 key2
![](https://img.haomeiwen.com/i20155953/bb9e57db97f0f7e6.png)
![](https://img.haomeiwen.com/i20155953/794d1146a153c39e.png)
- 将某个集合的一个数据移动到另一个集合中
smove source destiantion member
![](https://img.haomeiwen.com/i20155953/9bed1836a7201044.png)
注意事项
- set类型不允许重复,第一次添加成功,以后都会添加失败。
- set不能当hash用
业务场景1
用户权限校验,业务权限校验。
![](https://img.haomeiwen.com/i20155953/38da02e16c2414f9.png)
解决方案
- 依赖set数据不重复的特点,完成数据快速过滤与快速查询
- 根据用户id获取用户角色
- 根据用户的角色获取用户的所有的权限
- 根据用户的角色获取用户的个人信息
业务场景2
统计公司某网站的PV(访问量),UV(独立访客),IP(独立IP)。
解决方案
- 使用set类型的特征对数据去重,记录访问数据。
- 建立string类型数据,使用incr统计访问量。
- 建立set类型,保存不同的cookie
- 建立set类型,保存不同的IP
业务场景3
黑白名单
反爬虫,设置拥有一定特征的用户才可以访问网站。
解决方案
- 基于公司战略指定规则
- 周期性满足黑名单规则的用户,添加到黑名单set中
- 周期性满足白名单规则的用户,添加到白名单set中
- 黑名单自动过滤IP,设备信息,用户特征
网友评论