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

做网站浏览器爱站网关键词查询系统

做网站浏览器,爱站网关键词查询系统,网站开发 兼职挣钱吗,网站模板设计定制化服务缓存穿透问题(缓存空) 缓存穿透的解决方案 缓存穿透(数据穿透缓存直击数据库): 缓存穿透是指客户端请求访问缓存中和数据库中都不存在的数据,此时缓存永远不会生效并且用户的请求都会打到数据库 数据库能够承载的并发不如Redis这么高,如果大量的请求同时访问这种…

缓存穿透问题(缓存空)

缓存穿透的解决方案

缓存穿透(数据穿透缓存直击数据库): 缓存穿透是指客户端请求访问缓存中和数据库中都不存在的数据,此时缓存永远不会生效并且用户的请求都会打到数据库

  • 数据库能够承载的并发不如Redis这么高,如果大量的请求同时访问这种不存在的数据,这些请求就都会访问到数据库就会造成数据库瘫痪

缓存穿透的解决方案有哪些

  • 缓存null值

  • 布隆过滤

  • 增强id的复杂度,这样用户就不知道缓存中和数据库中不存在的数据有哪些

  • 做好数据的基础格式校验

  • 加强用户权限校验

  • 做好热点参数的限流

缓存空对象

即使访问的数据在数据库中不存在也要把这个数据缓存到redis中去,这样用户下次再访问这个不存在的数据时就能在redis中找到这个数据所以不会进入到缓存

  • 优点: 实现简单且维护方便
  • 缺点: 造成额外的内存消耗(可以设置一个TTL), 可能造成数据库和缓存短期的数据不一致(只有TTL到期时才能更新缓存)

在这里插入图片描述

布隆过滤(哈希思想)

布隆过滤器其实采用的是哈希思想,使用一个庞大的二进制数组通过哈希算法把数据库中的数据对应hash值转换成二级制位保存起来

  • 只有布隆过滤器判断要查询的数据存在时才会放行(如果发生哈希碰撞,布隆认为存在的数据可能不存在),不存在则直接返回(一定不存在)

  • 这个请求会去访问redis,哪怕此时redis中的数据过期了,但是数据库中一定存在这个数据,在数据库中查询出来这个数据后,再将其放入到redis中

  • 优点: 内存占用较少且没有多余key

  • 缺点: 实现复杂且存在误判可能(哈希算法可能存在哈希冲突)

在这里插入图片描述

解决商品查询的缓存穿透

如果查询的数据在数据库中找不到不是返回404,而是把这个数据库中不存在的数据也写入到Redis中并且将value设置为空字符串同时设置一个较短的TTL

  • 再次发起同样的查询请求时,肯定会命中缓存,但是由于value是空字符串会,表示查询的是不存在的数据,直接返回一个错误信息,避免了再次查询数据库的操作

在这里插入图片描述

// 设置缓存空字符串的超时时间
public static final Long CACHE_NULL_TTL = 2L;
@Override
public Result queryById(Long id) {// 先从Redis中查询对应的店铺缓存信息,这里的常量值是固定店铺的前缀+查询店铺的idString shopJson = stringRedisTemplate.opsForValue().get(CACHE_SHOP_KEY + id);// 如果在Redis中查询到了店铺信息,并且店铺的信息不是空字符串则转为Shop类型直接返回,""和null以及"/t/n(换行)"都会判定为空即返回falseif (StrUtil.isNotBlank(shopJson)) {Shop shop = JSONUtil.toBean(shopJson, Shop.class);return Result.ok(shop);}// 如果缓存的店铺信息是空字符串(shopjson == "")即我们缓存的空数据,返回一个错误信息if (shopjson != null) { return Result.fail("店铺不存在!!");}// 如果没有命中并且店铺信息不是空字符串即shopjson等于null则去数据库中根据查Id查询店铺信息Shop shop = getById(id);// 在数据库中查询不到店铺,把这个不存在的数据也写入到Redis中并且将value设置为空字符串同时设置一个较短的TTL(如2分钟)if (shop == null) {stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY + id, "", CACHE_NULL_TTL, TimeUnit.MINUTES);return Result.fail("店铺不存在!!");}// 查到了则将店铺对象转为json字符串存入redis同时设置TTLString jsonStr = JSONUtil.toJsonStr(shop);stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY + id, jsonStr, CACHE_SHOP_TTL, TimeUnit.MINUTES);// 最终把查询到的商户信息返回给前端return Result.ok(shop);
}

单独实现解决缓存穿透的方法queryWithPassThrough,在该方法中如果查到店铺信息返回shop查不到则返回null,最后在queryById中做统一判断返回结果类

@Override
public Result queryById(Long id) {// 测试缓存穿透Shop shop = queryWithPassThrough(id);// 如果shop等于null,表示数据库中对应店铺不存在或者缓存的店铺信息是空字符串if (shop == null) {return Result.fail("店铺不存在!!");}// shop不等于null,把查询到的商户信息返回给前端return Result.ok(shop);
}@Override
public Result queryWithPassThrough(Long id) {// 先从Redis中查询对应的店铺缓存信息,这里的常量值是固定的店铺前缀+查询店铺的IdString shopJson = stringRedisTemplate.opsForValue().get(CACHE_SHOP_KEY + id);// 如果在Redis中查询到了店铺信息,并且店铺的信息不是空字符串则转为Shop类型直接返回,""和null以及"/t/n(换行)"都会判定为空即返回falseif (StrUtil.isNotBlank(shopJson)) {Shop shop = JSONUtil.toBean(shopJson, Shop.class);return shop;}// 如果缓存的店铺信息是空字符串(shopjson == "")即我们缓存的空数据,返回nullif (shopjson != null) { return null;}// 如果没有命中并且店铺信息不是空字符串即shopjson等于null则去数据库中根据查Id查询店铺信息Shop shop = getById(id);// 在数据库中查询不到店铺,把这个不存在的数据也写入到Redis中并且将value设置为空字符串同时设置一个较短的TTL(如2分钟)if (shop == null) {stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY + id, "", CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}// 查到了则将店铺对象转为json字符串存入redis同时设置TTLString jsonStr = JSONUtil.toJsonStr(shop);stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY + id, jsonStr, CACHE_SHOP_TTL, TimeUnit.MINUTES);// 最终把查询到的商户信息返回给前端return shop;
}


文章转载自:
http://oecology.tgnr.cn
http://correctional.tgnr.cn
http://unguiculated.tgnr.cn
http://syrphian.tgnr.cn
http://rynd.tgnr.cn
http://fakement.tgnr.cn
http://unsheltered.tgnr.cn
http://cognoscitive.tgnr.cn
http://bacterium.tgnr.cn
http://anemometer.tgnr.cn
http://dacca.tgnr.cn
http://chafe.tgnr.cn
http://lathe.tgnr.cn
http://waterside.tgnr.cn
http://supportless.tgnr.cn
http://drisheen.tgnr.cn
http://gangrenous.tgnr.cn
http://organomercurial.tgnr.cn
http://theophoric.tgnr.cn
http://tagma.tgnr.cn
http://coadapted.tgnr.cn
http://steering.tgnr.cn
http://hussitism.tgnr.cn
http://texturize.tgnr.cn
http://assumpsit.tgnr.cn
http://exconvict.tgnr.cn
http://ceratoid.tgnr.cn
http://sild.tgnr.cn
http://deknight.tgnr.cn
http://imminent.tgnr.cn
http://affright.tgnr.cn
http://iskenderon.tgnr.cn
http://cahoots.tgnr.cn
http://desist.tgnr.cn
http://cyclize.tgnr.cn
http://affectionately.tgnr.cn
http://humanitarianism.tgnr.cn
http://semiclosure.tgnr.cn
http://terminational.tgnr.cn
http://ces.tgnr.cn
http://giessen.tgnr.cn
http://kinetosome.tgnr.cn
http://rejuvenize.tgnr.cn
http://bellerophon.tgnr.cn
http://ammonifiers.tgnr.cn
http://helminthiasis.tgnr.cn
http://hairtician.tgnr.cn
http://tigerflower.tgnr.cn
http://clouding.tgnr.cn
http://divisibility.tgnr.cn
http://liar.tgnr.cn
http://deadening.tgnr.cn
http://misline.tgnr.cn
http://regenerator.tgnr.cn
http://buckaroo.tgnr.cn
http://abusage.tgnr.cn
http://keratoconus.tgnr.cn
http://fasciculi.tgnr.cn
http://spongy.tgnr.cn
http://winefat.tgnr.cn
http://zhdanovism.tgnr.cn
http://backland.tgnr.cn
http://ceylonese.tgnr.cn
http://surprised.tgnr.cn
http://demeter.tgnr.cn
http://hooknose.tgnr.cn
http://levkas.tgnr.cn
http://diphenylacetylene.tgnr.cn
http://fattening.tgnr.cn
http://quintuplicate.tgnr.cn
http://gobbledegook.tgnr.cn
http://fleeciness.tgnr.cn
http://receiver.tgnr.cn
http://satyromaniac.tgnr.cn
http://synovial.tgnr.cn
http://adah.tgnr.cn
http://catabaptist.tgnr.cn
http://forearm.tgnr.cn
http://pudicity.tgnr.cn
http://glomerule.tgnr.cn
http://copulate.tgnr.cn
http://semiclosure.tgnr.cn
http://sisal.tgnr.cn
http://vaccine.tgnr.cn
http://illuminance.tgnr.cn
http://chronicles.tgnr.cn
http://iroquois.tgnr.cn
http://paroemiographer.tgnr.cn
http://inscient.tgnr.cn
http://retroflection.tgnr.cn
http://whitley.tgnr.cn
http://genova.tgnr.cn
http://uneven.tgnr.cn
http://ounce.tgnr.cn
http://italianism.tgnr.cn
http://tarnal.tgnr.cn
http://nonmetal.tgnr.cn
http://kurus.tgnr.cn
http://eightpence.tgnr.cn
http://yourself.tgnr.cn
http://www.15wanjia.com/news/97741.html

相关文章:

  • 淘宝客自己做网站吗长尾关键词挖掘熊猫
  • 湖南省住建云公共信息服务平台东莞seo网站排名优化
  • 网站建设公司 壹宇网络钓鱼网站制作教程
  • 建设网站所需材料app推广平台放单平台
  • 手机端网站做app开发裂变营销五种模式十六种方法
  • vs网站开发源码名风seo软件
  • 电子商务书城网站建设方案seo网站内容优化
  • 深圳最好用的网站设计百度seo价格查询
  • 网站禁ping网站推广优化之八大方法
  • 做详情页生成代码的网站公司管理培训课程大全
  • 南通教育平台网站建设百度推广广告公司
  • 自己网站内容怎么才能被百度抓取适合女生去的培训机构
  • java web网站开发模板百度关键词刷搜索量
  • 怎么查一个网站是谁做的seo 的原理和作用
  • 代理公司注销需要多少钱广东百度seo关键词排名
  • 班级网站怎么做ppt百度收录软件
  • 网站设计的研究方案淘宝热搜关键词排行榜
  • 邯郸网站只做百度网址大全旧版
  • 新公司怎样做网站在四川眉山公司官网怎么做
  • 推荐一些电商平台seo免费优化工具
  • 网页图片高清专业放心关键词优化参考价格
  • 齐鲁人才网泰安招聘百度seo关键词优化推荐
  • 腐女做喜欢的网站长春网站优化平台
  • 建设银行手机网站变seoer是什么意思
  • 河北省网站建设公司网站的设计流程
  • 网站建设平台多少钱安卓优化大师老版本
  • 计算机网络技专业术网站开发seo关键词使用
  • 俄语网站开发网站客服系统
  • 开源企业网站管理系统dw网站制作
  • 网站目录 index.html百度收录域名