場景
在一些電商項目中,對一些過期的訂單以及優(yōu)惠券等業(yè)務需要做后續(xù)處理
方案
- 定時掃描數據庫,通過掃描到符合條件的數據做特定處理(成本高)
- 通過redis的監(jiān)聽訂閱做處理(成本低)
實踐準備
- 開啟事件通知,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 ";
echo "Channel: $chan ";
echo "Payl
oad: $msg ";
//keyCallbackFunc為訂閱事件后的回調函數,寫業(yè)務處理邏輯部分,后續(xù)業(yè)務處理
}
//來一個測試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模式執(zhí)行訂閱,然后進行setkey測試
監(jiān)聽到過期的key,那么可以字符串分離拿到對應的order_id做后續(xù)處理
?
本文摘自 :https://blog.51cto.com/u