­

redis缓存穿透,缓存击穿,缓存雪崩原因+解决方案

  • 2019 年 10 月 3 日
  • 筆記

????Java.md

????

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????/?????????????????????????????????????????????????????/???????????????????????????????????????????????????

?????????????????NoSQL?????????????????????????????

redis????NoSQL???????????redis?????????????????????????????????????????

?????

  • ?????key???????????????????key?????????????????????????????????????????id????????????????????????????????????????
  • ?????key??????????redis??????????????????????????????????DB?????????????????????????????DB???
  • ???????????????????????????????????????????????(??DB)???????

??????????

??????????????????????????????????????????????????????????????????????????????????????????????

?????????????????????????????????????????????????????bitmap?????????????? ??bitmap???????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????

//???  public object GetProductListNew() {      int cacheTime = 30;      String cacheKey = "product_list";        String cacheValue = CacheHelper.Get(cacheKey);      if (cacheValue != null) {          return cacheValue;      }        cacheValue = CacheHelper.Get(cacheKey);      if (cacheValue != null) {          return cacheValue;      } else {          //??????????          cacheValue = GetProductListFromDB();          if (cacheValue == null) {              //???????????????????              cacheValue = string.Empty;          }          CacheHelper.Add(cacheKey, cacheValue, cacheTime);          return cacheValue;      }  }

??????????

key???????????????????????“??”?????????????????????“??”????

?????(mutex key)

?????????????mutex??????????????????????????????????load db???????????????????????????Redis?SETNX??Memcache?ADD??set??mutex key?????????????load db?????????????????get??????

SETNX???SET if Not eXists?????????????????????????????????

public String get(key) {        String value = redis.get(key);        if (value == null) { //???????            //??3min??????del??????????????????load db        if (redis.setnx(key_mutex, 1, 3 * 60) == 1) {  //??????                 value = db.get(key);                        redis.set(key, value, expire_secs);                        redis.del(key_mutex);                } else {  //????????????????load db????????????????????                        sleep(50);                        get(key);  //??                }            } else {                return value;            }   }

memcache???

if (memcache.get(key) == null) {      // 3 min timeout to avoid mutex holder crash      if (memcache.add(key_mutex, 3 * 60 * 1000) == true) {          value = db.get(key);          memcache.set(key, value);          memcache.delete(key_mutex);      } else {          sleep(50);          retry();      }  }

???????????

??????????

????????????????key??????????key?

?????Redis??????????
redis1.md

????????????
redis2.md

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1-5???????????????????????????????????????

???????????

//???  public object GetProductListNew() {      int cacheTime = 30;      String cacheKey = "product_list";      String lockKey = cacheKey;        String cacheValue = CacheHelper.get(cacheKey);      if (cacheValue != null) {          return cacheValue;      } else {          synchronized(lockKey) {              cacheValue = CacheHelper.get(cacheKey);              if (cacheValue != null) {                  return cacheValue;              } else {                //?????sql????                  cacheValue = GetProductListFromDB();                  CacheHelper.Add(cacheKey, cacheValue, cacheTime);              }          }          return cacheValue;      }  }

??????????????????????????????????????????key?????????1000???999???????????????????????????????

???????????????????????????????????????????????????????????????????????

???????

//???  public object GetProductListNew() {      int cacheTime = 30;      String cacheKey = "product_list";      //????      String cacheSign = cacheKey + "_sign";        String sign = CacheHelper.Get(cacheSign);      //?????      String cacheValue = CacheHelper.Get(cacheKey);      if (sign != null) {          return cacheValue; //????????      } else {          CacheHelper.Add(cacheSign, "1", cacheTime);          ThreadPool.QueueUserWorkItem((arg) -> {        //????? sql????              cacheValue = GetProductListFromDB();            //????????2??????            CacheHelper.Add(cacheKey, cacheValue, cacheTime * 2);          });          return cacheValue;      }  } 

?????

  • ??????????????????????????????????????key????
  • ?????????????????????1??????????30??????????60???????????key?????????????????????????????????????????????

?????????????????????????????????????????key???????????????????“????”??????

????

???????????????????????????????

??????????????????????????????????????LRU?RDB?AOF???????LRU???????Redis?RDB?AOF???????????????????

???????

https://blog.csdn.net/zeb_perfect/article/details/54135506
https://blog.csdn.net/fanrenxiang/article/details/80542580
https://baijiahao.baidu.com/s?id=1619572269435584821&wfr=spider&for=pc
https://blog.csdn.net/xlgen157387/article/details/79530877

???????????????

https://pan.baidu.com/mbox/homepage?short=btNBJoN

??????????

https://mp.weixin.qq.com/s/ksVC1049wZgPIOy2gGziNA

??????Java???????????Java?????

??Java.md