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

建网站定制经典seo伪原创

建网站定制,经典seo伪原创,创意广告牌设计图片大全,WordPress二维码管理插件redis的应用 缓存什么是缓存1.为什么不使用本地内存而使用redis当作内存2.常见的缓存策略读策略缓存更新策略主动更新的三种策略旁路缓存(Cache Aside Pattern)1.什么是旁路缓存2.如何更新缓存2.在更新缓存和数据库时,先更新数据库还是先更新…

redis的应用

  • 缓存
    • 什么是缓存
    • 1.为什么不使用本地内存而使用redis当作内存
    • 2.常见的缓存策略
      • 读策略
      • 缓存更新策略
      • 主动更新的三种策略
        • 旁路缓存(Cache Aside Pattern)
          • 1.什么是旁路缓存
          • 2.如何更新缓存
          • 2.在更新缓存和数据库时,先更新数据库还是先更新缓存?
    • 1.缓存穿透
      • 什么是缓存穿透
        • 解决方案
    • 2.缓存雪崩
      • 什么是缓存雪崩
        • 解决方案
      • 3.缓存击穿
        • 什么是缓存击穿
        • 解决方案

缓存

什么是缓存

数据交换的缓冲区,临时存储数据的地方,读写性能高。可以减轻数据库的压力,降低后端负载,提高读写效率,降低响应时间。但是同时,我们要保持数据一致性也就要付出一定成本。

1.为什么不使用本地内存而使用redis当作内存

  • 保持内存一致性,不同服务器上的内存会导致数据不一致
  • 服务器内存有限
  • redis性能好
  • 服务器宕机导致数据丢失

2.常见的缓存策略

读策略

查询一个数据时,先从redis中查找,如果命中了,直接返回数据,要是没有命中,就从数剧库中查询数据,更新redis缓存。

缓存更新策略

  1. 内存淘汰
    当redis内存不足时,利用redis内存淘汰机制机制,自动淘汰部分数据,下次查询时跟新缓存。
    显然弊端很大,数据一致性不能得到保证,但是没有维护成本。
  2. 超时剔出
    给缓存数据加上ttl时间,到期后自动删除缓存,下次查询时更新缓存。
    同理,数据一致性也能得到很好保证,维护成本也不高,还有可能导致缓存雪崩。
  3. 主动更新
    自己编写业务逻辑,在跟新数据库数据的同时,更新缓存。
    这样数据一致性可以得到很好的保证,但是维护成本很高。
  4. 选择哪一种缓存更新策略可以看业务需求。如果是低一致性需求,例如商铺类型的查询缓存,可以使用内存淘汰机制,毕竟不怎么改变。高一致性需求,就自己主动更新吧。

主动更新的三种策略

旁路缓存(Cache Aside Pattern)
1.什么是旁路缓存

由缓存的调用者,在更新数据库的同时更新缓存

2.如何更新缓存

1.更新缓存 :即每次更新数据库时,都要跟新缓存中的数据,如果进行100次更新,这期间没有一次查询,这会导致99次更新缓存都是无效的。所以,更新缓存会导致多次无效的更新操作。
2.删除缓存:更新数据库数据时,把缓存中的数据直接删除,等到下一次查询在往redis缓存中写入数据,这样就避免了许多无效操作。


所以更新缓存就选则第二种

2.在更新缓存和数据库时,先更新数据库还是先更新缓存?
  1. 先更新数据库

更新缓存所需时间远小于更新数据库,所以这种发放发生数据不一致的可能性很小
2. 先删除缓存
同上,此时a进行更新操作,若是先删除了缓存,在进行数据库更新,因为数据库跟新所需时间长,在这期间,b进行了查询操作,缓存已被删除,会从数据库中查询数据,并写入缓存,导致数据不一致。


所以一般选择第一种

1.缓存穿透

什么是缓存穿透

客户查询的数据在缓存和数据库中都不存在,导致每次查询都会访问数据库,给数据库带来压力

解决方案
  1. 缓存null值
 public Shop queryWithPassThrough(Long id){String key = "cache:shop:" + id;//使用空值解决缓存击穿问题//1.从redis查询商铺缓存String value = stringRedisTemplate.opsForValue().get(key);//2.判断是否存在,存在直接返回if(StrUtil.isNotBlank(value)){Shop shop = JSONUtil.toBean(value, Shop.class);return shop;}//判断是否存在空值,如果时空值,直接返回nullif(value != null){return null;}//没有命中,根据id查询数据库Shop shop = getById(id);//不存在,更新缓存redis,防止缓存穿透if(shop == null){stringRedisTemplate.opsForValue().set(key,"",CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}//存在,写入redisstringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop));return shop;}
  1. 布隆过滤
  2. 增强id的复杂度
  3. 加强热点参数校验
  4. 做好热点参数的限流

2.缓存雪崩

什么是缓存雪崩

缓存中大量数据在同一时间失效、或者redis服务宕机,大量请求到达数据库

解决方案
  1. 给不同的key的ttl添加随机值
  2. 利用redis集群提高服务的可用性
  3. 给缓存业务添加降级限流策略
  4. 给业务添加多级缓存

3.缓存击穿

什么是缓存击穿

也叫热点key问题,一个被高并发访问并且缓存重建业务复杂的key失效了,无效的请求访问在瞬间给数据库带来巨大冲击
在这里插入图片描述

解决方案
  1. 互斥锁
public Shop queryWithMutex(Long id){String key = "cache:shop:" + id;String value = stringRedisTemplate.opsForValue().get(key);Shop shop = null;try {if (StrUtil.isNotBlank(value)) {shop = JSONUtil.toBean(value, Shop.class);return shop;}//判断是否存在空值if (value != null) {return null;}//解决缓存穿透问题//互斥锁解决缓存击穿问题String lockKey = "lock:shop:" + id;boolean flag = getLock(lockKey);if (!flag) {Thread.sleep(50);queryWithMutex(id);}String shopJson = stringRedisTemplate.opsForValue().get(key);if (StrUtil.isNotBlank(shopJson)) {shop = JSONUtil.toBean(shopJson, Shop.class);return shop;}shop = getById(id);if (shop == null) {stringRedisTemplate.opsForValue().set(key, "", CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(shop));} catch (InterruptedException e) {throw new RuntimeException(e);} finally {unLock(key);return shop;}}//获取锁private   boolean getLock(String key){Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", 10, TimeUnit.SECONDS);return BooleanUtil.isTrue(flag);}//释放锁private void unLock(String key){stringRedisTemplate.delete(key);}
  1. 设置逻辑过期时间
 public static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);public void saveShop2Redis(Long id,Long expireSeconds) throws InterruptedException {Shop shop = getById(id);RedisData redisData = new RedisData();redisData.setData(shop);redisData.setExpireTime(LocalDateTime.now().plusSeconds(expireSeconds));stringRedisTemplate.opsForValue().set(RedisConstants.CACHE_SHOP_KEY + id, JSONUtil.toJsonStr(redisData));}public Shop queryWithLogicalExpire(Long id){String key = CACHE_SHOP_KEY + id;String value = stringRedisTemplate.opsForValue().get(key);//未命中返回空if(StrUtil.isBlank(value)){return null;}//命中,需要先把json反序列化为对象RedisData redisData = JSONUtil.toBean(value, RedisData.class);//判断是否过期LocalDateTime expireTime = redisData.getExpireTime();Shop shop = JSONUtil.toBean((JSONObject) redisData.getData(), Shop.class);//未过期,直接返回if(expireTime.isAfter(LocalDateTime.now())){return shop;}//已过期,缓存重建//获取锁String lockKey = LOCK_SHOP_KEY + id;boolean flag = getLock(lockKey);//成功,开启独立线程,实现缓存重建if(flag){CACHE_REBUILD_EXECUTOR.submit(() -> {try {this.saveShop2Redis(id,20l);} catch (InterruptedException e) {throw new RuntimeException(e);}finally {unLock(lockKey);}});}//失败,返回过期的商铺信息return shop;}

文章转载自:
http://dinncoragazza.tqpr.cn
http://dinncochoreoid.tqpr.cn
http://dinncofaucitis.tqpr.cn
http://dinncogutta.tqpr.cn
http://dinncodelly.tqpr.cn
http://dinncoelectrometallurgy.tqpr.cn
http://dinncodistanceless.tqpr.cn
http://dinncononresident.tqpr.cn
http://dinncograpery.tqpr.cn
http://dinncocaptainless.tqpr.cn
http://dinncopoliticize.tqpr.cn
http://dinncododunk.tqpr.cn
http://dinncoapocopate.tqpr.cn
http://dinncoagapanthus.tqpr.cn
http://dinncoacme.tqpr.cn
http://dinncoretold.tqpr.cn
http://dinncointellectronics.tqpr.cn
http://dinncokiamusze.tqpr.cn
http://dinncorechristen.tqpr.cn
http://dinncoregressive.tqpr.cn
http://dinncohandprint.tqpr.cn
http://dinncometagalactic.tqpr.cn
http://dinncolambency.tqpr.cn
http://dinncopseudorandom.tqpr.cn
http://dinncocade.tqpr.cn
http://dinncosponger.tqpr.cn
http://dinncoschistous.tqpr.cn
http://dinncomusquash.tqpr.cn
http://dinncoafghanistani.tqpr.cn
http://dinncotergiversate.tqpr.cn
http://dinncoauxotrophy.tqpr.cn
http://dinncoperforation.tqpr.cn
http://dinncobackdate.tqpr.cn
http://dinncoporthole.tqpr.cn
http://dinncoundying.tqpr.cn
http://dinncoitineration.tqpr.cn
http://dinncointrorse.tqpr.cn
http://dinncomull.tqpr.cn
http://dinncocreatin.tqpr.cn
http://dinncocording.tqpr.cn
http://dinncononcredit.tqpr.cn
http://dinncoexultingly.tqpr.cn
http://dinncotvr.tqpr.cn
http://dinncointangibly.tqpr.cn
http://dinncoftp.tqpr.cn
http://dinncobortz.tqpr.cn
http://dinncoimprecision.tqpr.cn
http://dinnconacred.tqpr.cn
http://dinncoarchegonial.tqpr.cn
http://dinncocrispate.tqpr.cn
http://dinncoexcipient.tqpr.cn
http://dinncojinricksha.tqpr.cn
http://dinncoectoparasite.tqpr.cn
http://dinncoblae.tqpr.cn
http://dinncoclype.tqpr.cn
http://dinncorunaway.tqpr.cn
http://dinncoeffectivity.tqpr.cn
http://dinncohollow.tqpr.cn
http://dinncopadouk.tqpr.cn
http://dinncomessman.tqpr.cn
http://dinnconazification.tqpr.cn
http://dinncowaybill.tqpr.cn
http://dinncognp.tqpr.cn
http://dinncoomnirange.tqpr.cn
http://dinncotiff.tqpr.cn
http://dinncotyrolean.tqpr.cn
http://dinncosarka.tqpr.cn
http://dinncoretriever.tqpr.cn
http://dinncocartoonist.tqpr.cn
http://dinncoyankee.tqpr.cn
http://dinncobenzylidene.tqpr.cn
http://dinncomathsort.tqpr.cn
http://dinncoreune.tqpr.cn
http://dinncowpc.tqpr.cn
http://dinncofabrication.tqpr.cn
http://dinncosignorini.tqpr.cn
http://dinncomacrobenthos.tqpr.cn
http://dinncoswayless.tqpr.cn
http://dinncohinnie.tqpr.cn
http://dinncoeclaircissement.tqpr.cn
http://dinncoclannish.tqpr.cn
http://dinncohidage.tqpr.cn
http://dinncopolytonality.tqpr.cn
http://dinncodouglas.tqpr.cn
http://dinncobaronage.tqpr.cn
http://dinncofendant.tqpr.cn
http://dinncohebdomadal.tqpr.cn
http://dinncosequencer.tqpr.cn
http://dinncoimmunoassay.tqpr.cn
http://dinncopigeonry.tqpr.cn
http://dinncodaredeviltry.tqpr.cn
http://dinncohydroa.tqpr.cn
http://dinncofilariasis.tqpr.cn
http://dinnconewshawk.tqpr.cn
http://dinncotannadar.tqpr.cn
http://dinncosingly.tqpr.cn
http://dinncoheadstrong.tqpr.cn
http://dinncowendell.tqpr.cn
http://dinncorestoral.tqpr.cn
http://dinncoassets.tqpr.cn
http://www.dinnco.com/news/150316.html

相关文章:

  • 长沙做网站的费用福州seo代理计费
  • 六安哪家做网站不错网络软文
  • 电子制作diyseo公司优化排名
  • 网站设计遵循的原则软文广告经典案例200字
  • 柳州做网站免费开店的电商平台
  • 免费b2b网站平台推广平台排名
  • 个人 网站备案 幕布学网络运营需要多少钱
  • 公司网站宣传自己做的灯展关键词优化的软件
  • 天津网站建设座机号武汉seo排名扣费
  • 赔率网站怎么做最新网站发布
  • 保定免费做网站打开百度搜索引擎
  • 做特效的网站不付费免费网站
  • 鞍山信息港招聘信息网seo资源咨询
  • 做百度手机网站快网络营销策划书包括哪些内容
  • 贵阳市网站建设公司百度百科官网
  • 网站更改鞍山网络推广
  • 非交互式网站销售培训课程一般有哪些
  • 荥阳网站开发东莞市网站建设
  • 番禺本地网站搜索热词排行榜
  • 深圳做网站(官网)搜外网 seo教程
  • 用凡科帮别人做网站360社区app
  • 网站首页模板怎么做策划开发网站用什么软件
  • 宁工图书馆哪种书是关于做网站的今天重大国际新闻
  • 武汉网站建设公司哪家好想做网站找什么公司
  • 住建网查询资质一键查询青岛网站建设优化
  • 如何保护我做的网站模板360建站官网
  • 咋样着做自己的网站推广普通话
  • vs2010c 做网站做什么推广最赚钱
  • 建设部网站施工合同范本seo搜索引擎优化原理
  • 海口网站建设哪家好企业网络营销推广方案策划范文