当前位置: 首页 > news >正文

【黑马点评】(二)缓存

(一) 什么是缓存

在这里插入图片描述
在这里插入图片描述

(二)添加商户缓存

在这里插入图片描述
在这里插入图片描述
控制层

 /*** 根据id查询商铺信息* @param id 商铺id* @return 商铺详情数据*/@GetMapping("/{id}")public Result queryShopById(@PathVariable("id") Long id) {return shopService.queryById(id);}

service层

public interface IShopService extends IService<Shop> {Result queryById(Long id);
}@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService{@Resourceprivate StringRedisTemplate stringRedisTemplate;@Overridepublic Result queryById(Long id) {String key = CACHE_SHOP_KEY + id;// 1.从redis查询缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isNotBlank(shopJson)){// 3.存在,直接返回Shop shop = JSONUtil.toBean(shopJson, Shop.class);return Result.ok(shop);}// 4.不存在,查询数据库Shop shop = getById(id);if(shop == null){return Result.fail("店铺不存在:!");}// 5.存在, 存入redisstringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop));return Result.ok(shop);}
}

测试效果:
在这里插入图片描述

(三)缓存练习题分析

在这里插入图片描述
自行实现即可,不难

(四)缓存更新策略

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

先删除缓存,在操作数据库的正常情况(缓存 数据库 一开始都是10)
在这里插入图片描述
产生不一致情况:
在这里插入图片描述

先操作数据库,在删除缓存的正常情况:
在这里插入图片描述
产生不一致情况:
在这里插入图片描述

方案二先操作数据库,在删除缓存比方案一概率更低,因为需要线程1恰好查询缓存的时候缓存是失效的,同时在准备写入缓存的很短的时间需要有线程二进来更新数据库,删除缓存,需要这两个条件同时成立。

在这里插入图片描述

(五)实现商铺缓存与数据库的双写一致

在这里插入图片描述

这里更新接口字段需要去掉updatetime和createtime,因为会报错,后续在找办法解决,子需要自定义配置时间就行,多个配置类。

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: raw timestamp (1642066339000) not allowed for java.time.LocalDateTime: need additional information such as an offset or time-zone (see class Javadocs); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: raw timestamp (1642066339000) not allowed for java.time.LocalDateTime: need additional information such as an offset or time-zone (see class Javadocs)
at [Source: (PushbackInputStream); line: 7, column: 19] (through reference chain: com.hmdp.entity.Shop[“updateTime”])

在这里插入图片描述

当执行更新店铺时,会更新数据库,在删除缓存

在这里插入图片描述

当再次查询时数据库时,会自动更新缓存
在这里插入图片描述

修改代码如下,添加缓存时候设置过期时间,然后在更新数据库时删除缓存。

    @Overridepublic Result queryById(Long id) {String key = CACHE_SHOP_KEY + id;// 1.从redis查询缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isNotBlank(shopJson)){// 3.存在,直接返回Shop shop = JSONUtil.toBean(shopJson, Shop.class);return Result.ok(shop);}// 4.不存在,查询数据库Shop shop = getById(id);if(shop == null){return Result.fail("店铺不存在:!");}// 5.存在, 存入redisstringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop),CACHE_SHOP_TTL, TimeUnit.MINUTES);return Result.ok(shop);}@Override@Transactionalpublic Result update(Shop shop) {Long id = shop.getId();if(id == null){return Result.fail("店铺id不能为空");}// 1. 更新数据库updateById(shop);// 2. 删除缓存stringRedisTemplate.delete(CACHE_SHOP_KEY + id);return Result.ok();}

(六)缓存穿透的解决思路

在这里插入图片描述

(七)编码解决商品查询的缓存穿透问题

在这里插入图片描述

    @Overridepublic Result queryById(Long id) {String key = CACHE_SHOP_KEY + id;// 1.从redis查询缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isNotBlank(shopJson)){// 3.存在,直接返回Shop shop = JSONUtil.toBean(shopJson, Shop.class);return Result.ok(shop);}// 判断命中的是否是空值if(shopJson != null){// 返回错误信息return Result.fail("店铺不存在");}// 4.不存在,查询数据库Shop shop = getById(id);if(shop == null){// 空值写入redis 2分钟ttlstringRedisTemplate.opsForValue().set(key,"",CACHE_NULL_TTL, TimeUnit.MINUTES);return Result.fail("店铺信息不存在!");}// 5.存在, 存入redisstringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop),CACHE_SHOP_TTL, TimeUnit.MINUTES);return Result.ok(shop);}

第一次客户端发起请求查询店铺为0的数据时,应该返回店铺信息不存在,同时将空值写入缓存

在这里插入图片描述

此时redis中应该存储0的空值
在这里插入图片描述
当再次查询时,不会在请求到数据库当中,会查询缓存返回。

在这里插入图片描述

在这里插入图片描述

(八)缓存雪崩问题以及解决思路

在这里插入图片描述

(九)缓存击穿问题以及解决方案

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

(十)利用互斥锁解决缓存击穿问题

在这里插入图片描述
在这里插入图片描述
封装保存缓存穿透的代码以及封装缓存击穿的代码,实现setnx方法以及释放锁

         // 缓存穿透
//        Shop shop = queryWithPassThrough(id);// 互斥锁解决缓存击穿Shop shop = queryWithMutex(id);if(shop == null){return Result.fail("店铺不存在");}// 返回return Result.ok(shop);}public Shop queryWithPassThrough(Long id){String key = CACHE_SHOP_KEY + id;// 1.从redis查询缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isNotBlank(shopJson)){// 3.存在,直接返回return JSONUtil.toBean(shopJson, Shop.class);}// 判断命中的是否是空值if(shopJson != null){// 返回错误信息return null;}// 4.不存在,查询数据库Shop shop = getById(id);if(shop == null){// 空值写入redis 2分钟ttlstringRedisTemplate.opsForValue().set(key,"",CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}// 5.存在, 存入redisstringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop),CACHE_SHOP_TTL, TimeUnit.MINUTES);return shop;}public Shop queryWithMutex(Long id){String key = CACHE_SHOP_KEY + id;// 1.从redis查询缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isNotBlank(shopJson)){// 3.存在,直接返回return JSONUtil.toBean(shopJson, Shop.class);}// 判断命中的是否是空值if(shopJson != null){// 返回错误信息return null;}// 4实现缓存重建 (ctrl + alt + T 快捷try-catch)//  4.1 获取互斥锁String lockKey = "lock:shop" + id;Shop shop = null;try {boolean isLock = tryLock(lockKey);//  4.2 判断是否成功if(!isLock){//  4.3 失败则休眠重试Thread.sleep(50);return queryWithMutex(id);}//  4.4 成功,根据id查询数据库shop = getById(id);// 模拟重建延时Thread.sleep(200);// 5 不存在,返回错误if(shop == null){// 空值写入redis 2分钟ttlstringRedisTemplate.opsForValue().set(key,"",CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}// 6.存在, 存入redisstringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop),CACHE_SHOP_TTL, TimeUnit.MINUTES);} catch (InterruptedException e) {e.printStackTrace();} finally {// 7.释放互斥锁unlock(key);}// 8.返回return shop;}private boolean tryLock(String key){Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", 10, TimeUnit.SECONDS);return BooleanUtil.isTrue(flag);}private void unlock(String key){stringRedisTemplate.delete(key);}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
自己测试的指标,为啥还有一些请求失败的呢,不过确实只查了一次数据库,缓存中也被更新了
在这里插入图片描述

(十)利用逻辑过期解决缓存击穿问题

在这里插入图片描述
缓存预热

 public void saveShop2Redis(Long id, Long expireSeconds){// 1. 查询店铺数据Shop shop = getById(id);// 2. 封装逻辑过期时间RedisData redisData = new RedisData();redisData.setData(shop);redisData.setExpireTime(LocalDateTime.now().plusSeconds(expireSeconds));// 3. 写入redisstringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY + id, JSONUtil.toJsonStr(redisData));}
@Data
public class RedisData {private LocalDateTime expireTime;private Object data;
}

测试插入一条数据到redis

@SpringBootTest
class HmDianPingApplicationTests {@Resourceprivate ShopServiceImpl shopService;@Testvoid testSaveShop(){shopService.saveShop2Redis(1L, 10L);}
}

在这里插入图片描述
逻辑过期代码

    private final static ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);public Shop queryWithLogicalExpire(Long id){String key = CACHE_SHOP_KEY + id;// 1.从redis查询缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isBlank(shopJson)){// 3.未命中return null;}// 命中需要判断过期时间RedisData redisData = JSONUtil.toBean(shopJson, RedisData.class);JSONObject data = (JSONObject) redisData.getData();Shop shop = JSONUtil.toBean(data, Shop.class);LocalDateTime expireTime = redisData.getExpireTime();if(expireTime.isAfter(LocalDateTime.now())){// 过期时间是否在当前时间之后// 未过期,直接返回数据return shop;}// 过期,重建缓存String lockKey = LOCK_SHOP_KEY + id;boolean isLock = tryLock(lockKey);if(isLock){CACHE_REBUILD_EXECUTOR.submit(() -> {// 重建缓存try {this.saveShop2Redis(id, 20L);} catch (Exception e) {throw new RuntimeException(e);}finally {// 释放锁unlock(lockKey);}});}return shop;}
    public void saveShop2Redis(Long id, Long expireSeconds) throws InterruptedException {// 1. 查询店铺数据Shop shop = getById(id);// 模拟延迟Thread.sleep(200);// 2. 封装逻辑过期时间RedisData redisData = new RedisData();redisData.setData(shop);redisData.setExpireTime(LocalDateTime.now().plusSeconds(expireSeconds));// 3. 写入redisstringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY + id, JSONUtil.toJsonStr(redisData));}

将数据库id为1的改为108茶餐厅,redis之前预热的是105茶餐厅。进行压测

在这里插入图片描述

(十二)封装Redis工具类

在这里插入图片描述

@Slf4j
@Component
public class CacheUtil {private final StringRedisTemplate stringRedisTemplate;public CacheUtil(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}public void set(String key, Object value, Long time, TimeUnit unit){stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, unit);}public void setWithLogicalExpire(String key, Object value, Long time, TimeUnit unit){RedisData redisData = new RedisData();redisData.setData(value);redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData), time, unit);}public <R, ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type, Function<ID,R> dbFallback,Long time, TimeUnit unit){String key = keyPrefix + id;// 1.从redis查询缓存String json = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isNotBlank(json)){// 3.存在,直接返回return JSONUtil.toBean(json, type);}// 判断命中的是否是空值if(json != null){// 返回错误信息return null;}// 4.不存在,查询数据库R r = dbFallback.apply(id);if(r == null){// 空值写入redis 2分钟ttlstringRedisTemplate.opsForValue().set(key,"",CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}// 5.存在, 存入redisthis.set(key,r,time,unit);return r;}private final static ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);public <R, ID> R queryWithLogicalExpire(String keyPrefix, ID id, Class<R> type, Function<ID,R> dbFallback,Long time, TimeUnit unit){String key = keyPrefix + id;// 1.从redis查询缓存String json = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if(StrUtil.isBlank(json)){// 3.未命中return null;}// 命中需要判断过期时间RedisData redisData = JSONUtil.toBean(json, RedisData.class);R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);LocalDateTime expireTime = redisData.getExpireTime();if(expireTime.isAfter(LocalDateTime.now())){// 过期时间是否在当前时间之后// 未过期,直接返回数据return r;}// 过期,重建缓存String lockKey = LOCK_SHOP_KEY + id;boolean isLock = tryLock(lockKey);if(isLock){CACHE_REBUILD_EXECUTOR.submit(() -> {// 重建缓存try {// 查询数据库R r1 = dbFallback.apply(id);// 写入redisthis.setWithLogicalExpire(key, r1, time, unit);} catch (Exception e) {throw new RuntimeException(e);}finally {// 释放锁unlock(lockKey);}});}return r;}private boolean tryLock(String key){Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", 10, TimeUnit.SECONDS);return BooleanUtil.isTrue(flag);}private void unlock(String key){stringRedisTemplate.delete(key);}
}
http://www.lryc.cn/news/581598.html

相关文章:

  • 模块化汽车基础设施的正面交锋---区域架构与域架构
  • QT 菜单栏设计使用方法
  • brpc怎么解决C++静态初始化顺序难题的?
  • golang 协程 如何中断和恢复
  • React 各颜色转换方法、颜色值换算工具HEX、RGB/RGBA、HSL/HSLA、HSV、CMYK
  • 存储延时数据,帮你选数据库和缓存架构
  • 微前端架构在嵌入式BI中的集成实践与性能优化
  • 20250706-4-Docker 快速入门(上)-常用容器管理命令_笔记
  • Windows 11 Enterprise LTSC 转 IoT
  • 前端防抖Debounce如何实现
  • 小白成长之路-mysql数据基础(三)
  • stm32地址偏移:为什么相邻寄存器的地址偏移量0x04表示4个字节?
  • 【JS逆向基础】数据分析之XPATH
  • android 获取手机配对的蓝牙耳机的电量
  • 【PyTorch】PyTorch中torch.nn模块的池化层
  • 全能视频处理工具介绍说明
  • [shad-PS4] docs | 内核/系统服务 | HLE-高等级模拟
  • Spark流水线数据质量检查组件
  • UNet改进(16):稀疏注意力(Sparse Attention)在UNet中的应用与优化策略
  • Redis集群和 zookeeper 实现分布式锁的优势和劣势
  • 物联网实施与运维【路由器/网关配置】+智能楼道系统
  • python库 dateutil 库的各种案例的使用详解
  • 【Note】《Kafka: The Definitive Guide》第三章: Kafka 生产者深入解析:如何高效写入 Kafka 消息队列
  • Android studio在点击运行按钮时执行过程中输出的compileDebugKotlin 这个任务是由gradle执行的吗
  • 升级AGP(Android Gradle plugin)和gradle的版本可以提高kapt的执行速度吗
  • 【python】对纯二进制向量(仅包含 0 和 1,长度为 8 或 16)的检测和提取
  • 基于腾讯云开发与“人·事·财·物”架构理念的家政预约小程序设计与实现
  • 【Python练习】030. 编写一个函数,实现字符串的反转
  • Python 中 ffmpeg-python 库的详细使用
  • 一条 SQL 语句的内部执行流程详解(MySQL为例)