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

网站建设需注意哪些事项深圳全网推互联科技有限公司

网站建设需注意哪些事项,深圳全网推互联科技有限公司,使用angular2框架做的网站,东莞市政府网站官网问题由来 数据redis和MySQL都要有一份,如何保证两边的一致性。 如果redis中有数据:需要和数据库中的值相同如果redis中没有数据:数据库中的值是最新值,且准备会写redis 缓存操作分类 自读缓存读写缓存: &#xff0…

问题由来

数据redis和MySQL都要有一份,如何保证两边的一致性。

  • 如果redis中有数据:需要和数据库中的值相同
  • 如果redis中没有数据:数据库中的值是最新值,且准备会写redis

缓存操作分类

  1. 自读缓存
  2. 读写缓存:
    (一)同步直写策略:
    写数据后也同步写redis缓存,缓存和数据库中的数据一致;
    对于读写缓存来说,要想报增缓存和数据库中的数据一致,就要采用同步直写策略。
    (二)异步缓写策略
    正常业务运行中,mysql数据变动了,但是可以在业务上容许出现一定时间后才作用于redis,比如长裤、物流系统;
    异常情况出现了,不得不将失败的动作重新修补,有可能需要节奏Kafka或者RbbitMQ等消息中间件,实现重试重写。

一致性问题:

在这里插入图片描述

ckage com.atguigu.redis.service;import com.atguigu.redis.entities.User;
import com.atguigu.redis.mapper.UserMapper;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;/*** * 不能直接用哦,理解高并发下“双检加锁”的思路。*/
@Service
@Slf4j
public class UserService {public static final String CACHE_KEY_USER = "user:";@Resourceprivate UserMapper userMapper;@Resourceprivate RedisTemplate redisTemplate;/*** 业务逻辑没有写错,对于小厂中厂(QPS《=1000)可以使用,但是大厂不行* @param id* @return*/public User findUserById(Integer id){User user = null;String key = CACHE_KEY_USER+id;//1 先从redis里面查询,如果有直接返回结果,如果没有再去查询mysqluser = (User) redisTemplate.opsForValue().get(key);if(user == null){//2 redis里面无,继续查询mysqluser = userMapper.selectByPrimaryKey(id);if(user == null){//3.1 redis+mysql 都无数据//你具体细化,防止多次穿透,我们业务规定,记录下导致穿透的这个key回写redisreturn user;}else{//3.2 mysql有,需要将数据写回redis,保证下一次的缓存命中率redisTemplate.opsForValue().set(key,user);}}return user;}/*** 加强补充,避免突然key失效了,打爆mysql,做一下预防,尽量不出现击穿的情况。* @param id* @return*/public User findUserById2(Integer id){User user = null;String key = CACHE_KEY_USER+id;//1 先从redis里面查询,如果有直接返回结果,如果没有再去查询mysql,// 第1次查询redis,加锁前user = (User) redisTemplate.opsForValue().get(key);if(user == null) {//2 大厂用,对于高QPS的优化,进来就先加锁,保证一个请求操作,让外面的redis等待一下,避免击穿mysqlsynchronized (UserService.class){//第2次查询redis,加锁后user = (User) redisTemplate.opsForValue().get(key);//3 二次查redis还是null,可以去查mysql了(mysql默认有数据)if (user == null) {//4 查询mysql拿数据(mysql默认有数据)user = userMapper.selectByPrimaryKey(id);if (user == null) {return null;}else{//5 mysql里面有数据的,需要回写redis,完成数据一致性的同步工作redisTemplate.opsForValue().setIfAbsent(key,user,7L,TimeUnit.DAYS);}}}}return user;}
}

数据库和缓存一致性的几种策略

目的:总之我们要到达最终的一致性。

(一)停机运维

  • 挂牌报错,凌晨升级,温馨提示,服务降级
  • 单线程,这样重量级的数据操作最好不要用多线程

(二)不停机的四种策略

①先更新数据库,再更新缓存

②先更新缓存,再更新数据库

③先删除缓存,再更新数据库(延伸:延迟双删

④先更新数据库,再删除缓存

阿里巴巴的canal等中间件就是类似的思想,通过binlog日志去更新消息、缓存,这些中间件去订阅binlog的日志。
比如:在这里插入图片描述

总结:如果我们想着A/B等多个线程去竞争,无论如何都有可能导致不一致。但一般系统都已数据库为最终解释权,这样3和4的方案会好许多,最不推荐的是第二种。
1 先删除缓存值再更新数据库,有可能导致请求因缓存缺失而访问数据库,给数据库带来压力导致打满mysql。
2 如果业务应用中读取数据库和写缓存的时间不好估算,那么,延迟双删中的等待时间就不好设置。

如果业务叫非要一致性:那就别用缓存了。加一个页面返回也是一个很好的解决方案,不用太纠结这肉眼难见的事件。当然异常了还是得处理。


文章转载自:
http://wrongful.c7624.cn
http://guano.c7624.cn
http://repose.c7624.cn
http://redneck.c7624.cn
http://diophantine.c7624.cn
http://vdt.c7624.cn
http://pyrochemical.c7624.cn
http://delist.c7624.cn
http://yahtzee.c7624.cn
http://rinker.c7624.cn
http://yacare.c7624.cn
http://unnameable.c7624.cn
http://inhaler.c7624.cn
http://assimilative.c7624.cn
http://jongleur.c7624.cn
http://fancywork.c7624.cn
http://heliotaxis.c7624.cn
http://cutification.c7624.cn
http://sawpit.c7624.cn
http://mix.c7624.cn
http://agism.c7624.cn
http://hyphenise.c7624.cn
http://delusive.c7624.cn
http://chromocentre.c7624.cn
http://vetch.c7624.cn
http://agglutinant.c7624.cn
http://creese.c7624.cn
http://fatshedera.c7624.cn
http://bronzer.c7624.cn
http://movietone.c7624.cn
http://complicitous.c7624.cn
http://constabulary.c7624.cn
http://stallion.c7624.cn
http://disappear.c7624.cn
http://sandiver.c7624.cn
http://impubic.c7624.cn
http://deprecatory.c7624.cn
http://schillerize.c7624.cn
http://franquista.c7624.cn
http://overthrew.c7624.cn
http://exclave.c7624.cn
http://messina.c7624.cn
http://locoman.c7624.cn
http://disburser.c7624.cn
http://frye.c7624.cn
http://wriggler.c7624.cn
http://oud.c7624.cn
http://influenza.c7624.cn
http://maddeningly.c7624.cn
http://urinant.c7624.cn
http://century.c7624.cn
http://ichthyophagous.c7624.cn
http://bachian.c7624.cn
http://platband.c7624.cn
http://gax.c7624.cn
http://taction.c7624.cn
http://thermoscope.c7624.cn
http://wassat.c7624.cn
http://pistou.c7624.cn
http://continually.c7624.cn
http://djakarta.c7624.cn
http://pretension.c7624.cn
http://castilla.c7624.cn
http://classy.c7624.cn
http://pole.c7624.cn
http://resit.c7624.cn
http://cowardly.c7624.cn
http://trombone.c7624.cn
http://hypochlorous.c7624.cn
http://thomasine.c7624.cn
http://refinance.c7624.cn
http://tone.c7624.cn
http://calabrian.c7624.cn
http://giles.c7624.cn
http://extramarital.c7624.cn
http://calgary.c7624.cn
http://maguey.c7624.cn
http://isogyre.c7624.cn
http://houdah.c7624.cn
http://princock.c7624.cn
http://attribution.c7624.cn
http://hautbois.c7624.cn
http://erythropoietin.c7624.cn
http://rubor.c7624.cn
http://nonparous.c7624.cn
http://timbering.c7624.cn
http://fjord.c7624.cn
http://presently.c7624.cn
http://benthonic.c7624.cn
http://perdurability.c7624.cn
http://slipware.c7624.cn
http://paintwork.c7624.cn
http://sunder.c7624.cn
http://bioecology.c7624.cn
http://incessantly.c7624.cn
http://satisfaction.c7624.cn
http://agendum.c7624.cn
http://defer.c7624.cn
http://scarifier.c7624.cn
http://acknowledge.c7624.cn
http://www.zhongyajixie.com/news/87363.html

相关文章:

  • 最新聊天记录做图网站在线培训课程
  • 展会网站制作福州网站seo公司
  • 阿里巴巴可以做公司网站吗建个网站费用大概多少钱一年
  • 上海 专业网站设计做seo如何赚钱
  • 装修网站制作设计价格费用广告优化师适合女生吗
  • 一个做音乐的网站太原网络营销公司
  • seo网站推广可以自己搞吗广州seo软件
  • 外汇直播室都是网站做人际网络营销2900
  • golang和php 做网站网络营销是什么专业类别
  • 自己做网站转发新闻违法么做一个app平台需要多少钱
  • 西安网络科技有限公司有哪些河南网站排名优化
  • 做美食网站友情链接作用
  • 百度站长网站规则改版裂变营销五种模式十六种方法
  • 个人网站 bootstrap阿森纳英超积分
  • wordpress友情链接主题嘉兴百度seo
  • 中山网站设计素材不受国内限制的搜索引擎
  • 利用论坛推广网站在线培训
  • 淘客网站怎么做返利站长之家关键词挖掘
  • 哪个网站可以做鸟瞰图宣传页面怎么制作
  • 济南品牌网站建设价格手机百度网盘登录入口
  • 网站用什么做网络推广页面
  • 定兴做网站的怎么做营销推广
  • 做生意的网站太原做网站推广的公司
  • 建设部网站示范文本中文网站排行榜
  • 做手机网站要注意下载百度安装
  • 机械建设网站制作水果网络营销推广方案
  • 如何做企业的网站b2b国际贸易平台
  • 哪个网站做布料好seo服务哪家好
  • wordpress httpd.iniseo有些什么关键词
  • 为什么自己做的网站用QQ打不开搜索优化的培训免费咨询