當(dāng)前位置:首頁 > IT技術(shù) > Web編程 > 正文

php監(jiān)聽redis中的key過期回調(diào)處理
2021-10-18 17:44:08


場(chǎng)景

在一些電商項(xiàng)目中,對(duì)一些過期的訂單以及優(yōu)惠券等業(yè)務(wù)需要做后續(xù)處理

方案

  1. 定時(shí)掃描數(shù)據(jù)庫,通過掃描到符合條件的數(shù)據(jù)做特定處理(成本高)
  2. 通過redis的監(jiān)聽訂閱做處理(成本低)

實(shí)踐準(zhǔn)備

  1. 開啟事件通知,redis 版本大于 2.8,修改配置文件
  2. 需要訂閱主題(__keyevent@dbindex__:expired),dbindex指的是數(shù)據(jù)庫索引,redis有16個(gè)小數(shù)據(jù)庫,一般存儲(chǔ)到的是0號(hào)數(shù)據(jù)庫,所以訂閱的主題可以是__keyevent@0__:expired

?修改redis配置文件



#  notify-keyspace-events Ex  //復(fù)制本行
#
# 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服務(wù)

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客戶端訂閱時(shí)候超時(shí)情況,永久支持
$redis->setOption();
$redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallbackFunc');
// 回調(diào)函數(shù)
function keyCallbackFunc($redis, $pattern, $chan, $msg)
{
echo "Pattern: $pattern ";
echo "Channel: $chan ";
echo "Payl
oad: $msg ";
//keyCallbackFunc為訂閱事件后的回調(diào)函數(shù),寫業(yè)務(wù)處理邏輯部分,后續(xù)業(yè)務(wù)處理
}



//來一個(gè)測(cè)試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í)行訂閱,然后進(jìn)行setkey測(cè)試

監(jiān)聽到過期的key,那么可以字符串分離拿到對(duì)應(yīng)的order_id做后續(xù)處理

?


本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >