基于Redis的List实现特价商品列表功能
来源:脚本之家
2023-01-09 16:25:57
0浏览
收藏
本篇文章给大家分享《基于Redis的List实现特价商品列表功能》,覆盖了数据库的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
1、场景分析
淘宝京东的特价商品列表,
商品特点:
- 商品有限,并发量非常的大。
- 考虑分页
传统解决方案:数据库db,
但是在如此大的并发量的情况下,不可取。
一般会采用redis来处理。这些特价商品的数据不多,而且redis的list本身也支持分页。是天然处理这种列表的最佳选择解决方案。
2、分析
采用list数据,因为list数据结构有:lrange key 0 -1 可以进行数据的分页。
127.0.0.1:6379> lpush products p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 (integer) 10 127.0.0.1:6379> lrange products 0 1 1) "p10" 2) "p9" 127.0.0.1:6379> lrange products 2 3 1) "p8" 2) "p7" 127.0.0.1:6379> lrange products 4 5 1) "p6" 2) "p5"
3 、具体实现
淘宝,京东的热门商品在双11的时候,可能有100多w需要搞活动:程序需要5分钟对特价商品进行刷新。
3.1 ProductListService类
- 初始化的活动的商品信息100个(从数据库去查询)
@PostContrcut使用
- 查询产品列表信息
换算的分页的起始位置和结束位置
package com.example.service; import com.example.entity.Product; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * @Auther: 长颈鹿 * @Date: 2021/08/29/18:00 * @Description: */ @Service @Slf4j public class ProductListService { @Autowired private RedisTemplate redisTemplate; // 数据热加载 @PostConstruct public void initData(){ log.info("启动定时加载特价商品到redis的list中..."); new Thread(() -> runCourse()).start(); } public void runCourse() { while (true) { // 从数据库中查询出特价商品 List<product> productList = this.findProductsDB(); // 删除原来的特价商品 this.redisTemplate.delete("product:hot:list"); // 把特价商品添加到集合中 this.redisTemplate.opsForList().leftPushAll("product:hot:list", productList); try { // 每隔一分钟执行一次 Thread.sleep(1000 * 60); log.info("定时刷新特价商品...."); } catch (Exception ex) { ex.printStackTrace(); } } } /** * 数据库中查询特价商品 * * @return */ public List<product> findProductsDB() { //List<product> productList = productMapper.selectListHot(); List<product> productList = new ArrayList(); for (long i = 1; i <h3>3.2 商品的数据接口的定义和展示及分页</h3> <pre class="brush:java;"> package com.example.controller; import com.example.entity.Product; import com.example.service.ProductListService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @Auther: 长颈鹿 * @Date: 2021/08/29/18:04 * @Description: */ @RestController public class ProductListController { @Autowired private RedisTemplate redisTemplate; @Autowired private ProductListService productListService; @GetMapping("/findProducts") public List<product> findProducts(int pageNo, int pageSize) { // 从那个集合去查询 String key = "product:hot:list"; // 分页的开始结束的换算 if (pageNo productList = this.redisTemplate.opsForList().range(key, start, end); if (CollectionUtils.isEmpty(productList)) { //todo: 查询数据库,存在缓存击穿的情况,大量的并发请求进来,可能把数据库冲 productList = productListService.findProductsDB(); } return productList; } catch (Exception ex) { ex.printStackTrace(); return null; } } }</product>
3.3 定时任务
@Configuration // 主要用于标记配置类,兼备Component的效果。 @EnableScheduling // 开启定时任务 public class SaticScheduleTask { // 添加定时任务 @Scheduled(cron = "* 0/5 * * * ?") // 或直接指定时间间隔,例如:5秒 // @Scheduled(fixedRate=5000) private void configureTasks() { System.err.println("执行静态定时任务时间: " + LocalDateTime.now()); } }
4、解决商品列表存在的缓存击穿问题
4.1 如何引起的缓存击穿的情况
public void runCourse() { while (true) { // 从数据库中查询出特价商品 List<product> productList = this.findProductsDB(); // 删除原来的特价商品 this.redisTemplate.delete("product:hot:list"); // 把特价商品添加到集合中 需要时间 this.redisTemplate.opsForList().leftPushAll("product:hot:list", productList); try { // 每隔一分钟执行一遍 Thread.sleep(1000 * 60); log.info("定时刷新特价商品...."); } catch (Exception ex) { ex.printStackTrace(); } } }</product>
出现原因:
- 特价商品的数据更换需要时间,刚好特价商品还没有放入到redis缓存中。
- 查询特价商品的并发量非常大,可能程序还正在写入特价商品到缓存中,这时查询缓存根本没有数据,就会直接冲入数据库中去查询特价商品。可能造成数据库冲垮。这个就叫做:缓存击穿
4.2 解决方案
主从轮询
可以开辟两块redis的集合空间A和B。定时器在更新缓存的时候,先更新B缓存
,然后再更新A缓存
。
一定要按照特定顺序来处理。
package com.example.service; import com.example.entity.Product; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * @Auther: 长颈鹿 * @Date: 2021/08/29/18:00 * @Description: */ @Service @Slf4j public class ProductListService { @Autowired private RedisTemplate redisTemplate; // 数据热加载 @PostConstruct public void initData(){ log.info("启动定时加载特价商品到redis的list中..."); new Thread(() -> runCourse()).start(); } public void runCourse() { while (true) { // 从数据库中查询出特价商品 List<product> productList = this.findProductsDB(); // 删除原来的特价商品 this.redisTemplate.delete("product:hot:slave:list"); // 把特价商品添加到集合中 this.redisTemplate.opsForList().leftPushAll("product:hot:slave:list", productList);// 删除原来的特价商品 this.redisTemplate.delete("product:hot:master:list"); // 把特价商品添加到集合中 this.redisTemplate.opsForList().leftPushAll("product:hot:master:list", productList); // // 删除原来的特价商品 // this.redisTemplate.delete("product:hot:list"); // // 把特价商品添加到集合中 // this.redisTemplate.opsForList().leftPushAll("product:hot:list", productList); try { // 每隔一分钟执行一次 Thread.sleep(1000 * 60); log.info("定时刷新特价商品...."); } catch (Exception ex) { ex.printStackTrace(); } } } /** * 数据库中查询特价商品 * * @return */ public List<product> findProductsDB() { //List<product> productList = productMapper.selectListHot(); List<product> productList = new ArrayList(); for (long i = 1; i <pre class="brush:java;"> package com.example.controller; import com.example.entity.Product; import com.example.service.ProductListService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @Auther: 长颈鹿 * @Date: 2021/08/29/18:04 * @Description: */ @RestController public class ProductListController { @Autowired private RedisTemplate redisTemplate; @Autowired private ProductListService productListService; @GetMapping("/findProducts") public List<product> findProducts(int pageNo, int pageSize) { // 从那个集合去查询 String master_key = "product:hot:master:list"; String slave_key = "product:hot:slave:list"; String key = "product:hot:list"; // 分页的开始结束的换算 if (pageNo productList = this.redisTemplate.opsForList().range(master_key, start, end); // List<product> productList = this.redisTemplate.opsForList().range(key, start, end); if (CollectionUtils.isEmpty(productList)) { // todo: 查询数据库,存在缓存击穿的情况,大量的并发请求进来,可能把数据库冲 productList = this.redisTemplate.opsForList().range(slave_key, start, end); // productList = productListService.findProductsDB(); } return productList; } catch (Exception ex) { ex.printStackTrace(); return null; } } }</product></product>
好了,本文到此结束,带大家了解了《基于Redis的List实现特价商品列表功能》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!
版本声明
本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- 为何Redis使用跳表而非红黑树实现SortedSet

- 下一篇
- NestJS+Redis实现缓存步骤详解
评论列表
-
- 迷人的大叔
- 这篇技术贴真及时,太详细了,赞 ??,已加入收藏夹了,关注楼主了!希望楼主能多写数据库相关的文章。
- 2023-01-27 20:49:06
-
- 慈祥的狗
- 这篇文章出现的刚刚好,太全面了,受益颇多,已加入收藏夹了,关注师傅了!希望师傅能多写数据库相关的文章。
- 2023-01-26 01:37:11
-
- 高兴的学姐
- 感谢大佬分享,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢博主分享博文!
- 2023-01-24 05:41:12
-
- 火星上的蜜蜂
- 这篇文章内容出现的刚刚好,大佬加油!
- 2023-01-16 22:14:19
-
- 多情的唇膏
- 很详细,码住,感谢作者的这篇文章内容,我会继续支持!
- 2023-01-16 06:51:44
-
- 发嗲的咖啡
- 这篇博文真是及时雨啊,作者大大加油!
- 2023-01-15 23:37:28
-
- 尊敬的石头
- 很有用,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢博主分享文章内容!
- 2023-01-12 23:18:32
-
- 儒雅的咖啡豆
- 好细啊,码住,感谢大佬的这篇文章,我会继续支持!
- 2023-01-11 14:41:51
查看更多
最新文章
-
- 数据库 · Redis | 7小时前 |
- Redis与MySQL缓存同步方法解析
- 245浏览 收藏
-
- 数据库 · Redis | 8小时前 |
- Redis性能监控工具有哪些
- 124浏览 收藏
-
- 数据库 · Redis | 9小时前 |
- RedisList队列优化方法分享
- 378浏览 收藏
-
- 数据库 · Redis | 9小时前 |
- Redis位图实现用户签到优化方案
- 322浏览 收藏
-
- 数据库 · Redis | 18小时前 |
- Redis数据安全防护全攻略
- 112浏览 收藏
-
- 数据库 · Redis | 18小时前 |
- Redis哈希技巧与实战应用
- 204浏览 收藏
-
- 数据库 · Redis | 18小时前 |
- 扩展Redis集群节点的步骤与注意事项
- 163浏览 收藏
-
- 数据库 · Redis | 19小时前 |
- 高并发Redis优化技巧分享
- 147浏览 收藏
-
- 数据库 · Redis | 19小时前 |
- Redis主从复制故障排查指南
- 477浏览 收藏
-
- 数据库 · Redis | 23小时前 |
- Redis与HBase存储方案详解
- 414浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis与MongoDB缓存优化方法
- 193浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis安全配置更新操作教程
- 313浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 96次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 89次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 107次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 98次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 98次使用
查看更多
相关文章
-
- golang 如何获取文件夹下面的文件列表
- 2023-01-09 350浏览
-
- redis的list数据类型相关命令介绍及使用
- 2022-12-31 326浏览
-
- Redis 使用 List 实现消息队列的优缺点
- 2022-12-30 114浏览