Redis set数据类型命令使用及应用场景使用总结
转载请注明出处:
目录
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>
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
smembers key
示例:
127.0.0.1:6379> smembers action:10001 1) "101" 2) "102" 3) "103" 4) "104"
scard key
示例:
127.0.0.1:6379> scard action:10001
(integer) 4
127.0.0.1:6379>
应用场景:多少人点赞,多少人评论,以及多少人关注等
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>
应用场景:检查用户是否点过赞或访问过
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>
应用场景:随机抽取,并不从集合中移除随机抽取出的元素
spop key count
示例
127.0.0.1:6379> spop action:10001 1 1) "103" 127.0.0.1:6379>
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>
应用场景:共同的好友,爱好等
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>
应用场景:共同的好友,爱好等
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>
应用场景:获取集合间的所有元素
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>
应用场景:获取集合间的所有元素到新的集合
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>
应用场景:可能认识的人
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>
标签:
redis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-01-01 spring restTemplate 进行http请求的工具类封装
2021-01-01 idea 查看类的继承结构及其子类
2021-01-01 mysql too many connections 解决