php监听redis中的key过期回调处理
场景
在一些电商项目中,对一些过期的订单以及优惠券等业务需要做后续处理
方案
- 定时扫描数据库,通过扫描到符合条件的数据做特定处理(成本高)
- 通过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做后续处理