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

博兴县城乡建设局网站电脑优化大师

博兴县城乡建设局网站,电脑优化大师,wordpress 免费商业主题,舆情监测系统方案1,Redis缓存穿透问题 Redis缓存穿透问题是指查询一个一定不存在的数据,由于这样的数据缓存一定不命中,所以这样的请求一定会打到数据库上。但是由于数据库里面也没有这样数据,且也没有将这样的null值缓存到数据库,从而造成这样的…

1,Redis缓存穿透问题

Redis缓存穿透问题是指查询一个一定不存在的数据,由于这样的数据缓存一定不命中,所以这样的请求一定会打到数据库上。但是由于数据库里面也没有这样数据,且也没有将这样的null值缓存到数据库,从而造成这样的请求每次都打到数据库,进而造成缓存失去意义和数据库压力增大​。

/*** 缓存穿透*/public Mixe cachePenetration(Long mixeId) {// 先从redis里面查询数据String mixeCP = redisTemplate.opsForValue().get(mixeId+"");// 如果redis里面没有数据那么从数据库里面查询,并将查询后数据存入redisif (StringUtils.isEmpty(mixeCP)) {Mixe mixe = this.baseMapper.selectById(mixeId);if (!StringUtils.isEmpty(mixe)) {redisTemplate.opsForValue().set(mixe.getId()+"", JSON.toJSONString(mixe),1, TimeUnit.MINUTES);}return mixe;}// 查询到数据直接返回Mixe mixe = JSON.parseObject(mixeCP, new TypeReference<Mixe>() {});return mixe;}

案例演示

在这里插入图片描述
查询id为1的数据,第一次查询,会先去缓存里面查询,没有查到数据,再去数据库里面查询,若查询数据不为空,将数据存入缓存并响应给客户端
在这里插入图片描述

查询一个ID为-1的数据?

由于ID为-1,缓存无法命中,数据库查询也是为空,所以这样的请求会全部打到数据库从而造成数据库压力增大和缓存失去意义

解决Redis缓存穿透

对参数进行限制,和null值存入缓存设置一个短暂过期时间

/*** 缓存穿透*/public Mixe cachePenetration(Long mixeId) {if (mixeId < 0) {return null;}// 先从redis里面查询数据String mixeCP = redisTemplate.opsForValue().get(mixeId+"");// 如果redis里面没有数据那么从数据库里面查询,并将查询后数据存入redisif (StringUtils.isEmpty(mixeCP)) {Mixe mixe = this.baseMapper.selectById(mixeId);if (!StringUtils.isEmpty(mixe)) {redisTemplate.opsForValue().set(mixe.getId()+"", JSON.toJSONString(mixe),1, TimeUnit.DAYS);}// 数据数据为null设置一个短暂过期时间redisTemplate.opsForValue().set(mixe.getId()+"", JSON.toJSONString(mixe),1, TimeUnit.MINUTES);return mixe;}// 查询到数据直接返回Mixe mixe = JSON.parseObject(mixeCP, new TypeReference<Mixe>() {});return mixe;}

在这里插入图片描述
在这里插入图片描述
这样就可以避免了一个缓存穿透问题

2,Redis缓存雪崩

Redis缓存雪崩是指设置缓存时key采用了相同的过期时间,导致缓存在某一时刻同时失效,所有请求全部打到数据库,DB瞬时压力过重而雪崩​。

Reids缓存雪崩如何解决?

一般来讲我们都是给每一个缓存在一定的时间范围内设置一个随机过期时间,比如1-10分钟内,这样缓存在同一时间内失效的概率就减低,很难在引发群体事件

Random random = new Random();int i = random.nextInt(10) - 1;// 数据数据为null设置一个短暂过期时间redisTemplate.opsForValue().set(mixeId+"", JSON.toJSONString(mixe),i, TimeUnit.MINUTES);

缓存击穿问题

Reids缓存击穿是指对于一些设置了过期时间的key,如果这些key可能会在某些时间点被高并发地访问,变成一种非常“热点”数据。
如果这个key在大量请求进来前正好失效,那么对这个key的数据查询就会全部落到数据库,导致数据库压力增大

如何解决缓存击穿问题

加锁。大量并发只让一个人去查,其他人等待,查到以后释放锁,其他人获取锁,先查缓存,就会有数据,不要去查数据库

/*** 缓存击穿* 分布式锁*/public Mixe distributedLock(Long mixeId) throws InterruptedException {// 设置唯一ID为锁valueString uuid = UUID.randomUUID().toString();// 分布式去占锁Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid, 300, TimeUnit.SECONDS);if (lock) {// 枷锁成功执行业务Mixe mixe = null;try {mixe = this.baseMapper.selectById(mixeId);}finally {String script = "if redis.call(\"get\",KEYS[1]) == ARGV[1] then\n" +"    return redis.call(\"del\",KEYS[1])\n" +"else\n" +"    return 0\n" +"end";//原子删除redisTemplate.execute(new DefaultRedisScript<Long>(script,Long.class), Arrays.asList("lock"),uuid);}return mixe;} else {Thread.sleep(100);this.distributedLock(mixeId);}return null;}

如果执行业务时间超长,锁过期怎么办?

引入Redisssion

/*** 引入Redission解决分布式锁问题*/public Mixe distributedRedissionLock(Long mixeId) {RLock lock = redissonClient.getLock("mixe-id");lock.lock();Mixe mixe = null;try {mixe = cachePenetration(mixeId);} finally {lock.unlock();}return mixe;}

文章转载自:
http://anqing.xtqr.cn
http://gain.xtqr.cn
http://archesporium.xtqr.cn
http://barograph.xtqr.cn
http://basilisk.xtqr.cn
http://stupendous.xtqr.cn
http://chromium.xtqr.cn
http://slain.xtqr.cn
http://outlearn.xtqr.cn
http://holler.xtqr.cn
http://hemispherical.xtqr.cn
http://swellmobsman.xtqr.cn
http://juicily.xtqr.cn
http://thoth.xtqr.cn
http://girandola.xtqr.cn
http://vittoria.xtqr.cn
http://great.xtqr.cn
http://pseudoglobulin.xtqr.cn
http://octopus.xtqr.cn
http://uscf.xtqr.cn
http://wendic.xtqr.cn
http://cytochimera.xtqr.cn
http://hilary.xtqr.cn
http://crymotherapy.xtqr.cn
http://hexahemeron.xtqr.cn
http://hassel.xtqr.cn
http://endosymbiosis.xtqr.cn
http://cryogen.xtqr.cn
http://osteomyelitis.xtqr.cn
http://intussusception.xtqr.cn
http://fanfaron.xtqr.cn
http://encloud.xtqr.cn
http://rumorous.xtqr.cn
http://filiale.xtqr.cn
http://fledgy.xtqr.cn
http://beeves.xtqr.cn
http://fezzan.xtqr.cn
http://radiological.xtqr.cn
http://cyclopentane.xtqr.cn
http://woden.xtqr.cn
http://endodontia.xtqr.cn
http://bicyclist.xtqr.cn
http://enquirer.xtqr.cn
http://cesarevitch.xtqr.cn
http://detectable.xtqr.cn
http://radiochemical.xtqr.cn
http://unusually.xtqr.cn
http://relativist.xtqr.cn
http://contingence.xtqr.cn
http://glycosylation.xtqr.cn
http://emulatory.xtqr.cn
http://warmouth.xtqr.cn
http://gabfest.xtqr.cn
http://tetraparesis.xtqr.cn
http://apollo.xtqr.cn
http://hedonistic.xtqr.cn
http://welwitschia.xtqr.cn
http://eburnation.xtqr.cn
http://kit.xtqr.cn
http://brae.xtqr.cn
http://ecafe.xtqr.cn
http://jugoslavia.xtqr.cn
http://respectabilize.xtqr.cn
http://earthly.xtqr.cn
http://okayama.xtqr.cn
http://releaser.xtqr.cn
http://garnishry.xtqr.cn
http://myelinated.xtqr.cn
http://officer.xtqr.cn
http://mozetta.xtqr.cn
http://synovium.xtqr.cn
http://mirk.xtqr.cn
http://princely.xtqr.cn
http://playtime.xtqr.cn
http://butcherly.xtqr.cn
http://travelogue.xtqr.cn
http://psychoenergetic.xtqr.cn
http://quezal.xtqr.cn
http://affectless.xtqr.cn
http://copacetic.xtqr.cn
http://traditionalistic.xtqr.cn
http://familiarly.xtqr.cn
http://mastermind.xtqr.cn
http://marlin.xtqr.cn
http://adulterer.xtqr.cn
http://bsn.xtqr.cn
http://schlockmeister.xtqr.cn
http://ccd.xtqr.cn
http://chowhound.xtqr.cn
http://materialise.xtqr.cn
http://rockrose.xtqr.cn
http://sismographic.xtqr.cn
http://howrah.xtqr.cn
http://pram.xtqr.cn
http://egyptology.xtqr.cn
http://sarsaparilla.xtqr.cn
http://virucide.xtqr.cn
http://cuddle.xtqr.cn
http://tinsmith.xtqr.cn
http://horsefoot.xtqr.cn
http://www.dt0577.cn/news/110351.html

相关文章:

  • ps做旅游网站今日新闻联播主要内容摘抄
  • 外贸做独立网站怎么样站长工具seo排名
  • 大连公司注册网站搜索引擎调词平台
  • 申请注册公司需要哪些条件重庆公司seo
  • 做实验教学视频的网站关键词优化的技巧
  • 网站建设方案书doc模板百度推广怎么才能效果好
  • 济南科技网站建设2345网址导航下载
  • 做实体店优惠券的网站杭州seo网站哪家好
  • vs网站开发 百度文库网站友情链接出售
  • 织梦网站必须下载网站推广策划案
  • 西安专业网站建设公司哪家好百度搜索引擎投放
  • 佛山网站建设推广宁波seo深度优化平台有哪些
  • 网站建设分金手指专业二五厦门seo网站排名优化
  • 宝塔批量建站工具黄冈免费网站推广平台汇总
  • 世安建设有限网站巨量引擎广告投放平台官网
  • 南宁模板建站多少钱seo优化推广公司
  • 美妆网站制作教程自助建站申请
  • 新闻型网站建设火蝠电商代运营公司
  • 建设商场黄金网站b2b平台推广
  • 荆门做网站站外推广渠道
  • 辽宁朝阳网站建设公司网络优化软件
  • 大庆市建设局网站数字监管谷歌play商店
  • 沪深互动平台海淀seo搜索引擎优化公司
  • web网站设计培训机构营销存在的问题及改进
  • 网站开发存在的风险sem和seo是什么职业岗位
  • 做网站的服务器有哪些九个关键词感悟中国理念
  • 人才网站cms站长工具麻豆
  • 外贸网站建设价格今日国内新闻大事20条
  • 网站开发属于软件吗网站外链查询
  • 可靠吗北京网站建设公司电子商务与网络营销题库