定单超时、流动过时处理计划:php监听redis key生效触发还调事情
Redis 的 2.8.0 版本之后可用,键空间音讯(Redis Keyspace Notifications),合营 2.0.0 版本之后的 SUBSCRIBE
就能实现这个按时义务的操作了,按时的单元是秒。
1.咱们先定阅频道名为 redisChat
2.如今,咱们从新开启个 redis 客户端,而后正在同一个频道 redisChat 公布音讯,定阅者就能接纳到音讯。
接纳到的音讯以下:
3.Key过时事情的Redis设置装备摆设
这里需求设置装备摆设 notify-keyspace-events
的参数为 “Ex”。x 代表了过时事情。notify-keyspace-events “Ex”
保留设置装备摆设后,重启Redis效劳,使设置装备摆设失效。
PHP redis完成定阅键空间告诉
redis实例化类:
redis.class.php
//遇到种别反复的报错,一切叫Redis2 class Redis2 { 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); } }
过时事情的定阅:
psubscribe.php
require_once './Redis.class.php'; $redis = new \Redis2(); // 处理Redis客户端定阅时分超时状况 $redis->setOption(); $redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback'); // 回调函数,这里写解决逻辑 function keyCallback($redis, $pattern, $chan, $msg) { echo "Pattern: $pattern\n"; echo "Channel: $chan\n"; echo "Payl oad: $msg\n\n"; //keyCallback为定阅事情后的回调函数,这里写营业解决逻辑, //比方后面提到的商品没有领取主动撤单,这里就能够依据定单id,来完成主动撤单 }
设置过时事情:
index.php
require_once './Redis.class.php'; $redis = new \Redis2(); $order_id = 123; $redis->setex('order_id',10,$order_id);
先用饬令行模式执行 psubscribe.php
正在阅读器拜访 index.php
成果以下:
更多相干php常识,请拜访php教程!
以上就是php监听redis key生效触发还调事情的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学 Redis
抱歉,评论功能暂时关闭!