服装网站建设价格,网络服务合同范本大全,物流网络优化,成都哪些地方好玩目录 一、查询商品信息的常规代码示例二、缓存击穿2.1、缓存击穿的理解2.2、缓存击穿的解决方案2.3、解决缓存击穿的代码示例 三、缓存雪崩3.1、缓存雪崩的理解3.2、缓存雪崩的解决方案3.2.1、缓存集中过期的情况3.2.2、缓存服务器宕机的情况3.2.3、缓存服务器断电的情况 3.3、… 目录 一、查询商品信息的常规代码示例二、缓存击穿2.1、缓存击穿的理解2.2、缓存击穿的解决方案2.3、解决缓存击穿的代码示例 三、缓存雪崩3.1、缓存雪崩的理解3.2、缓存雪崩的解决方案3.2.1、缓存集中过期的情况3.2.2、缓存服务器宕机的情况3.2.3、缓存服务器断电的情况 3.3、解决缓存雪崩缓存集中过期的代码示例 四、缓存穿透4.1、缓存穿透的理解4.2、缓存穿透的解决方案4.3、解决缓存穿透的代码示例 一、查询商品信息的常规代码示例
查询商品信息的常规代码示例
/**
*查询商品信息
*/
public ExpressInfo findByDeliveryOrderId(Long id){String keyxz-express:expmess-info://从 Redis查询物流信息Object obj redisTemplate.opsForValue().get( key id);if (obi ! null) [return (ExpressInfo) obj; }else {ExpressInfo expressInfo expressMapper,selectByDeliveryOrderId(id);//数据库查询 if(expressInfo l nul1){ redisTemplate,opsForValue(),set(key dexpressInfo,Duration,ofHours(2));return expressInfo;}else {throw new clientException(发货单的物流信息不存在id);}}
}
二、缓存击穿
2.1、缓存击穿的理解
高并发时当一个kev非常热点(类似于爆款)在不停的扛着大并发当这个key在失效的瞬间持续的大并发就穿破缓存直接请求数据库并设置到缓存中导致性能下降。
2.2、缓存击穿的解决方案
设置缓存永不过期加锁排队
2.3、解决缓存击穿的代码示例 代码示例 /**
*查询商品信息
*/
Suppresswarnings(unchecked”)
public ExpressInfo findByDeliveryOrderId(Long id){String keyxz-express:expmess-info://从 Redis查询物流信息Object obj redisTemplate.opsForValue().get( key id);if (obi null) {synchronized (this){//进入 synchronized 一定要先再查询一次 Redis防止上一个抢到锁的线程已经更新过了obj redisTemplate.opsForValue().get( key id);if(obj ! null){return (ListProductCategory) obj;}//数据库查询 ListProductCategory categorylList productCategoryMapper.selectProductCategory(id);redisTemplate,opsForValue().set(keycategoryListDuration.ofHours(2L));}return categorylList ; }else {return (ListProductCategory) obj;}
}三、缓存雪崩
3.1、缓存雪崩的理解
缓存集中过期或者缓存服务器宕机导致大量请求访问数据库造成数据库瞬间压力过大宕机。
3.2、缓存雪崩的解决方案
3.2.1、缓存集中过期的情况
加锁排队设置随机失效时间
3.2.2、缓存服务器宕机的情况
提前部署好redis高可用集群比如哨兵模式
3.2.3、缓存服务器断电的情况
提前做好灾备多机房部署
3.3、解决缓存雪崩缓存集中过期的代码示例 代码示例 /**
*查询商品信息
*/
Suppresswarnings(unchecked”)
public ExpressInfo findByDeliveryOrderId(Long id){String keyxz-express:expmess-info://从 Redis查询物流信息Object obj redisTemplate.opsForValue().get( key id);if (obi null) {synchronized (this){//进入 synchronized 一定要先再查询一次 Redis防止上一个抢到锁的线程已经更新过了obj redisTemplate.opsForValue().get( key id);if(obj ! null){return (ListProductCategory) obj;}//数据库查询 ListProductCategory categorylList productCategoryMapper.selectProductCategory(id);//设置随机失效时间Duration expire DurationofHours(2L).plus(Duration.ofSeconds((Math .random() 100)));redisTemplate,opsForValue().set(keycategoryListexpire);}return categorylList ; }else {return (ListProductCategory) obj;}
}四、缓存穿透
4.1、缓存穿透的理解
数据库不存在缓存中也不存在导致每次请求都会去查询数据库这时的用户很可能是攻击者如发起为id为“-1”的数据或id为特别大(不存在的数据)导致数据库压力过大或宕机。
4.2、缓存穿透的解决方案
参数校验缓存空对象布隆过滤器
4.3、解决缓存穿透的代码示例 代码示例 /**
*查询商品信息
*/
Suppresswarnings(unchecked”)
public ExpressInfo findByDeliveryOrderId(Long id){String keyxz-express:expmess-info://从 Redis查询物流信息Object obj redisTemplate.opsForValue().get( key id);if (obi null) {synchronized (this){//进入 synchronized 一定要先再查询一次 Redis防止上一个抢到锁的线程已经更新过了obj redisTemplate.opsForValue().get( key id);if(obj ! null){return (ListProductCategory) obj;}//数据库查询 ListProductCategory categorylList productCategoryMapper.selectProductCategory(id);//设置随机失效时间Duration expire DurationofHours(2L).plus(Duration.ofSeconds((Math .random() 100)));//从数据库中查询出的categoryList不管是否是空都存到redis中redisTemplate,opsForValue().set(keycategoryListexpire);}return categorylList ; }else {return (ListProductCategory) obj;}
}