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

sql server网站建设百度今日数据

sql server网站建设,百度今日数据,计算机网站开发图片,能看wordpress目录 一、查询商品信息的常规代码示例二、缓存击穿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 key="xz-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 + d,expressInfo,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 key="xz-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 (List<ProductCategory>) obj;}//数据库查询	List<ProductCategory> categorylList = productCategoryMapper.selectProductCategory(id);redisTemplate,opsForValue().set(key,categoryList,Duration.ofHours(2L));}return categorylList ; }else {return (List<ProductCategory>) 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 key="xz-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 (List<ProductCategory>) obj;}//数据库查询	List<ProductCategory> categorylList = productCategoryMapper.selectProductCategory(id);//设置随机失效时间Duration expire = DurationofHours(2L).plus(Duration.ofSeconds((Math .random() 100)));redisTemplate,opsForValue().set(key,categoryList,expire);}return categorylList ; }else {return (List<ProductCategory>) obj;}
    }
    

四、缓存穿透

4.1、缓存穿透的理解

  • 数据库不存在缓存中也不存在,导致每次请求都会去查询数据库,这时的用户很可能是攻击者如发起为id为“-1”的数据或id为特别大(不存在的数据),导致数据库压力过大或宕机。
    在这里插入图片描述

4.2、缓存穿透的解决方案

  • 参数校验
  • 缓存空对象
  • 布隆过滤器

4.3、解决缓存穿透的代码示例

  • 代码示例

    /**
    *查询商品信息
    */
    @Suppresswarnings("unchecked”)
    public ExpressInfo findByDeliveryOrderId(Long id){String key="xz-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 (List<ProductCategory>) obj;}//数据库查询	List<ProductCategory> categorylList = productCategoryMapper.selectProductCategory(id);//设置随机失效时间Duration expire = DurationofHours(2L).plus(Duration.ofSeconds((Math .random() 100)));//从数据库中查询出的categoryList不管是否是空,都存到redis中redisTemplate,opsForValue().set(key,categoryList,expire);}return categorylList ; }else {return (List<ProductCategory>) obj;}
    }
    
http://www.yidumall.com/news/25725.html

相关文章:

  • 单页营销网站怎么做最近三天的国内新闻
  • 如何做团购网站中的美食地处地图功能太原seo全网营销
  • 在线做txt下载网站怎么做推广比较成功
  • 广告设计培训内容湖南长沙seo
  • 三网站建设最稳定的灰色词排名
  • 南京市建设委员会网站培训机构排名
  • Wordpress插件开发中文字幕南京百度提升优化
  • 有什么网站是做中式酒店大堂的如何做好市场推广
  • 建立企业网站的步骤培训机构是干什么的
  • php做外贸网站好吗seo课程培训学校
  • 做佩戴护身符的厂家网站手机清理优化软件排名
  • 广东手机网站开发公司seo搜索引擎优化推广
  • 企业网站如何更新备案信息百度一下百度网页版
  • 哪个网站能帮助做路书谷歌 google
  • 网络营销工具平台徐州seo
  • 万江仿做网站百度极速版下载
  • 响应式网站的宽度推广链接
  • 深圳微信网站开发网站建设的整体流程有哪些
  • wordpress 微信 论坛seo是指什么意思
  • 太原公司网站建立自动引流免费app
  • 体育php网站源码平台推广方案
  • 缙云做网站百度关键词查询工具免费
  • 自制wordpress百度seo优化软件
  • 张掖哪家公司做网站个人网页设计
  • 安徽网站建设cnfg淘宝seo
  • 一个电子商务网站的用户购买行为监测报告文档格式怎么做?苏州seo建站
  • 网站qq在线客服代码网站快速上排名方法
  • 成都私人网站建设seo技术优化
  • 网站好玩代码和特效bt蚂蚁磁力
  • 博客网站开发背景网络广告联盟