推荐(免费):redis
缓存穿透:key中对应的缓存数据不存在,导致去请求数据库,造成数据库的压力倍增的情况
缓存击穿:redis过期后的一瞬间,有大量用户请求同一个缓存数据,导致这些请求都去请求数据库,造成数据库压力倍增的情,针对一个key而言
缓存雪崩:缓存服务器宕机或者大量缓存集中某个时间段失效,导致请求全部去到数据库,造成数据库压力倍增的情况,这个是针对多个key而言
一、缓存穿透的解决方案
<?php class getPrizeList { /** * redis实例 * @var \Redis */ private $redis; /** * @var string */ private $redis_key = '|prize_list'; /** * 过期时间 * @var int */ private $expire = 30; /** * getPrizeList constructor. * @param $redis */ public function __construct($redis) { $this->redis = $redis; } /** * @return array|bool|string */ public function fetch() { $result = $this->redis->get($this->redis_key); if(!isset($result)) { //此处应该进行数据库查询... //如果查询结果不存在,给其默认空数组进行缓存 $result = []; $this->redis->set($this->redis_key, $result, $this->expire); } return $result; } }
二、缓存击穿解决办法
<?php class getPrizeList { /** * redis实例 * @var \Redis */ private $redis; /** * @var string */ private $redis_key = '|prize_list'; /** * @var string */ private $setnx_key = '|prize_list_setnx'; /** * 过期时间 * @var int */ private $expire = 30; /** * getPrizeList constructor. * @param $redis */ public function __construct($redis) { $this->redis = $redis; } /** * @return array|bool|string */ public function fetch() { $result = $this->redis->get($this->redis_key); if(!isset($result)) { if($this->redis->setnx($this->setnx_key, 1, $this->expire)) { //此处应该进行数据库查询... //$result = 数据库查询结果; $this->redis->set($this->redis_key, $result, $this->expire); $this->redis->del($this->setnx_key); //删除互斥锁 } else { //其他请求每等待10毫秒重新请求一次 sleep(10); self::fetch(); } } return $result; } }
三、缓存雪崩的解决办法
<?php class getPrizeList { /** * redis实例 * @var \Redis */ private $redis; /** * @var string */ private $redis_key = '|prize_list'; /** * 缓存标记key * @var string */ private $cash_key = '|prize_list_cash'; /** * 过期时间 * @var int */ private $expire = 30; /** * getPrizeList constructor. * @param $redis */ public function __construct($redis) { $this->redis = $redis; } /** * @return array|bool|string */ public function fetch() { $cash_result = $this->redis->get($this->cash_key); $result = $this->redis->get($this->redis_key); if(!$cash_result) { $this->redis->set($this->cash_key, 1, $this->expire); //此处应该进行数据库查询... //$result = 数据库查询结果, 并且设置的时间要比cash_key长,这里设置为2倍; $this->redis->set($this->redis_key, $result, $this->expire * 2); } return $result; } }
以上就是一起学习 Redis缓存穿透、缓存击穿、缓存雪崩的原理和解决办法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号