場景
在一些電商項目中,對一些過期的訂單以及優惠券等業務需要做后續處理
方案
- 定時掃描數據庫,通過掃描到符合條件的數據做特定處理(成本高)
- 通過redis的監聽訂閱做處理(成本低)
實踐准備
- 開啟事件通知,redis 版本大於 2.8,修改配置文件
- 需要訂閱主題(__keyevent@dbindex__:expired),dbindex指的是數據庫索引,redis有16個小數據庫,一般存儲到的是0號數據庫,所以訂閱的主題可以是__keyevent@0__:expired
修改redis配置文件
# notify-keyspace-events Ex //復制本行 # # By default all notifications are disabled because most users don't need # this feature and the feature has some overhead. Note that if you don't # specify at least one of K or E, no events will be delivered. #notify-keyspace-events “” //注釋此行 notify-keyspace-events Ex //新寫一行 ############################### GOPHER SERVER #################################
修改完重啟redis服務
php代碼
//redis類
class RedisClient { private $redis; public function __construct($host = '127.0.0.1', $port = 6379) { $this->redis = new Redis(); $this->redis->connect($host, $port); } public function setex($key, $time, $val) { return $this->redis->setex($key, $time, $val); } public function set($key, $val) { return $this->redis->set($key, $val); } public function get($key) { return $this->redis->get($key); } public function expire($key = null, $time = 0) { return $this->redis->expire($key, $time); } public function psubscribe($patterns = array(), $callback) { $this->redis->psubscribe($patterns, $callback); } public function setOption() { $this->redis->setOption(\Redis::OPT_READ_TIMEOUT, -1); } }
//過期事件訂閱 require_once './RedisClient.class.php'; $redis = new \RedisClient(); // 解決Redis客戶端訂閱時候超時情況,永久支持 $redis->setOption(); $redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallbackFunc'); // 回調函數 function keyCallbackFunc($redis, $pattern, $chan, $msg) { echo "Pattern: $pattern\n"; echo "Channel: $chan\n"; echo "Payl oad: $msg\n\n"; //keyCallbackFunc為訂閱事件后的回調函數,寫業務處理邏輯部分,后續業務處理 }
//來一個測試key require_once './RedisClient.class.php'; $redis = new \RedisClient();
$cache_key = "order_id:" $order_id = $cache_key.123456; $redis->setex('123456’,5,$order_id);
先phpcli模式執行訂閱,然后進行setkey測試
監聽到過期的key,那么可以字符串分離拿到對應的order_id做后續處理
