婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > 詳解redis緩存與數(shù)據(jù)庫(kù)一致性問(wèn)題解決

詳解redis緩存與數(shù)據(jù)庫(kù)一致性問(wèn)題解決

熱門(mén)標(biāo)簽:鄭州人工智能電銷(xiāo)機(jī)器人系統(tǒng) 北京400電話(huà)辦理收費(fèi)標(biāo)準(zhǔn) 十堰營(yíng)銷(xiāo)電銷(xiāo)機(jī)器人哪家便宜 魔獸2青云地圖標(biāo)注 日本中國(guó)地圖標(biāo)注 貴州電銷(xiāo)卡外呼系統(tǒng) 山東外呼銷(xiāo)售系統(tǒng)招商 超呼電話(huà)機(jī)器人 宿遷便宜外呼系統(tǒng)平臺(tái)

數(shù)據(jù)庫(kù)與緩存讀寫(xiě)模式策略

寫(xiě)完數(shù)據(jù)庫(kù)后是否需要馬上更新緩存還是直接刪除緩存?

(1)、如果寫(xiě)數(shù)據(jù)庫(kù)的值與更新到緩存值是一樣的,不需要經(jīng)過(guò)任何的計(jì)算,可以馬上更新緩存,但是如果對(duì)于那種寫(xiě)數(shù)據(jù)頻繁而讀數(shù)據(jù)少的場(chǎng)景并不合適這種解決方案,因?yàn)橐苍S還沒(méi)有查詢(xún)就被刪除或修改了,這樣會(huì)浪費(fèi)時(shí)間和資源

(2)、如果寫(xiě)數(shù)據(jù)庫(kù)的值與更新緩存的值不一致,寫(xiě)入緩存中的數(shù)據(jù)需要經(jīng)過(guò)幾個(gè)表的關(guān)聯(lián)計(jì)算后得到的結(jié)果插入緩存中,那就沒(méi)有必要馬上更新緩存,只有刪除緩存即可,等到查詢(xún)的時(shí)候在去把計(jì)算后得到的結(jié)果插入到緩存中即可。

所以一般的策略是當(dāng)更新數(shù)據(jù)時(shí),先刪除緩存數(shù)據(jù),然后更新數(shù)據(jù)庫(kù),而不是更新緩存,等要查詢(xún)的時(shí)候才把最新的數(shù)據(jù)更新到緩存

數(shù)據(jù)庫(kù)與緩存雙寫(xiě)情況下導(dǎo)致數(shù)據(jù)不一致問(wèn)題

場(chǎng)景一
當(dāng)更新數(shù)據(jù)時(shí),如更新某商品的庫(kù)存,當(dāng)前商品的庫(kù)存是100,現(xiàn)在要更新為99,先更新數(shù)據(jù)庫(kù)更改成99,然后刪除緩存,發(fā)現(xiàn)刪除緩存失敗了,這意味著數(shù)據(jù)庫(kù)存的是99,而緩存是100,這導(dǎo)致數(shù)據(jù)庫(kù)和緩存不一致。

場(chǎng)景一解決方案 

這種情況應(yīng)該是先刪除緩存,然后在更新數(shù)據(jù)庫(kù),如果刪除緩存失敗,那就不要更新數(shù)據(jù)庫(kù),如果說(shuō)刪除緩存成功,而更新數(shù)據(jù)庫(kù)失敗,那查詢(xún)的時(shí)候只是從數(shù)據(jù)庫(kù)里查了舊的數(shù)據(jù)而已,這樣就能保持?jǐn)?shù)據(jù)庫(kù)與緩存的一致性。

場(chǎng)景二
在高并發(fā)的情況下,如果當(dāng)刪除完緩存的時(shí)候,這時(shí)去更新數(shù)據(jù)庫(kù),但還沒(méi)有更新完,另外一個(gè)請(qǐng)求來(lái)查詢(xún)數(shù)據(jù),發(fā)現(xiàn)緩存里沒(méi)有,就去數(shù)據(jù)庫(kù)里查,還是以上面商品庫(kù)存為例,如果數(shù)據(jù)庫(kù)中產(chǎn)品的庫(kù)存是100,那么查詢(xún)到的庫(kù)存是100,然后插入緩存,插入完緩存后,原來(lái)那個(gè)更新數(shù)據(jù)庫(kù)的線程把數(shù)據(jù)庫(kù)更新為了99,導(dǎo)致數(shù)據(jù)庫(kù)與緩存不一致的情況

場(chǎng)景二解決方案
遇到這種情況,可以用隊(duì)列的去解決這個(gè)問(wèn),創(chuàng)建幾個(gè)隊(duì)列,如20個(gè),根據(jù)商品的ID去做hash值,然后對(duì)隊(duì)列個(gè)數(shù)取摸,當(dāng)有數(shù)據(jù)更新請(qǐng)求時(shí),先把它丟到隊(duì)列里去,當(dāng)更新完后在從隊(duì)列里去除,如果在更新的過(guò)程中,遇到以上場(chǎng)景,先去緩存里看下有沒(méi)有數(shù)據(jù),如果沒(méi)有,可以先去隊(duì)列里看是否有相同商品ID在做更新,如果有也把查詢(xún)的請(qǐng)求發(fā)送到隊(duì)列里去,然后同步等待緩存更新完成。
這里有一個(gè)優(yōu)化點(diǎn),如果發(fā)現(xiàn)隊(duì)列里有一個(gè)查詢(xún)請(qǐng)求了,那么就不要放新的查詢(xún)操作進(jìn)去了,用一個(gè)while(true)循環(huán)去查詢(xún)緩存,循環(huán)個(gè)200MS左右,如果緩存里還沒(méi)有則直接取數(shù)據(jù)庫(kù)的舊數(shù)據(jù),一般情況下是可以取到的。

在高并發(fā)下解決場(chǎng)景二要注意的問(wèn)題

(1)讀請(qǐng)求時(shí)長(zhǎng)阻塞
 由于讀請(qǐng)求進(jìn)行了非常輕度的異步化,所以一定要注意讀超時(shí)的問(wèn)題,每個(gè)讀請(qǐng)求必須在超時(shí)間內(nèi)返回,該解決方案最大的風(fēng)險(xiǎn)在于可能數(shù)據(jù)更新很頻繁,導(dǎo)致隊(duì)列中擠壓了大量的更新操作在里面,然后讀請(qǐng)求會(huì)發(fā)生大量的超時(shí),最后導(dǎo)致大量的請(qǐng)求直接走數(shù)據(jù)庫(kù),像遇到這種情況,一般要做好足夠的壓力測(cè)試,如果壓力過(guò)大,需要根據(jù)實(shí)際情況添加機(jī)器。
(2)請(qǐng)求并發(fā)量過(guò)高
 這里還是要做好壓力測(cè)試,多模擬真實(shí)場(chǎng)景,并發(fā)量在最高的時(shí)候QPS多少,扛不住就要多加機(jī)器,還有就是做好讀寫(xiě)比例是多少
(3)多服務(wù)實(shí)例部署的請(qǐng)求路由
可能這個(gè)服務(wù)部署了多個(gè)實(shí)例,那么必須保證說(shuō),執(zhí)行數(shù)據(jù)更新操作,以及執(zhí)行緩存更新操作的請(qǐng)求,都通過(guò)nginx服務(wù)器路由到相同的服務(wù)實(shí)例上
(4)熱點(diǎn)商品的路由問(wèn)題,導(dǎo)致請(qǐng)求的傾斜
某些商品的讀請(qǐng)求特別高,全部打到了相同的機(jī)器的相同丟列里了,可能造成某臺(tái)服務(wù)器壓力過(guò)大,因?yàn)橹挥性谏唐窋?shù)據(jù)更新的時(shí)候才會(huì)清空緩存,然后才會(huì)導(dǎo)致讀寫(xiě)并發(fā),所以更新頻率不是太高的話(huà),這個(gè)問(wèn)題的影響并不是很大,但是確實(shí)有可能某些服務(wù)器的負(fù)載會(huì)高一些。

數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性解決方案流程圖

數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性解決方案對(duì)應(yīng)代碼

商品庫(kù)存實(shí)體

package com.shux.inventory.entity;
/**
 **********************************************
 * 描述:
 * Simba.Hua
 * 2017年8月30日
 **********************************************
**/
public class InventoryProduct {
 private Integer productId;
 private Long InventoryCnt;
 
 public Integer getProductId() {
  return productId;
 }
 public void setProductId(Integer productId) {
  this.productId = productId;
 }
 public Long getInventoryCnt() {
  return InventoryCnt;
 }
 public void setInventoryCnt(Long inventoryCnt) {
  InventoryCnt = inventoryCnt;
 }
 
}

請(qǐng)求接口

/**
 **********************************************
 * 描述:
 * Simba.Hua
 * 2017年8月27日
 **********************************************
**/
public interface Request {
 public void process();
 public Integer getProductId();
 public boolean isForceFefresh();
}

數(shù)據(jù)更新請(qǐng)求

package com.shux.inventory.request;
 
import org.springframework.transaction.annotation.Transactional;
 
import com.shux.inventory.biz.InventoryProductBiz;
import com.shux.inventory.entity.InventoryProduct;
 
/**
 **********************************************
 * 描述:更新庫(kù)存信息
 * 1、先刪除緩存中的數(shù)據(jù)
 * 2、更新數(shù)據(jù)庫(kù)中的數(shù)據(jù)
 * Simba.Hua
 * 2017年8月30日
 **********************************************
**/
public class InventoryUpdateDBRequest implements Request{
 private InventoryProductBiz inventoryProductBiz;
 private InventoryProduct inventoryProduct;
 
 public InventoryUpdateDBRequest(InventoryProduct inventoryProduct,InventoryProductBiz inventoryProductBiz){
  this.inventoryProduct = inventoryProduct;
  this.inventoryProductBiz = inventoryProductBiz;
 }
 @Override
 @Transactional
 public void process() {
  inventoryProductBiz.removeInventoryProductCache(inventoryProduct.getProductId());
  inventoryProductBiz.updateInventoryProduct(inventoryProduct);
 }
 @Override
 public Integer getProductId() {
  // TODO Auto-generated method stub
  return inventoryProduct.getProductId();
 }
 @Override
 public boolean isForceFefresh() {
  // TODO Auto-generated method stub
  return false;
 }
 
}

查詢(xún)請(qǐng)求

package com.shux.inventory.request;
 
import com.shux.inventory.biz.InventoryProductBiz;
import com.shux.inventory.entity.InventoryProduct;
 
/**
 **********************************************
 * 描述:查詢(xún)緩存數(shù)據(jù)
 * 1、從數(shù)據(jù)庫(kù)中查詢(xún)
 * 2、從數(shù)據(jù)庫(kù)中查詢(xún)后插入到緩存中
 * Simba.Hua
 * 2017年8月30日
 **********************************************
**/
public class InventoryQueryCacheRequest implements Request {
 private InventoryProductBiz inventoryProductBiz;
 private Integer productId;
 private boolean isForceFefresh;
 
 public InventoryQueryCacheRequest(Integer productId,InventoryProductBiz inventoryProductBiz,boolean isForceFefresh) {
  this.productId = productId;
  this.inventoryProductBiz = inventoryProductBiz;
  this.isForceFefresh = isForceFefresh;
 }
 @Override
 public void process() {
  InventoryProduct inventoryProduct = inventoryProductBiz.loadInventoryProductByProductId(productId);
  inventoryProductBiz.setInventoryProductCache(inventoryProduct);
 }
 @Override
 public Integer getProductId() {
  // TODO Auto-generated method stub
  return productId;
 }
 public boolean isForceFefresh() {
  return isForceFefresh;
 }
 public void setForceFefresh(boolean isForceFefresh) {
  this.isForceFefresh = isForceFefresh;
 }
 
}

spring啟動(dòng)時(shí)初始化隊(duì)列線程池

package com.shux.inventory.thread;
 
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
import com.shux.inventory.request.Request;
import com.shux.inventory.request.RequestQueue;
import com.shux.utils.other.SysConfigUtil;
 
/**
 **********************************************
 * 描述:請(qǐng)求處理線程池,初始化隊(duì)列數(shù)及每個(gè)隊(duì)列最多能處理的數(shù)量
 * Simba.Hua
 * 2017年8月27日
 **********************************************
**/
public class RequestProcessorThreadPool {
 private static final int blockingQueueNum = SysConfigUtil.get("request.blockingqueue.number")==null?10:Integer.valueOf(SysConfigUtil.get("request.blockingqueue.number").toString());
 private static final int queueDataNum = SysConfigUtil.get("request.everyqueue.data.length")==null?100:Integer.valueOf(SysConfigUtil.get("request.everyqueue.data.length").toString());
 private ExecutorService threadPool = Executors.newFixedThreadPool(blockingQueueNum);
 private RequestProcessorThreadPool(){
  for(int i=0;iblockingQueueNum;i++){//初始化隊(duì)列
   ArrayBlockingQueueRequest> queue = new ArrayBlockingQueueRequest>(queueDataNum);//每個(gè)隊(duì)列中放100條數(shù)據(jù)
   RequestQueue.getInstance().addQueue(queue);
   threadPool.submit(new RequestProcessorThread(queue));//把每個(gè)queue交個(gè)線程去處理,線程會(huì)處理每個(gè)queue中的數(shù)據(jù)
  }
 }
 public static class Singleton{
  private static RequestProcessorThreadPool instance;
  static{
   instance = new RequestProcessorThreadPool();
  }
  public static RequestProcessorThreadPool getInstance(){
   return instance;
  }
 }
 public static RequestProcessorThreadPool getInstance(){
  return Singleton.getInstance();
 }
 /**
  * 初始化線程池
  */
 public static void init(){
  getInstance();
 }
}

請(qǐng)求處理線程

package com.shux.inventory.thread;
 
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
 
import com.shux.inventory.request.InventoryUpdateDBRequest;
import com.shux.inventory.request.Request;
import com.shux.inventory.request.RequestQueue;
 
/**
 **********************************************
 * 描述:請(qǐng)求處理線程
 * Simba.Hua
 * 2017年8月27日
 **********************************************
**/
public class RequestProcessorThread implements CallableBoolean>{
 private ArrayBlockingQueueRequest> queue;
 public RequestProcessorThread(ArrayBlockingQueueRequest> queue){
  this.queue = queue;
 }
 @Override
 public Boolean call() throws Exception {
  Request request = queue.take();
  MapInteger,Boolean> flagMap = RequestQueue.getInstance().getFlagMap();
  //不需要強(qiáng)制刷新的時(shí)候,查詢(xún)請(qǐng)求去重處理
   if (!request.isForceFefresh()){
    if (request instanceof InventoryUpdateDBRequest) {//如果是更新請(qǐng)求,那就置為false
     flagMap.put(request.getProductId(), true);
    } else {
     Boolean flag = flagMap.get(request.getProductId());
     /**
     * 標(biāo)志位為空,有三種情況
     * 1、沒(méi)有過(guò)更新請(qǐng)求
     * 2、沒(méi)有查詢(xún)請(qǐng)求
     * 3、數(shù)據(jù)庫(kù)中根本沒(méi)有數(shù)據(jù)
     * 在最初情況,一旦庫(kù)存了插入了數(shù)據(jù),那就好會(huì)在緩存中也會(huì)放一份數(shù)據(jù),
     * 但這種情況下有可能由于redis中內(nèi)存滿(mǎn)了,redis通過(guò)LRU算法把這個(gè)商品給清除了,導(dǎo)致緩存中沒(méi)有數(shù)據(jù)
     * 所以當(dāng)標(biāo)志位為空的時(shí)候,需要從數(shù)據(jù)庫(kù)重查詢(xún)一次,并且把標(biāo)志位置為false,以便后面的請(qǐng)求能夠從緩存中取
     */
     if ( flag == null) {
      flagMap.put(request.getProductId(), false);
     }
     /**
     * 如果不為空,并且flag為true,說(shuō)明之前有一次更新請(qǐng)求,說(shuō)明緩存中沒(méi)有數(shù)據(jù)了(更新緩存會(huì)先刪除緩存),
     * 這個(gè)時(shí)候就要去刷新緩存,即從數(shù)據(jù)庫(kù)中查詢(xún)一次,并把標(biāo)志位設(shè)置為false
     */
     if ( flag != null  flag) {
      flagMap.put(request.getProductId(), false);
     }
     /**
     * 這種情況說(shuō)明之前有一個(gè)查詢(xún)請(qǐng)求,并且把數(shù)據(jù)刷新到了緩存中,所以這時(shí)候就不用去刷新緩存了,直接返回就可以了
     */
     if (flag != null  !flag) {
      flagMap.put(request.getProductId(), false);
      return true;
     } 
    }
   }
   request.process();
  return true;
 } 
}

請(qǐng)求隊(duì)列

package com.shux.inventory.request;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 **********************************************
 * 描述:請(qǐng)求隊(duì)列
 * Simba.Hua
 * 2017年8月27日
 **********************************************
**/
public class RequestQueue {
 private ListArrayBlockingQueueRequest>> queues = new ArrayList>();
 
 private MapInteger,Boolean> flagMap = new ConcurrentHashMap>();
 private RequestQueue(){
  
 }
 private static class Singleton{
  private static RequestQueue queue;
  static{
   queue = new RequestQueue();
  }
  public static RequestQueue getInstance() {
   return queue;
  }
 }
 
 public static RequestQueue getInstance(){
  return Singleton.getInstance();
 }
 public void addQueue(ArrayBlockingQueueRequest> queue) {
  queues.add(queue);
 }
 
 public int getQueueSize(){
  return queues.size();
 }
 public ArrayBlockingQueueRequest> getQueueByIndex(int index) {
  return queues.get(index);
 }
 
 public MapInteger,Boolean> getFlagMap() {
  return this.flagMap;
 }
}

spring 啟動(dòng)初始化線程池類(lèi) 

package com.shux.inventory.listener;
 
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
 
import com.shux.inventory.thread.RequestProcessorThreadPool;
 
/**
 **********************************************
 * 描述:spring 啟動(dòng)初始化線程池類(lèi)
 * Simba.Hua
 * 2017年8月27日
 **********************************************
**/
public class InitListener implements ApplicationListenerContextRefreshedEvent>{
 
 @Override
 public void onApplicationEvent(ContextRefreshedEvent event) {
  // TODO Auto-generated method stub
  if(event.getApplicationContext().getParent() != null){
   return;
  }
  RequestProcessorThreadPool.init();
 }
}

異步處理請(qǐng)求接口

package com.shux.inventory.biz; 
import com.shux.inventory.request.Request;
 
/**
 **********************************************
 * 描述:請(qǐng)求異步處理接口,用于路由隊(duì)列并把請(qǐng)求加入到隊(duì)列中
 * Simba.Hua
 * 2017年8月30日
 **********************************************
**/
public interface IRequestAsyncProcessBiz {
 void process(Request request);
}

異步處理請(qǐng)求接口實(shí)現(xiàn)

package com.shux.inventory.biz.impl;
 
import java.util.concurrent.ArrayBlockingQueue;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
 
import com.shux.inventory.biz.IRequestAsyncProcessBiz;
import com.shux.inventory.request.Request;
import com.shux.inventory.request.RequestQueue;
 
 
/**
 **********************************************
 * 描述:異步處理請(qǐng)求,用于路由隊(duì)列并把請(qǐng)求加入到隊(duì)列中
 * Simba.Hua
 * 2017年8月30日
 **********************************************
**/
@Service("requestAsyncProcessService")
public class RequestAsyncProcessBizImpl implements IRequestAsyncProcessBiz {
 private Logger logger = LoggerFactory.getLogger(getClass());
 @Override
 public void process(Request request) {
  // 做請(qǐng)求的路由,根據(jù)productId路由到對(duì)應(yīng)的隊(duì)列
  ArrayBlockingQueueRequest> queue = getQueueByProductId(request.getProductId());
  try {
   queue.put(request);
  } catch (InterruptedException e) {
   logger.error("產(chǎn)品ID{}加入隊(duì)列失敗",request.getProductId(),e);
  }
 }
 
 private ArrayBlockingQueueRequest> getQueueByProductId(Integer productId) {
  RequestQueue requestQueue = RequestQueue.getInstance();
  String key = String.valueOf(productId);
  int hashcode;
  int hash = (key == null) ? 0 : (hashcode = key.hashCode())^(hashcode >>> 16);
  //對(duì)hashcode取摸
  int index = (requestQueue.getQueueSize()-1)  hash;
  return requestQueue.getQueueByIndex(index);
 }
}
package com.shux.inventory.biz.impl; 
import javax.annotation.Resource;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.shux.inventory.biz.InventoryProductBiz;
import com.shux.inventory.entity.InventoryProduct;
import com.shux.inventory.mapper.InventoryProductMapper;
import com.shux.redis.biz.IRedisBiz;
 
/**
 **********************************************
 * 描述
 * Simba.Hua
 * 2017年8月30日
 **********************************************
**/
@Service("inventoryProductBiz")
public class InventoryProductBizImpl implements InventoryProductBiz {
 private @Autowired IRedisBizInventoryProduct> redisBiz;
 private @Resource InventoryProductMapper mapper;
 @Override
 public void updateInventoryProduct(InventoryProduct inventoryProduct) {
  // TODO Auto-generated method stub
  mapper.updateInventoryProduct(inventoryProduct);
 }
 
 @Override
 public InventoryProduct loadInventoryProductByProductId(Integer productId) {
  // TODO Auto-generated method stub
  return mapper.loadInventoryProductByProductId(productId);
 }
 
 @Override
 public void setInventoryProductCache(InventoryProduct inventoryProduct) {
  redisBiz.set("inventoryProduct:"+inventoryProduct.getProductId(), inventoryProduct);
  
 }
 
 @Override
 public void removeInventoryProductCache(Integer productId) {
  redisBiz.delete("inventoryProduct:"+productId);
  
 }
 
 @Override
 public InventoryProduct loadInventoryProductCache(Integer productId) {
  // TODO Auto-generated method stub
  return redisBiz.get("inventoryProduct:"+productId);
 }
}

數(shù)據(jù)更新請(qǐng)求controller

package com.shux.inventory.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.shux.inventory.biz.IRequestAsyncProcessBiz;
import com.shux.inventory.biz.InventoryProductBiz;
import com.shux.inventory.entity.InventoryProduct;
import com.shux.inventory.request.InventoryUpdateDBRequest;
import com.shux.inventory.request.Request;
import com.shux.utils.other.Response;
 
/**
 **********************************************
 * 描述:提交更新請(qǐng)求
 * Simba.Hua
 * 2017年9月1日
 **********************************************
**/
@Controller("/inventory")
public class InventoryUpdateDBController {
 private @Autowired InventoryProductBiz inventoryProductBiz;
 private @Autowired IRequestAsyncProcessBiz requestAsyncProcessBiz;
 @RequestMapping("/updateDBInventoryProduct")
 @ResponseBody
 public Response updateDBInventoryProduct(InventoryProduct inventoryProduct){
  Request request = new InventoryUpdateDBRequest(inventoryProduct,inventoryProductBiz);
  requestAsyncProcessBiz.process(request);
  return new Response(Response.SUCCESS,"更新成功");
 }
}

數(shù)據(jù)查詢(xún)請(qǐng)求controller

package com.shux.inventory.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.shux.inventory.biz.IRequestAsyncProcessBiz;
import com.shux.inventory.biz.InventoryProductBiz;
import com.shux.inventory.entity.InventoryProduct;
import com.shux.inventory.request.InventoryQueryCacheRequest;
import com.shux.inventory.request.Request;
 
/**
 **********************************************
 * 描述:提交查詢(xún)請(qǐng)求
 * 1、先從緩存中取數(shù)據(jù)
 * 2、如果能從緩存中取到數(shù)據(jù),則返回
 * 3、如果不能從緩存取到數(shù)據(jù),則等待20毫秒,然后再次去數(shù)據(jù),直到200毫秒,如果超過(guò)200毫秒還不能取到數(shù)據(jù),則從數(shù)據(jù)庫(kù)中取,并強(qiáng)制刷新緩存數(shù)據(jù)
 * Simba.Hua
 * 2017年9月1日
 **********************************************
**/
@Controller("/inventory")
public class InventoryQueryCacheController {
 private @Autowired InventoryProductBiz inventoryProductBiz;
 private @Autowired IRequestAsyncProcessBiz requestAsyncProcessBiz;
 @RequestMapping("/queryInventoryProduct")
 public InventoryProduct queryInventoryProduct(Integer productId) {
   Request request = new InventoryQueryCacheRequest(productId,inventoryProductBiz,false);
   requestAsyncProcessBiz.process(request);//加入到隊(duì)列中
   long startTime = System.currentTimeMillis();
   long allTime = 0L;
   long endTime = 0L;
   InventoryProduct inventoryProduct = null;
   while (true) {
    if (allTime > 200){//如果超過(guò)了200ms,那就直接退出,然后從數(shù)據(jù)庫(kù)中查詢(xún)
     break;
    }
    try {
     inventoryProduct = inventoryProductBiz.loadInventoryProductCache(productId);
     if (inventoryProduct != null) {
      return inventoryProduct;
     } else {
      Thread.sleep(20);//如果查詢(xún)不到就等20毫秒
     } 
     endTime = System.currentTimeMillis();
     allTime = endTime - startTime;
    } catch (Exception e) {
    } 
   }
   /**
   * 代碼執(zhí)行到這來(lái),只有以下三種情況
   * 1、緩存中本來(lái)有數(shù)據(jù),由于redis內(nèi)存滿(mǎn)了,redis通過(guò)LRU算法清除了緩存,導(dǎo)致數(shù)據(jù)沒(méi)有了
   * 2、由于之前數(shù)據(jù)庫(kù)查詢(xún)比較慢或者內(nèi)存太小處理不過(guò)來(lái)隊(duì)列中的數(shù)據(jù),導(dǎo)致隊(duì)列里擠壓了很多的數(shù)據(jù),所以一直沒(méi)有從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)然后插入到緩存中
   * 3、數(shù)據(jù)庫(kù)中根本沒(méi)有這樣的數(shù)據(jù),這種情況叫數(shù)據(jù)穿透,一旦別人知道這個(gè)商品沒(méi)有,如果一直執(zhí)行查詢(xún),就會(huì)一直查詢(xún)數(shù)據(jù)庫(kù),如果過(guò)多,那么有可能會(huì)導(dǎo)致數(shù)據(jù)庫(kù)癱瘓
   */
   inventoryProduct = inventoryProductBiz.loadInventoryProductByProductId(productId);
   if (inventoryProduct != null) {
    Request forcRrequest = new InventoryQueryCacheRequest(productId,inventoryProductBiz,true);
    requestAsyncProcessBiz.process(forcRrequest);//這個(gè)時(shí)候需要強(qiáng)制刷新數(shù)據(jù)庫(kù),使緩存中有數(shù)據(jù)
    return inventoryProduct;
   }
   return null;
   
  }
}

到此這篇關(guān)于詳解redis緩存與數(shù)據(jù)庫(kù)一致性問(wèn)題解決的文章就介紹到這了,更多相關(guān)redis緩存與數(shù)據(jù)庫(kù)一致性?xún)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Redis緩存常用4種策略原理詳解
  • 聊一聊Redis與MySQL雙寫(xiě)一致性如何保證
  • MySQL與Redis如何保證數(shù)據(jù)一致性詳解
  • redis實(shí)現(xiàn)分布式的方法總結(jié)
  • 面試常問(wèn):如何保證Redis緩存和數(shù)據(jù)庫(kù)的數(shù)據(jù)一致性

標(biāo)簽:楊凌 朝陽(yáng) 吉安 果洛 北京 江蘇 臺(tái)州 大慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解redis緩存與數(shù)據(jù)庫(kù)一致性問(wèn)題解決》,本文關(guān)鍵詞  詳解,redis,緩存,與,數(shù)據(jù)庫(kù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳解redis緩存與數(shù)據(jù)庫(kù)一致性問(wèn)題解決》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于詳解redis緩存與數(shù)據(jù)庫(kù)一致性問(wèn)題解決的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    色综合久久久久| 久久国产精品一区二区| 日韩视频免费观看高清在线视频| 国产精品国产三级国产aⅴ原创| 奇米一区二区三区| 久久久美女毛片| 国产精品一卡二卡| 亚洲高清在线精品| 亚洲色图都市小说| 色94色欧美sute亚洲13| 亚洲综合清纯丝袜自拍| 欧美电影免费观看完整版| 91在线观看下载| 男男视频亚洲欧美| 国产精品美女久久福利网站| 91丨九色丨黑人外教| 国产日韩欧美综合在线| 色噜噜久久综合| 午夜伦理一区二区| 成人app在线| 久久精品国产99| 中文天堂在线一区| 欧美在线免费播放| 亚洲精品va在线观看| 日韩欧美一级二级三级久久久| 婷婷激情综合网| 国产麻豆视频一区| 免费成人结看片| 成人免费高清视频在线观看| 香蕉影视欧美成人| 三级欧美在线一区| 亚洲美女区一区| 中文久久乱码一区二区| 中文字幕一区不卡| 欧美激情一区二区三区| 欧美色爱综合网| 国产尤物一区二区| 亚洲午夜激情网站| 久久久久99精品一区| 亚洲v中文字幕| 日韩精品一区二区三区视频| 99视频热这里只有精品免费| 午夜视频一区在线观看| 亚洲精品乱码久久久久| 精品对白一区国产伦| 欧美色精品天天在线观看视频| 一区二区久久久| 欧美色涩在线第一页| 欧美一级二级在线观看| 精一区二区三区| 综合久久综合久久| 99久久99久久精品免费观看| 一本色道a无线码一区v| 国产麻豆一精品一av一免费 | 亚洲高清视频中文字幕| 91精品欧美久久久久久动漫| 欧美在线观看视频一区二区| 日韩av中文在线观看| 97精品久久久午夜一区二区三区| 亚洲免费在线视频| 久久99精品久久久久久久久久久久 | 成人久久18免费网站麻豆| 国产真实乱偷精品视频免| 日本特黄久久久高潮| 蜜臀99久久精品久久久久久软件| 国产精品不卡在线| 欧美美女一区二区在线观看| 国产精品久久午夜夜伦鲁鲁| av电影在线观看一区| 国产精品久久久久久久岛一牛影视 | 亚洲激情在线激情| 精品国偷自产国产一区| 欧美巨大另类极品videosbest| 欧美美女bb生活片| 综合av第一页| 日韩激情在线观看| 国产成人自拍网| 国产成人精品三级麻豆| av亚洲精华国产精华| 色婷婷国产精品| 91日韩精品一区| 51精品视频一区二区三区| 日韩欧美黄色影院| 日韩欧美的一区| aaa亚洲精品| 欧美久久久久久久久中文字幕| 欧亚洲嫩模精品一区三区| 另类欧美日韩国产在线| 国产精品久久久久aaaa樱花| 在线观看视频91| 成人av电影在线网| 欧美精品成人一区二区三区四区| 亚洲青青青在线视频| 美国一区二区三区在线播放| av一区二区三区| 亚洲制服丝袜在线| 国产成人8x视频一区二区| 国产一区二区视频在线| 久久久久国产精品麻豆| 久久婷婷国产综合国色天香| 午夜精品一区二区三区免费视频| 亚洲一区二区三区小说| 国产一区二区精品在线观看| 欧美性色aⅴ视频一区日韩精品| 99精品久久免费看蜜臀剧情介绍| 91精品国产综合久久国产大片| 精品在线播放午夜| 91麻豆国产香蕉久久精品| 欧美视频自拍偷拍| 日本不卡高清视频| 天堂蜜桃91精品| 裸体健美xxxx欧美裸体表演| 久久综合av免费| 欧美日韩aaaaaa| 成人av网站大全| 久久国产人妖系列| 日韩理论片中文av| 日韩精品综合一本久道在线视频| 91视频观看视频| 中文字幕一区av| 蜜桃视频一区二区三区| www.成人网.com| 国产精品素人视频| 亚洲国产成人午夜在线一区| 国产日韩欧美综合在线| 欧美艳星brazzers| 午夜精品久久一牛影视| 亚洲人成网站色在线观看| 国产精品久久久久久户外露出 | 日本va欧美va瓶| 99麻豆久久久国产精品免费 | 国产欧美日韩在线看| 国产丝袜欧美中文另类| 日本欧美加勒比视频| 欧美美女一区二区三区| 亚洲一级二级三级| 欧美日韩在线播| 久久影院午夜片一区| 精品久久久久一区二区国产| 亚洲女同一区二区| 欧美日韩一区二区欧美激情| 亚洲一二三四区不卡| 欧美色倩网站大全免费| 色天天综合久久久久综合片| 欧美国产亚洲另类动漫| 成人v精品蜜桃久久一区| 日韩电影在线一区二区| 欧美中文字幕一二三区视频| 精品久久久久香蕉网| 国产麻豆精品一区二区| 一区二区三区丝袜| 国产一区999| 国产欧美一区二区三区鸳鸯浴| 久久精品欧美日韩| 久久精品亚洲麻豆av一区二区| 欧美精品丝袜中出| 日韩**一区毛片| 蜜桃久久精品一区二区| 日韩欧美一区在线观看| 欧美日韩黄色一区二区| 91麻豆精品国产综合久久久久久| 欧美亚洲禁片免费| 国产精品一级二级三级| 91精品久久久久久久91蜜桃 | 亚洲五码中文字幕| 久久亚区不卡日本| 国内精品在线播放| 欧美不卡激情三级在线观看| 日本不卡在线视频| 国产精品丝袜在线| 欧美一区二区三区色| 日韩你懂的电影在线观看| 国产精品中文有码| 午夜不卡在线视频| 精品99久久久久久| 日韩一二在线观看| 色综合色狠狠综合色| 亚洲v中文字幕| 亚洲激情校园春色| 国产精品女同一区二区三区| 精品国产乱码久久久久久久久| 1区2区3区欧美| 中文字幕日韩精品一区| 1024成人网色www| 亚洲天堂av一区| 国产成人在线网站| 国产成人在线观看免费网站| 韩日av一区二区| 国产白丝精品91爽爽久久| 欧美一级理论性理论a| 久久无码av三级| 中文字幕五月欧美| 日韩免费观看2025年上映的电影| 欧美不卡123| 国产精品免费看片| 成人黄色小视频在线观看| 国产婷婷精品av在线| 偷拍日韩校园综合在线| 在线一区二区视频| 国产在线一区二区|