谷粒商城【成神路】-【10】——缓存

目录

🧂1.引入缓存的优势

🥓2.哪些数据适合放入缓存 

🌭3.使用redis作为缓存组件 

🍿4.redis存在的问题 

🧈5.添加本地锁 

🥞6.添加分布式锁

🥚7.整合redisson作为分布式锁


🚗🚗🚗1.引入缓存的优势

为了系统性能的提升,我们一般都会将部分数据放入缓存中,加速访问。而db承担数据落盘工作。

🚗🚗🚗2.哪些数据适合放入缓存 

  • 即时性、数据—致性要求不高的
  • 访问量大且更新频率不高的数据(读多,写少)

🚗🚗🚗3.使用redis作为缓存组件 

先确保reidis正常启动

3.1配置redis

  • 1.引入依赖
        <!--redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
  • 2.配置reids信息
springredis:port: 6379host: ip地址password: XXX

3.2优化查询 

之前都是从数据库查询的,现在加入缓存逻辑~

 /*** 使用redis缓存*/@Overridepublic Map<String, List<Catalog2Vo>> getCatalogJson() {//1.加入缓存,缓存中存的数据全都是jsonString catalogJson = redisTemplate.opsForValue().get("catalogJson");if (StringUtils.isEmpty(catalogJson)) {//2.缓存中如果没有,再去数据库查找Map<String, List<Catalog2Vo>> catalogJsonFromDB = getCatalogJsonFromDB();//3.将数据库查到的数据,将对象转换为json存放到缓存String s = JSON.toJSONString(catalogJsonFromDB);redisTemplate.opsForValue().set("catalogJson", s);}//4.从缓存中获取,转换为我们指定的类型Map<String, List<Catalog2Vo>> result = JSON.parseObject(catalogJson, new TypeReference<Map<String, List<Catalog2Vo>>>() {});return result;}/*** 从数据库查询并封装的分类数据** @return*/public Map<String, List<Catalog2Vo>> getCatalogJsonFromDB() {/*** 优化:将数据库查询的多次变为一次*/List<CategoryEntity> selectList = baseMapper.selectList(null);//1.查出所有1级分类List<CategoryEntity> leve1Categorys = getParent_cid(selectList, 0L);//2.封装数据Map<String, List<Catalog2Vo>> parentCid = leve1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {//1.每一个一级分类List<CategoryEntity> categoryEntities = getParent_cid(selectList, v.getCatId());List<Catalog2Vo> catalog2Vos = null;if (categoryEntities != null) {catalog2Vos = categoryEntities.stream().map(l2 -> {Catalog2Vo vo = new Catalog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());//找二级分类的三级分类List<CategoryEntity> categoryEntities3 = getParent_cid(selectList, l2.getCatId());if (categoryEntities3 != null) {List<Catalog2Vo.Catalog3Vo> collect = categoryEntities3.stream().map(l3 -> {Catalog2Vo.Catalog3Vo catalog3Vo = new Catalog2Vo.Catalog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());return catalog3Vo;}).collect(Collectors.toList());vo.setCatalog3List(collect);}return vo;}).collect(Collectors.toList());}return catalog2Vos;}));return parentCid;}

3.3测试 

在本地第一次查询后,查看redis,发现redis已经存储

使用JMeter压测一下

🚗🚗🚗4.redis存在的问题 

4.1缓存穿透

  • 缓存穿透:  查询一个一定不存在的数据,由于缓存是不命中,将去查询数据库,但是数据库也无此记录,我们没有将这次查询的null写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义
  • 解决方案:null结果缓存,并加入短暂过期时间

4.2缓存雪崩 

  • 缓存雪崩: 在我们设置缓存时key采用了相同的过期时间,导致缓存在某一时刻同时失效,大量请求全部转发到DB, DB瞬时压力过重雪崩。
  • 解决方案:原有的失效时间基础上增加一个随机值,比如1-5分钟随机,这样每一个缓存的过期时间的重复率就会降低,就很难引发集体失效的事件。

4.3缓存击穿 

缓存击穿 :对于一些设置了过期时间的key,如果这些key可能会在某些时间点被超高并发地访问,是一种非常“热点”的数据。如果这个key在大量请求同时进来前正好失效,那么所有对这个key的数据查询都落到db,我们称为缓存击穿。

解决方案:枷锁    大量并发只让一个去查,其他人等待,查到以后释放锁,其他人获取到锁,先查缓存,就会有数据,不用去db

🚗🚗🚗5.添加本地锁 

若在缓存redis中没有查到,也去数据库查询,在查询数据库时,添加本地锁,在查之前,再次判断缓存reids中是否有数据,如果有,直接返回,如果没有,在查数据库,在查完数据库后,由于延迟原因,我们查完数据库时,将数据存放到缓存中,然后在释放锁

/*** 使用redis缓存*/@Overridepublic Map<String, List<Catalog2Vo>> getCatalogJson() {//1.使用redis缓存,存储为json对象String catalogJson = redisTemplate.opsForValue().get("catalogJson");//2.判断缓存中是否有if (StringUtils.isEmpty(catalogJson)) {System.out.println("缓存没有命中~查询数据库...");//3.如果缓存中没有,从数据库Map<String, List<Catalog2Vo>> catalogJsonFromDB = getCatalogJsonFromDB();return catalogJsonFromDB;}System.out.println("缓存命中....直接返回");//5.如果缓存中有,转换为我们需要的类型Map<String, List<Catalog2Vo>> result = JSON.parseObject(catalogJson, new TypeReference<Map<String, List<Catalog2Vo>>>() {});return result;}/*** 从数据库查询并封装的分类数据** @return*/public Map<String, List<Catalog2Vo>> getCatalogJsonFromDB() {/*** 优化:将数据库查询的多次变为一次*///TODO 本地锁,在分布式下,必须使用分布式锁//加锁,防止缓存击穿,使用同一把锁synchronized (this) {//加所以后,我们还要去缓存中确定一次,如果缓存中没有,才继续查数据库String catalogJson = redisTemplate.opsForValue().get("catalogJson");if (!StringUtils.isEmpty(catalogJson)) {//如果缓存中有,从缓存中获取Map<String, List<Catalog2Vo>> result = JSON.parseObject(catalogJson, new TypeReference<Map<String, List<Catalog2Vo>>>() {});return result;}System.out.println("查询了数据库~");List<CategoryEntity> selectList = baseMapper.selectList(null);//1.查出所有1级分类List<CategoryEntity> leve1Categorys = getParent_cid(selectList, 0L);//2.封装数据Map<String, List<Catalog2Vo>> parentCid = leve1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {//1.每一个一级分类List<CategoryEntity> categoryEntities = getParent_cid(selectList, v.getCatId());List<Catalog2Vo> catalog2Vos = null;if (categoryEntities != null) {catalog2Vos = categoryEntities.stream().map(l2 -> {Catalog2Vo vo = new Catalog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());//找二级分类的三级分类List<CategoryEntity> categoryEntities3 = getParent_cid(selectList, l2.getCatId());if (categoryEntities3 != null) {List<Catalog2Vo.Catalog3Vo> collect = categoryEntities3.stream().map(l3 -> {Catalog2Vo.Catalog3Vo catalog3Vo = new Catalog2Vo.Catalog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());return catalog3Vo;}).collect(Collectors.toList());vo.setCatalog3List(collect);}return vo;}).collect(Collectors.toList());}return catalog2Vos;}));//4.将从数据库中获取的数据,转换为Json存储到redisString s = JSON.toJSONString(parentCid);//设置缓存时间,方式雪崩redisTemplate.opsForValue().set("catalogJson", s, 1, TimeUnit.DAYS);return parentCid;}}

🚗🚗🚗6.添加分布式锁

使用分布式锁 步骤

  • 1.先去redis中设置一个key,为了保持原子性,同时设置过期时间
  • 2.判断是否设置成功,成功则继续业务操作,失败则自选再次获取
  • 3.执行业务之后,需要删除key释放锁,为了保持原子性,使用lua脚本
 /*** 使用redis缓存*/@Overridepublic Map<String, List<Catalog2Vo>> getCatalogJson() {//1.使用redis缓存,存储为json对象String catalogJson = redisTemplate.opsForValue().get("catalogJson");//2.判断缓存中是否有if (StringUtils.isEmpty(catalogJson)) {System.out.println("缓存没有命中~查询数据库...");//3.如果缓存中没有,从数据库Map<String, List<Catalog2Vo>> catalogJsonFromDB = getCatalogJsonFromDBWithRedisLock();return catalogJsonFromDB;}System.out.println("缓存命中....直接返回");//5.如果缓存中有,转换为我们需要的类型Map<String, List<Catalog2Vo>> result = JSON.parseObject(catalogJson, new TypeReference<Map<String, List<Catalog2Vo>>>() {});return result;}/*** 从数据库中获取数据,使用分布所锁** @return*/public Map<String, List<Catalog2Vo>> getCatalogJsonFromDBWithRedisLock() {//1.占分布式锁,去redis占位String uuid = UUID.randomUUID().toString();//2.设置过期时间,必须和加锁同步Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid, 300, TimeUnit.SECONDS);if (lock) {System.out.println("获取分布式锁成功..." + redisTemplate.opsForValue().get("lock"));//加锁成功...执行业务Map<String, List<Catalog2Vo>> dataFromDb;try {dataFromDb = getDataFromDb();} finally {String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";//删除锁,原子性Long lock1 = redisTemplate.execute(new DefaultRedisScript<Long>(script, Long.class), Arrays.asList("lock"), uuid);}return getDataFromDb();} else {//加锁失败...重试//休眠100毫秒try {Thread.sleep(2000);} catch (InterruptedException e) {}return getCatalogJsonFromDBWithRedisLock();//自旋方式}}/*** 提起方法,从数据库中获取** @return*/private Map<String, List<Catalog2Vo>> getDataFromDb() {//加所以后,我们还要去缓存中确定一次,如果缓存中没有,才继续查数据库String catalogJson = redisTemplate.opsForValue().get("catalogJson");if (!StringUtils.isEmpty(catalogJson)) {//如果缓存中有,从缓存中获取Map<String, List<Catalog2Vo>> result = JSON.parseObject(catalogJson, new TypeReference<Map<String, List<Catalog2Vo>>>() {});return result;}System.out.println("查询了数据库~");List<CategoryEntity> selectList = baseMapper.selectList(null);//1.查出所有1级分类List<CategoryEntity> leve1Categorys = getParent_cid(selectList, 0L);//2.封装数据Map<String, List<Catalog2Vo>> parentCid = leve1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {//1.每一个一级分类List<CategoryEntity> categoryEntities = getParent_cid(selectList, v.getCatId());List<Catalog2Vo> catalog2Vos = null;if (categoryEntities != null) {catalog2Vos = categoryEntities.stream().map(l2 -> {Catalog2Vo vo = new Catalog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());//找二级分类的三级分类List<CategoryEntity> categoryEntities3 = getParent_cid(selectList, l2.getCatId());if (categoryEntities3 != null) {List<Catalog2Vo.Catalog3Vo> collect = categoryEntities3.stream().map(l3 -> {Catalog2Vo.Catalog3Vo catalog3Vo = new Catalog2Vo.Catalog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());return catalog3Vo;}).collect(Collectors.toList());vo.setCatalog3List(collect);}return vo;}).collect(Collectors.toList());}return catalog2Vos;}));//4.将从数据库中获取的数据,转换为Json存储到redisString s = JSON.toJSONString(parentCid);//设置缓存时间,方式雪崩redisTemplate.opsForValue().set("catalogJson", s, 1, TimeUnit.DAYS);return parentCid;}

🚗🚗🚗7.整合redisson作为分布式锁

7.1引入依赖

       <!--redisson作为所有分布式锁--><dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.12.0</version></dependency>

7.2程序化配置

在配置地址时,一定要添加reds://

@Configuration
public class MyRedissonConfig {@Bean(destroyMethod = "shutdown")public RedissonClient redissonClient() throws IOException {//1.创建配置Config config = new Config();config.useSingleServer().setAddress("redis://192.168.20.130:6379");//2.根据config创建redisson实例RedissonClient redissonClient = Redisson.create(config);return redissonClient;}
}

7.3实例解析

  • 1.锁的自动续期,如果业务超长,运行期间自动给锁续上新的30秒,不用担心业务超长,所自动过期被删除掉
  • 2.加锁的业务只要运行完成,就不会给当前锁续期,即使不手动解锁,锁默认在30秒后自动删除
 @AutowiredRedissonClient redissonClient;@ResponseBody@GetMapping("/hello")public String hello() {//1.设置redis的key获取一把锁,只要名字相同,就是同一把锁RLock lock = redissonClient.getLock("my-lock");//2.手动枷锁lock.lock();//阻塞式等待,默认30秒try {//3.执行业务System.out.println("枷锁成功!" + Thread.currentThread().getName());//4.模拟业务消耗时间Thread.sleep(20000);} catch (Exception e) {} finally {//3.释放锁System.out.println("释放锁~"+Thread.currentThread().getName());lock.unlock();//不删除,默认30秒后过期,自动删除}return "hello";}
  • 3.如果我们传递了锁的超时时间,就发送给redis执行脚本,进行占锁,默认超时就是我们指定的时间
  • 4.如果未指定锁的超时时间,就使用30*1000【LockWatchingTimeOut看门狗默认时间】 
    • 只要占锁成功,就会启动一个定时任务【重新给锁设置过期时间,新的过期时间就是看门狗的默认时间】,每隔(【LockWatchingTimeOut看门狗默认时间】/3)折磨长时间自动续期;

7.4读写锁 

  • 1.保证一定能读到最新数据,修改期间,写锁是一个排他锁,读锁是一个共享锁
  • 2.读+读:相当于并发,在redis中记录好,都会读取成功
  • 3.写+读:等待写锁释放
  • 4.写+写:阻塞方式
  • 5.读+写:有读锁,写也需要等待
 @GetMapping("/write")@ResponseBodypublic String write() {RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("readWrite-lock");String word = "";RLock rLock = readWriteLock.writeLock();try {//该数据加写锁,读数据加读锁rLock.lock();word = UUID.randomUUID().toString();Thread.sleep(3000);redisTemplate.opsForValue().set("writeValue", word);} catch (InterruptedException e) {e.printStackTrace();} finally {rLock.unlock();}return word;}/*** 读写锁* @return*/@GetMapping("/read")@ResponseBodypublic String read() {String word = "";RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("readWrite-lock");//加读锁RLock rLock = readWriteLock.readLock();rLock.lock();try {word = redisTemplate.opsForValue().get("writeValue");} catch (Exception e) {e.printStackTrace();}finally {rLock.unlock();}return word;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/737216.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

学习大数据,所必需的java基础(完结篇)

文章目录 注解注解的介绍注解的定义以及属性的定义格式注解的使用注解解析的方法----AnnotatedElement接口&#xff08;扩展&#xff09; 元注解注解之在此注解 注解 注解的介绍 1.jdk1.5版本的新特性 — 一个引用数据类型 和类&#xff0c;接口&#xff0c;枚举是同一个层次…

【教程】使用小米换机来迁移数据

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhang.cn] 1、在新旧手机上都下载安装小米换机app&#xff1a;小米换机-小米应用商店 2、在新手机上&#xff0c;选择旧手机类型 3、授予权限 4、在旧手机上&#xff0c;授予权限 4、输入锁屏密码 5、选择发现的新手机 6、等…

EMC整改

EMC包括EMI和EMS&#xff0c;其中EMI由辐射干扰RE、传导干扰CE、谐波电流Harmonics、闪烁Flicker组成&#xff0c;EMS由静电抗扰度ESD、电快速瞬态脉冲群EFT、电压跌落DIP、传导抗扰度CS、辐射抗扰度RS、浪涌抗扰度surge、工频磁场抗扰度PMS。新产品生产出来但凡要做认证&#…

鸿蒙Harmony应用开发—ArkTS声明式开发(基础手势:NavDestination)

作为子页面的根容器&#xff0c;用于显示Navigation的内容区。 说明&#xff1a; 该组件从API Version 9开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 该组件从API Version 11开始默认支持安全区避让特性(默认值为&#xff1a;expandSaf…

嘿!AI 编码新玩法上线!

随着 AI 智能浪潮到来&#xff0c;AI 编码助手成为越来越多开发者的必备工具&#xff0c;将开发者从繁重的编码工作中解放出来&#xff0c;极大地提高了编程效率&#xff0c;帮助开发者实现更快、更好的代码编写。 通义灵码正是这样一款基于阿里云通义代码大模型打造的智能编码…

Java学习笔记------拼图游戏

图形化界面GUI GUI&#xff1a;Graphical User Interface&#xff08;图像用户接口&#xff09;&#xff0c;指采用图形化的方式显示操作界面 两套体系&#xff1a;AWT包中和Swing包中 组件 JFrame&#xff1a;最外层的窗体 JMenuBar&#xff1a;最上层菜单 JLaber&#…

【蓝桥杯】节省时间

一、对于string类型变量的连接&#xff0c;可以直接用“”或者“”来进行字符串的直接连接 string a"1"; string b"2"; string c; cab"12"; string操作符两边既可以都是string类型&#xff0c;也可是string与char类型 注意&#xff1a; (1)“”…

rj45网络变压器作用

WE-MIDC网络变压器是Wrth Elektronik公司生产的一种专业级别的网络变压器&#xff0c;也称为Wrth Elektronik网络变压器。它通常用于工业自动化、医疗保健、军事和航空航天等领域的网络通信应用。 WE-MIDC网络变压器具有以下特点&#xff1a; 电气隔离&#xff1a;WE-MIDC网络变…

一次压测经验过程的经验记录

开篇说明 如果在这里获得过启发和思考&#xff0c;希望点赞支持&#xff01;对于内容有不同的看法欢迎来信交流。 技术栈 >> java 邮箱 >> 15673219519163.com 描述 通常对于QPS较高的web应用程序在开发完成后&#xff0c;除了功能测试之外还需要做一轮压力测试…

工厂模式~

1. 简单工厂 它的主要特点是需要在工厂类中做判断&#xff0c;从而创造相应的产品。当增加新的产品时&#xff0c;就需要修改工厂类。在简单工厂模式中&#xff0c;增加新的产品需要修改工厂类&#xff0c;这违反了开闭原则&#xff08;对扩展开放&#xff0c;对修改封闭&#…

计算机网络-H3C 交换机FTP与TFTP

一、FTP与TFTP概述 FTP&#xff0c;全称为File Transfer Protocol&#xff0c;即文件传输协议&#xff0c;是一种用于在Internet上进行文件传输的应用层协议。FTP是基于客户端-服务器架构设计的&#xff0c;并使用TCP作为其传输层协议。TFTP (Trivial File Transfer Protocol) …

java继承,接口,抽象类

目录 目录 1 继承的含义 2 继承的好处 3使类与类之间产生了关系。 看这里继承-------我的理解 代码部分 接口 代码 抽象类 代码 各位友友们大家好呀&#x1f60a;&#xff01; 今天让我们继续回顾java&#xff0c;看看java中的抽象类以及接口继承是什么&#x1f914…

如何精确计算 π ?

如何精确计算 π &#xff1f; 01 原本是要回顾一下第六章内容&#xff0c;也就是“间隔性重复”。但我已经迫不及待&#xff0c;想要知道如何精确计算 π &#xff0c;因此&#xff0c;我们快走一步&#xff0c;来探讨一下 π 的计算。 对于 π 的计算&#xff0c;我从学校时…

Python基础学习(5)流程控制

文章目录 一. 程序三大执行流程二. 分支结构1.单分支结构(if)2.双分支结构(if..else)3.多分支结构(if..elif..else) 二,缩进(tab键)三,循环结构1.while循环2.for循环①遍历字典 五.break&#xff0c;continue和pass语句1.break&#xff0c;continue2.pass Python基础学习(1)基本…

蓝桥杯(日期问题纯暴力)

纯纯暴力&#xff0c;写的想吐&#xff0c;玛德服了。 但是复习了vector去重方法&#xff0c;日期的合法性判断。 #include <iostream> #include <vector> #include <cstring> #include <algorithm>using namespace std; vector<int> res; st…

浪潮信息数据中心管理平台InManage升级发布 新增三大场景功能

在AIGC应用日益广泛的当下&#xff0c;浪潮信息聚焦AIGC在数据中心运维管理中面临的难题&#xff0c;进一步通过技术创新升级功能及体验&#xff0c;为AIGC的高效应用创造了良好的基础。近日&#xff0c;浪潮信息数据中心管理平台InManage升级发布&#xff0c;新增资产数字化管…

强化学习(一)

#! https://zhuanlan.zhihu.com/p/686235471 深度强化学习&#xff08;一&#xff09;&#xff08;基础概念&#xff09; 一.马尔可夫决策过程 Agent:智能体&#xff0c;动作或决策对象 Environment: 与智能体交互的对象&#xff0c;可随时间变化 State&#xff1a;对某一…

智慧城市的未来:利用数字孪生技术推动智慧城市的智能化升级

目录 一、引言 二、数字孪生技术概述 三、数字孪生技术在智慧城市中的应用 1、城市规划与建设 2、城市管理与运营 3、公共服务与民生改善 4、应急管理与灾害防控 四、数字孪生技术推动智慧城市的智能化升级的价值 1、提高城市管理的智能化水平 2、优化城市资源配置 …

Allegro许可与其他软件的兼容性优势

解锁企业软件管理新篇章&#xff0c;Allegro许可与其他软件的兼容性优势 在数字化经济的时代&#xff0c;企业越来越依赖于各种软件应用来提升运营效率和管理水平。然而&#xff0c;企业在选择和使用软件时&#xff0c;经常会遇到与其他软件不兼容的问题&#xff0c;导致数据无…

答题pk小程序源码技术大解析

答题pk小程序源码解析 在数字化时代&#xff0c;小程序因其便捷性、即用性而受到广泛欢迎。其中&#xff0c;答题pk小程序更是成为了一种寓教于乐的现象。它不仅为用户提供了趣味性的知识竞技平台&#xff0c;还为企业、教育机构等提供了互动营销和知识传播的新途径。本文将对…