当前位置:网站首页>Redis 使用 sorted set 做最新评论缓存
Redis 使用 sorted set 做最新评论缓存
2022-07-31 03:19:00 【Mar丶流年】
前言
sorted set 是有序集合。提供了权重这一属性,可以按照权重大小来获取数据。
实现
假设现在A商品有评论11,22,33,44,55,这里权重按照评论先后顺序从1开始累加(+1)
zadd a_comments 1 11
zadd a_comments 2 22
zadd a_comments 3 33
zadd a_comments 4 44
zadd a_comments 5 55
这时产生了A新的评论66。确定评论66的权重(总数+1)
# 获取总数 5,确定权重6
zcard a_comments
# 添加到redis
zadd a_comments 6 66
查看最新评论,以2条记录为一页。
# 获取当前数量
zcard a_comments
# 第一页 55,66
zrangebyscore a_comments 5 6
# 第二页 33,44
zrangebyscore a_comments 3 4
# 第三页 11,22
zrangebyscore a_comments 1 2
边栏推荐
- Local area network computer hardware information collection tool
- 分布式锁以及实现方式三种
- SocialFi 何以成就 Web3 去中心化社交未来
- some of my own thoughts
- IDEA comment report red solution
- Mysql 45讲学习笔记(二十四)MYSQL主从一致
- 12 Disk related commands
- Redis implements distributed locks
- Moxa NPort device flaw could expose critical infrastructure to devastating attack
- The Map Entry understanding and application
猜你喜欢
IDEA 注释报红解决
LeetCode simple problem to find the subsequence of length K with the largest sum
Chapter 9 SVM实践
立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
LeetCode中等题之分数加减运算
5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
IDEA comment report red solution
LeetCode简单题之两个数组间的距离值
SIP协议标准和实现机制
随机推荐
CloudCompare&PCL 计算两个点云之间的重叠度
[Godot][GDScript] 2D cave map randomly generated
SQL 面试用题(重点)
学习DAVID数据库(1)
Mysql 45讲学习笔记(二十五)MYSQL保证高可用
What is distributed and clustered?What is the difference?
大小端模式
[Compilation principle] Design principle and implementation of recursive descent parsing
下载jar包的好地方
Good place to download jar packages
Compile Hudi
Annotation usage meaning
数据库文件中的未分配的块和未使用的块的区别
web容器及IIS --- 中间件渗透方法1
BUG definition of SonarQube
IIR filter and FIR filter
Addition and Subtraction of Scores in LeetCode Medium Questions
Select the smoke test case, and make the first pass for the product package entering QA
els 方块向左移动条件判断
Difference between unallocated blocks and unused blocks in database files