当前位置:网站首页>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
边栏推荐
- 【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
- 刚出道“一战成名”,安全、舒适一个不落
- 12 Disk related commands
- some of my own thoughts
- 下载jar包的好地方
- What skills do I need to learn to move from manual testing to automated testing?
- C#远程调试
- Getting Started with CefSharp - winform
- 2022 Nioke Multi-School League Game 4 Solution
- Web container and IIS --- Middleware penetration method 1
猜你喜欢
品牌广告投放平台的中台化应用与实践
STM32 problem collection
[Compilation principle] Lexical analysis program design principle and implementation
endian mode
Mysql 45 study notes (twenty-five) MYSQL guarantees high availability
【C语言】预处理操作
Local area network computer hardware information collection tool
Mysql 45 study notes (twenty-four) MYSQL master-slave consistency
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
Chapter 9 SVM实践
随机推荐
从滴滴罚款后数据治理思考
Discussion on Service Commitment of Class Objects under Multithreading
【C语言】三子棋(经典解法+一览图)
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
A brief introduction to the CheckBox component of the basic components of Flutter
TCP详解(一)
Observer pattern
SIP Protocol Standard and Implementation Mechanism
What is a distributed lock?Three ways of implementing distributed lock
[Android] Room - Alternative to SQLite
Annotation usage meaning
数据库文件中的未分配的块和未使用的块的区别
自己的一些思考
els block to the left to move the conditional judgment
【CocosCreator 3.5】CocosCreator get network status
Mysql 45 study notes (23) How does MYSQL ensure that data is not lost
C# remote debugging
SQALE 是什么
识Flutter 基本组件之showTimePicker 方法
注解用法含义