欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 点评项目-7-缓存击穿的两种解决方案、缓存工具类的编写

点评项目-7-缓存击穿的两种解决方案、缓存工具类的编写

2024/10/23 13:06:37 来源:https://blog.csdn.net/m0_75138009/article/details/142859049  浏览:    关键词:点评项目-7-缓存击穿的两种解决方案、缓存工具类的编写

缓存击穿

在高并发访问的访问中,对于复杂业务 key 的缓存,可能会在缓存生效前打入大量的请求,导致大量的请求打到数据库

解决方案:

1.互斥锁,给缓存的构建过程加上一个锁,当拿到锁时才进行下一步,锁被占用则睡眠一段时间后再拿锁

2.逻辑过期,给缓存加上一个逻辑过期时间,但是在 redis 中过期的数据不会被真正删除,在查询时,如果 key 在逻辑上过期了,则开启一个锁,并把更新 key 的任务交给另一个线程,然后先直接返回旧数据;若某个遇到锁被占用无需等待,直接返回旧数据。

编写缓存工具类,实现代码的复用

@Slf4j
@Component
public class CacheClient {private StringRedisTemplate stringRedisTemplate;public CacheClient(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}//存入缓存public void setForString(String key, Object value, long time, TimeUnit unit){String json = JSONUtil.toJsonStr(value);stringRedisTemplate.opsForValue().set(key,json,time,unit);}//删除缓存public void removeKey(String key){stringRedisTemplate.delete(key);}//存入缓存,并设置逻辑日期时间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 getById(String preKey, ID id, Class<R> type, Function<ID,R> getById,long time,TimeUnit unit) {String key = preKey + id;//先在 redis 中查询String json = stringRedisTemplate.opsForValue().get(key);if(StrUtil.isNotBlank(json)){//(2024-10-11这个逻辑出现了错误,json 并不是 null,可是封装过去后全是 null,原因:用成了 BeanUtil)return JSONUtil.toBean(json, type);}//判断是否穿透后查询到 ""if(json != null){return null;}//redis 中没查到,在数据库中查询R r = getById.apply(id);if(r == null){setForString(key,"",time,unit);//防击穿,防雪崩return null;}String s = JSONUtil.toJsonStr(r);
//      //(2024-10-11,这一句的逻辑出bug了,找到原因:传参 time 为 0,括号出现了位置错误)stringRedisTemplate.opsForValue().set(key,s,time,unit);return r;}}

 防穿透方法改造后的测试:

    //使用工具类查询(解决缓存穿透版)@Overridepublic Result getById(Long id){long ttl =  (long) (Math.random() * 100)+1;Shop shop = cacheClient.getById("shop:cache:", id, Shop.class, a -> shopMapper.selectById(a),ttl, TimeUnit.MINUTES);if(shop == null){return Result.fail("未查询到该店铺ToT");}return Result.ok(shop);}

基于互斥锁方式改造店铺查询业务:

对于锁的使用,我们可以利用 redis 中的 setnx 命令(只有 key 不存在是操作才成功)来充当锁,需要开启锁时写入一个 setnx ,释放锁时 del 这个 key

//基于互斥锁的方式防止店铺信息查询击穿public <R,ID> R queryShopByLock(String preKey, ID id, Class<R> type, Function<ID,R> getById,long time,TimeUnit unit){String key = preKey + id;//先在 redis 中查询String json = stringRedisTemplate.opsForValue().get(key);if(StrUtil.isNotBlank(json)){//存在,直接返回return JSONUtil.toBean(json, type);}//判断是否穿透后查询到 ""if(json != null){return null;}//redis 中没查到,获取互斥锁String lockKey = "shop:lock:"+id;R r = null;try {boolean getLock = tryToGetLock(lockKey);if(!getLock){//获取锁失败,休眠后重试Thread.sleep(50);queryShopByLock(preKey,id,type,getById,time,unit);}//获取锁成功,完成缓存重建r = getById.apply(id);if(r == null){setForString(key,"",time,unit);return null;}String s = JSONUtil.toJsonStr(r);stringRedisTemplate.opsForValue().set(key,s,time,unit);} catch (InterruptedException e) {throw new RuntimeException(e);} finally {cleanLock(lockKey);}return r;}

 使用 jmeter 进行并发测试:

    @Overridepublic Result getById(Long id){long ttl =  (long) (Math.random() * 100)+1;Shop shop = cacheClient.queryShopByLock("cache:shop:", id, Shop.class, a -> shopMapper.selectById(a), ttl, TimeUnit.MINUTES);if(shop == null){return Result.fail("未查询到该店铺ToT");}return Result.ok(shop);}

所有请求都成功响应 

基于逻辑过期方式改造店铺查询

这种方式一般是通过手动的批量添加需要频繁访问的 key,在不需要使用时再将其批量删除,使用场景有:临时活动相关的请求

这里通过测试方法批量添加 key

    @Testvoid saveKeysWithLogicalExpire(){for(int i=0;i<10;i++){RedisData redisData = new RedisData();Shop shop = shopMapper.selectById(i + 1);redisData.setData(shop);redisData.setExpireTime(LocalDateTime.now());stringRedisTemplate.opsForValue().set("cache:shop:"+shop.getId(), JSONUtil.toJsonStr(redisData));}}//手动删除缓存@Testvoid delKeys(){for(int i=0;i<10;i++){stringRedisTemplate.delete("cache:shop:"+(i+1));}}

维护一个线程池,用于单独处理更新缓存的逻辑

    //线程池,用于逻辑过期执行更新数据库private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);

业务逻辑:

    //基于逻辑过期的方式防止店铺信息查询击穿public <R,ID> R queryShopByLogicExpire(String preKey, ID id, Class<R> type , Function<ID,R> getById,long time,TimeUnit unit){String key = preKey + id;//先在 redis 中查询String json = stringRedisTemplate.opsForValue().get(key);if(StrUtil.isBlank(json)){//正常情况一定能查到,没查到,返回空对象return null;}//查到了,将 json 转换为可以使用的类RedisData redisData = JSONUtil.toBean(json, RedisData.class);R r = JSONUtil.toBean((JSONObject) redisData.getData(),type);//将Object类型的json转换为指定 type 的类LocalDateTime expireTime = redisData.getExpireTime();//判断是否过期,没过期直接返回数据if(expireTime.isAfter(LocalDateTime.now())){//过期时间在当前时间之后,未过期return r;}//过期了,重建缓存String lockKey = "shop:lock:"+id;boolean getLock = tryToGetLock(lockKey);if(getLock){//成功获取锁,再次判断是否过期boolean isOverTime = judgeLogicalExpire(key);if(!isOverTime){//释放锁cleanLock(lockKey);return r;}//再次判断依然过期,唤醒处理数据库的线程CACHE_REBUILD_EXECUTOR.submit(()->{//需要交给线程执行的逻辑try {R apply = getById.apply(id);this.setWithLogicalExpire(key,apply,time,unit);} catch (Exception e) {e.printStackTrace();}finally {//释放锁cleanLock(lockKey);}});}//返回已过期的数据return r;}//判断是否逻辑过期,true 表示过期public boolean judgeLogicalExpire(String key){String json = stringRedisTemplate.opsForValue().get(key);if(StrUtil.isBlank(json)){return true;}RedisData redisData = JSONUtil.toBean(json, RedisData.class);LocalDateTime expireTime = redisData.getExpireTime();return !expireTime.isAfter(LocalDateTime.now());}//尝试获取锁private boolean tryToGetLock(String key){Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "aaa", 10, TimeUnit.SECONDS);return BooleanUtil.isTrue(flag);}//释放锁private void cleanLock(String key){stringRedisTemplate.delete(key);}

使用 jmeter 进行并发测试:

    @Overridepublic Result getById(Long id){long ttl =  (long) (Math.random() * 100)+1;Shop shop = cacheClient.queryShopByLogicExpire("cache:shop:", id, Shop.class, a -> shopMapper.selectById(a), ttl, TimeUnit.MINUTES);if(shop == null){return Result.fail("未查询到该店铺ToT");}return Result.ok(shop);}

所有的请求都成功响应 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com