redis 使用互斥锁或逻辑过期两种方案解决缓存击穿,和缓存穿透(用缓存空值 或布隆过滤器)的解决方案

server/2024/9/24 5:26:30/

缓存穿透
        缓存穿透是指在缓存中查找一个不存在的值,由于缓存一般不会存储这种无效的数据,所以每次查询都会落到数据库上,导致数据库压力增大,严重时可能会导致数据库宕机。
解决方案:
缓存空值 (本文此方案)
2 布隆过滤器
3 增强id的复杂度
4 做好数据的基础格式校验
5 做好热点参数的限流


缓存击穿
        缓存击穿是指一个被频繁访问(高并发访问并且缓存重建业务较复杂)的缓存键因为过期失效,同时又有大量并发请求访问此键,导致请求直接落到数据库或后端服务上,增加了系统的负载并可能导致系统崩溃 
解决方案
互斥锁
逻辑过期


1 前提先好做redis与springboot的集成,redisson的集成【用于加锁解锁】【本文用的redisson】
   另外用到了hutool的依赖


缓存对象封装的类,这里只是逻辑过期方案可以用上,你也可以自己改

/*** 决缓存击穿--(设置逻辑过期时间)*/
@Data
public class RedisData {//逻辑过期时间private LocalDateTime expireTime;//缓存实际的内容private Object data;
}

3 相关的常量

public class Constant {//缓存空值的ttl时间public static final Long CACHE_NULL_TTL = 2L;//缓存时间,单位程序里参数传public static final Long CACHE_NEWS_TTL = 10L;//缓存前缀,根据模块来public static final String CACHE_NEWS_KEY = "cache:news:";//锁-前缀,根据模块来public static final String LOCK_NEWS_KEY = "lock:news:";//持有锁的时间public static final Long LOCK_TTL = 10L;
}

缓存核心类

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;import static org.example.service_a.cache.Constant.CACHE_NULL_TTL;
import static org.example.service_a.cache.Constant.LOCK_NEWS_KEY;@Slf4j
@Component
//封装的将Java对象存进redis 的工具类
public class CacheClient {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Autowiredprivate RedissonClient redissonClient;// 定义线程池private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);AtomicInteger atomicInteger = new AtomicInteger();/*** 设置TTL过期时间set** @param key* @param value* @param time* @param unit*/public void set(String key, Object value, Long time, TimeUnit unit) {// 需要把value序列化为string类型String jsonStr = JSONUtil.toJsonStr(value);stringRedisTemplate.opsForValue().set(key, jsonStr, time, unit);}/*** 缓存穿透功能封装** @param id* @return*/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);}// 这里要先判断命中的是否是null,因为是null的话也是被上面逻辑判断为不存在// 这里要做缓存穿透处理,所以要对null多做一次判断,如果命中的是null则shopJson为""if ("".equals(Json)) {return null;}//4. 不存在,根据id查询数据库R r = dbFallback.apply(id);log.error("查询数据库次数 {}",atomicInteger.incrementAndGet());if (r == null) {//5. 不存在,将null写入redis,以便下次继续查询缓存时,如果还是查询空值可以直接返回false信息stringRedisTemplate.opsForValue().set(key, "", CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}//6. 存在,写入Redisthis.set(key, r, time, unit);//7. 返回return r;}/*** 解决缓存击穿--(互斥锁)* @param keyPrefix* @param id* @param type* @param dbFallback* @param time* @param unit* @return* @param <R>* @param <ID>*/public <R, ID> R queryWithMutex(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit unit){String key = keyPrefix + id;// 1.从redis查询商铺缓存String shopJson = stringRedisTemplate.opsForValue().get(key);// 2.判断是否存在if (StrUtil.isNotBlank(shopJson)) {// 3.存在,直接返回return JSONUtil.toBean(shopJson, type);}// 判断命中的是否是空值if (shopJson != null) {// 返回一个错误信息return null;}log.error("缓存重建----");// 4.实现缓存重建// 4.1.获取互斥锁String lockKey = LOCK_NEWS_KEY + id;R r = null;try {boolean isLock = tryLock(lockKey);// 4.2.判断是否获取成功if (!isLock) {// 4.3.获取锁失败,休眠并重试Thread.sleep(10);return queryWithMutex(keyPrefix, id, type, dbFallback, time, unit);}// 4.4.获取锁成功,根据id查询数据库r = dbFallback.apply(id);log.info("查询数据库次数 {}",atomicInteger.incrementAndGet());// 5.不存在,返回错误if (r == null) {// 将空值写入redisstringRedisTemplate.opsForValue().set(key, "", CACHE_NULL_TTL, TimeUnit.MINUTES);// 返回错误信息return null;}// 6.存在,写入redisthis.set(key, r, time, unit);} catch (InterruptedException e) {throw new RuntimeException(e);}finally {// 7.释放锁unLock(lockKey);}// 8.返回return r;}/*** --------------注意 key 没有加过期时间,会一直存在,只是 缓存的内容里有个字段,标识了过期的时间----------------* 设置逻辑过期set** @param key* @param value* @param time* @param chronoUnit*/public void setWithLogicExpire(String key, Object value, Long time, ChronoUnit chronoUnit) {// 设置逻辑过期RedisData redisData = new RedisData();redisData.setData(value);redisData.setExpireTime(LocalDateTime.now().plus(time, chronoUnit));// 需要把value序列化为string类型String jsonStr = JSONUtil.toJsonStr(redisData);stringRedisTemplate.opsForValue().set(key, jsonStr);}/*** 解决缓存击穿--(设置逻辑过期时间)方式* 1. 组合键名,从Redis查询缓存。* 2. 缓存不存在,直接返回(预设热点数据已预热)。* 3. 解析缓存内容,获取过期时间。* 4. 若未过期,直接返回数据。* 5. 已过期,执行缓存重建流程:* a. 尝试获取互斥锁。* b. 二次检查缓存是否已重建且未过期,若是则返回数据。* c. 成功获取锁,异步执行:* i. 查询数据库获取最新数据。* ii. 重新写入Redis缓存,附带新的逻辑过期时间。* iii. 最终释放锁。* 6. 未能获取锁,直接返回旧数据。** @param id* @return*/public <R, ID> R queryWithLogicalExpire(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, ChronoUnit chronoUnit) throws InterruptedException {String key = keyPrefix + id;//1. 从Redis中查询缓存String Json = stringRedisTemplate.opsForValue().get(key);//2. 判断是否存在if (StrUtil.isBlank(Json)) {//3. 不存在,直接返回(这里做的是热点key,先要预热,所以已经假定热点key已经在缓存中)return null;}//4. 存在,需要判断过期时间,需要先把json反序列化为对象RedisData redisData = JSONUtil.toBean(Json, RedisData.class);R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);LocalDateTime expireTime = redisData.getExpireTime();//5. 判断是否过期if (expireTime.isAfter(LocalDateTime.now())) {//5.1 未过期,直接返回店铺信息return r;}log.error("缓存内容已逻辑过期-----------{}",LocalDateTime.now());//5.2 已过期,需要缓存重建//6. 缓存重建//6.1 获取互斥锁String lockKey = LOCK_NEWS_KEY + id;//6.2 判断是否获取锁成功boolean isLock = tryLock(lockKey);if (isLock) {// 二次验证是否过期,防止多线程下出现缓存重建多次String Json2 = stringRedisTemplate.opsForValue().get(key);// 这里假定key存在,所以不做存在校验// 存在,需要判断过期时间,需要先把json反序列化为对象RedisData redisData2 = JSONUtil.toBean(Json2, RedisData.class);R r2 = JSONUtil.toBean((JSONObject) redisData2.getData(), type);LocalDateTime expireTime2 = redisData2.getExpireTime();if (expireTime2.isAfter(LocalDateTime.now())) {// 未过期,直接返回店铺信息return r2;}//6.3 成功,开启独立线程,实现缓存重建CACHE_REBUILD_EXECUTOR.submit(() -> {try {// 重建缓存,这里设置的值小一点,方便观察程序执行效果,实际开发应该设为30min// 查询数据库R apply = dbFallback.apply(id);log.info("查询数据库次数 {}",atomicInteger.incrementAndGet());// 写入redisthis.setWithLogicExpire(key, apply, time, chronoUnit);} catch (Exception e) {throw new RuntimeException(e);} finally {// 释放锁unLock(lockKey);}});}//7. 返回,如果没有获得互斥锁,会直接返回旧数据return r;}/*** 加锁* @param lockKey* @return*/private boolean tryLock(String lockKey) {RLock lock = redissonClient.getLock(lockKey);try {// 尝试获取锁,最多等待10秒,获取到锁后自动 LOCK_SHOP_TTL 0秒后解锁return lock.tryLock(10, Constant.LOCK_TTL, TimeUnit.SECONDS);} catch (Exception e) {Thread.currentThread().interrupt();// 重新抛出中断异常log.error("获取锁时发生中断异常", e);return false;}}/*** 解锁* @param lockKey*/private void unLock(String lockKey) {RLock lock = redissonClient.getLock(lockKey);lock.unlock(); // 解锁操作}}

缓存预热和测试

import cn.hutool.json.JSONUtil;
import org.example.common.AppResult;
import org.example.common.AppResultBuilder;
import org.example.service_a.cache.CacheClient;
import org.example.service_a.cache.RedisData;
import org.example.service_a.domain.News;
import org.example.service_a.service.NewsService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;import static org.example.service_a.cache.Constant.CACHE_NEWS_KEY;
import static org.example.service_a.cache.Constant.CACHE_NEWS_TTL;@RestController
@Validated()
@RequestMapping("/article")
public class News_Controller {@Autowiredprivate StringRedisTemplate redisTemplate;@Autowiredprivate CacheClient cacheClient;@Autowiredprivate NewsService newsService;@Autowiredprivate RedissonClient redissonClient;/*** @param id 编号*/@RequestMapping("/get/{id}")public AppResult<News> getGirl(@PathVariable("id") Long id) throws InterruptedException {//解决缓存穿透-------->News news = cacheClient.queryWithPassThrough(CACHE_NEWS_KEY, id, News.class,newsService::getById,CACHE_NEWS_TTL, TimeUnit.MINUTES);//(互斥锁)解决缓存击穿---------->
//        News news = cacheClient.queryWithMutex(CACHE_NEWS_KEY, id, News.class,
//                (x) -> {
//                    return newsService.getById(id);
//                }
//                , CACHE_NEWS_TTL, TimeUnit.MINUTES);//(设置逻辑过期时间)解决缓存击穿---------->
//        News news = cacheClient.queryWithLogicalExpire(
//                CACHE_NEWS_KEY,
//                id,
//                News.class,
//                (x)->{
//                    return newsService.getById(id);
//                },
//                CACHE_NEWS_TTL,
//                ChronoUnit.SECONDS);System.out.println("news = " + news);//判断返回值是否为空
//        if (news == null) {
//            return Result.fail("信息不存在");
//        }
//        //返回
//        return Result.ok(news);return AppResultBuilder.success(news);}/***缓存预热*/@PostConstruct()public void cache_init() {RLock lock = redissonClient.getLock("lock:cacheInit");lock.lock();try {List<News> list = newsService.list();redisTemplate.executePipelined(new SessionCallback<Object>() {HashMap<String, Object> objectObjectHashMap = new HashMap<>();@Overridepublic <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {list.forEach(news -> {//演示缓存击穿--逻辑过期 用这种方式
//                        RedisData redisData = new RedisData();
//                        redisData.setData(news);
//                        redisData.setExpireTime(LocalDateTime.now().plusSeconds(30));
//                        objectObjectHashMap.put(CACHE_NEWS_KEY +news.getId(),JSONUtil.toJsonStr(redisData));//演示缓存击穿--互斥锁 用这种方式objectObjectHashMap.put(CACHE_NEWS_KEY + news.getId(), JSONUtil.toJsonStr(news));});operations.opsForValue().multiSet((Map<? extends K, ? extends V>) objectObjectHashMap);return null;}});} catch (Exception e) {throw new RuntimeException(e);} finally {lock.unlock();}}
}


http://www.ppmy.cn/server/29645.html

相关文章

基于 Wireshark 分析 IP 协议

一、IP 协议 IP&#xff08;Internet Protocol&#xff09;协议是一种网络层协议&#xff0c;它用于在计算机网络中实现数据包的传输和路由。 IP协议的主要功能有&#xff1a; 1. 数据报格式&#xff1a;IP协议将待传输的数据分割成一个个数据包&#xff0c;每个数据包包含有…

GESP一级 - 第三章 - 第1节 - 标准输入输出

C标准输入输出 1. 初识C程序结构 1.1 main()函数 示例1: 一个简单的C程序 #include <iostream> using namespace std;int main() {cout << "你好,世界!" << endl;return 0; }示例2: 带有返回值的main()函数 #include <iostream> using na…

redis

reids 基本概念特性 安装命令lua 脚本EVALSHA命令例子lua脚本摘要参考 基本 概念 redis: REmote DIctionary Server key-value 存储系统&#xff0c;非关系型数据库。 开源的使用 ANSI C 语言编写 支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库&am…

c# winform快速建websocket服务器源码 wpf快速搭建websocket服务 c#简单建立websocket服务 websocket快速搭建

完整源码下载----->点击 随着互联网技术的飞速发展&#xff0c;实时交互和数据推送已成为众多应用的核心需求。传统的HTTP协议&#xff0c;基于请求-响应模型&#xff0c;无法满足现代Web应用对低延迟、双向通信的高标准要求。在此背景下&#xff0c;WebSocket协议应运而生…

使用 ORPO 微调 Llama 3

原文地址&#xff1a;https://towardsdatascience.com/fine-tune-llama-3-with-orpo-56cfab2f9ada 更便宜、更快的统一微调技术 2024 年 4 月 19 日 ORPO 是一种新的令人兴奋的微调技术&#xff0c;它将传统的监督微调和偏好校准阶段合并为一个过程。这减少了训练所需的计算…

Github2024-05-02开源项目日报 Top10

根据Github Trendings的统计,今日(2024-05-02统计)共有10个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量TypeScript项目4Rust项目2Vue项目1GDScript项目1SystemVerilog项目1Python项目1非开发语言项目1精心策划的编码面试准备材料 创建周期:2764 天…

uni-app scroll-view隐藏滚动条的小细节 兼容主流浏览器

开端 想写个横向滚动的列表适配浏览器&#xff0c;主要就是隐藏一下滚动条在手机上美观一点。 但是使用uni-app官方文档建议的::-webkit-scrollbar在目标标签时发现没生效。 .scroll-view_H::-webkit-scrollbar{display: none; }解决 F12看了一下&#xff0c;原来编译到浏览…

微软开源 MS-DOS「GitHub 热点速览」

上周又是被「大模型」霸榜的一周&#xff0c;各种 AI、LLM、ChatGPT、Sora、RAG 的开源项目在 GitHub 上“争相斗艳”。这不 Meta 刚开源 Llama 3 没几天&#xff0c;苹果紧跟着就开源了手机端大模型&#xff1a;CoreNet。 GitHub 地址&#xff1a;github.com/apple/corenet 开…