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

缓存穿透 问题(缓存空对象)

文章目录

  • 1、缓存穿透
  • 2、缓存空对象
  • 3、AlbumInfoApiController --》getAlbumInfo()
  • 4、AlbumInfoServiceImpl --》getAlbumInfo()
  • 5、RedisConstant
  • 6、请求缓存不存在的数据

1、缓存穿透

在这里插入图片描述

2、缓存空对象

在这里插入图片描述

3、AlbumInfoApiController --》getAlbumInfo()

	@GetMapping("getAlbumInfo/{albumId}")public Result<AlbumInfo> getAlbumInfo(@PathVariable("albumId") Long albumId) {
//		try {
//			Thread.sleep(20);
//		} catch (InterruptedException e) {
//			throw new RuntimeException(e);
//		}AlbumInfo albumInfo = this.albumInfoService.getAlbumInfo(albumId);return Result.ok(albumInfo);}

4、AlbumInfoServiceImpl --》getAlbumInfo()

    public AlbumInfo getAlbumInfo(Long albumId) {// 1.先查询缓存,如果命中则直接返回AlbumInfo albumInfo = (AlbumInfo) this.redisTemplate.opsForValue().get(RedisConstant.ALBUM_INFO_PREFIX + albumId);if (albumInfo != null) {return albumInfo;}// 查询专辑albumInfo = this.getById(albumId);if (albumInfo != null) {// 根据专辑查询专辑标签值List<AlbumAttributeValue> albumAttributeValues = this.attributeValueMapper.selectList(new LambdaQueryWrapper<AlbumAttributeValue>().eq(AlbumAttributeValue::getAlbumId, albumId));albumInfo.setAlbumAttributeValueVoList(albumAttributeValues);}// 2.放入缓存if (albumInfo == null) {// 为了防止缓存穿透:数据即使为空也缓存,只是缓存时间不宜太长。this.redisTemplate.opsForValue().set(RedisConstant.ALBUM_INFO_PREFIX + albumId, albumInfo, RedisConstant.ALBUM_TEMPORARY_TIMEOUT, TimeUnit.SECONDS);}else {this.redisTemplate.opsForValue().set(RedisConstant.ALBUM_INFO_PREFIX + albumId, albumInfo, RedisConstant.CACHE_TIMEOUT, TimeUnit.SECONDS);}return albumInfo;}

在这里插入图片描述

5、RedisConstant

    public static final String ALBUM_INFO_PREFIX = "album:info:";// 商品如果在数据库中不存在那么会缓存一个空对象进去,但是这个对象是没有用的,所以这个对象的过期时间应该不能太长,// 如果太长会占用内存。// 定义变量,记录空对象的缓存过期时间public static final long ALBUM_TEMPORARY_TIMEOUT = 10 * 60;public static final long CACHE_TIMEOUT = 24 * 60 * 60;

6、请求缓存不存在的数据

http://127.0.0.1:8500/api/album/albumInfo/getAlbumInfo/9800

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

http://www.lryc.cn/news/441567.html

相关文章:

  • Vue3:mitt实现组件通信
  • 一个有个性的使用工具thefuck@Ubuntu
  • 【PyQt5】PyQt5桌面APP开发学习
  • JdbcTemplate常用方法一览AG网页参数绑定与数据寻址实操
  • 向日葵好用吗?4款稳定的远程控制软件推荐。
  • 【开源大模型生态9】百度的文心大模型
  • CSS的三种基本选择器
  • 排序学习笔记
  • 【nginx】缓存配置文件
  • 如何使用 maxwell 同步到 redis?
  • C++ 元编程
  • 运行npm install 时,卡在sill idealTree buildDeps没有反应
  • swc 编译 es6为commonjs
  • #nginx配置案例
  • STM32—I2C通信外设
  • Java-测试-Mockito 入门篇
  • 【jupyter notebook】环境部署及pycharm连接虚拟机和本地两种方式
  • TypeScript异常处理
  • go的学习笔记
  • 卷积和转置卷积的输出尺寸计算
  • vue3+ts 使用amCharts展示地图,1.点击左侧国家,可以高亮并放大右侧地图对应的国家。 2.展示数据球。
  • 汽车无钥匙启动功能工作原理
  • C++标准的一些特性记录:C++11的auto和decltype
  • 【Elasticsearch系列四】ELK Stack
  • 【新手上路】衡石分析平台使用手册-认证方式
  • 数字电路与逻辑设计-触发器功能测试及其应用
  • 【网站架构部署与优化】web服务与http协议
  • 【字符函数】strcpy函数(字符串复制函数)+strcat函数(字符串追加)+strcmp函数(字符串比较)【笔记】
  • codetop字符串刷题,刷穿地心!!不再畏惧!!暴打面试官!!
  • 快速体验Linux发行版:DistroSea详解与操作指南