当前位置:网站首页>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
边栏推荐
- Observer pattern
- Thesis framework of the opening report
- TCP详解(三)
- Distributed locks and three implementation methods
- Discussion on Service Commitment of Class Objects under Multithreading
- PMP WeChat group daily exercises
- Database implements distributed locks
- WebSocket Session is null
- 数据库实现分布式锁
- 立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
猜你喜欢

A brief introduction to the CheckBox component of the basic components of Flutter

接口测试关键技术

浅识Flutter 基本组件之CheckboxListTile组件

递归查询单表-单表树结构-(自用)

C#远程调试

TCP详解(三)

想从手工测试转岗自动化测试,需要学习哪些技能?

SQL injection Less54 (limited number of SQL injection + union injection)

Detailed explanation of TCP and UDP

JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
随机推荐
The Map Entry understanding and application
[C language] Three-pointed chess (classic solution + list diagram)
A brief introduction to the CheckboxListTile component of the basic components of Flutter
Ambiguous method call.both
品牌广告投放平台的中台化应用与实践
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
5. How does the SAP ABAP OData service support the $filter operation
浅识Flutter 基本组件之CheckboxListTile组件
【Cocos Creator 3.5】缓动系统停止所有动画
Detailed explanation of TCP and UDP
5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
CefSharp入门-winform
The els block moves the boundary to the right, and accelerates downward.
STM32 problem collection
MP使用时的几个常见报错
TCP详解(二)
Day32 LeetCode
CloudCompare&PCL 计算两个点云之间的重叠度
从滴滴罚款后数据治理思考
学习DAVID数据库(1)