使用PHP+Redis实现延迟任务,实现自动取消订单功能(2)
index.php
<?php require_once 'Redis2.class.php'; $redis = new \Redis2('127.0.0.1','6379','','15'); $order_sn = 'SN'.time().'T'.rand(10000000,99999999); $use_mysql = 1; //是否使用数据库,1使用,2不使用 if($use_mysql == 1){ /* * //数据表 * CREATE TABLE `order` ( * `ordersn` varchar(255) NOT NULL DEFAULT '', * `status` varchar(255) NOT NULL DEFAULT '', * `createtime` varchar(255) NOT NULL DEFAULT '', * `id` int(11) unsigned NOT NULL AUTO_INCREMENT, * PRIMARY KEY (`id`) * ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4; */ require_once 'db.class.php'; $mysql = new \mysql(); $mysql->connect(); $data = ['ordersn'=>$order_sn,'status'=>0,'createtime'=>date('Y-m-d H:i:s',time())]; $mysql->insert('order',$data); } $list = [$order_sn,$use_mysql]; $key = implode(':',$list); $redis->setex($key,3,'redis延迟任务'); //3秒后回调 $test_del = false; //测试删除缓存后是否会有过期回调。结果:没有回调 if($test_del == true){ //sleep(1); $redis->delete($order_sn); } echo $order_sn; /* * 测试其他key会不会有回调,结果:有回调 * $k = 'test'; * $redis2->set($k,'100'); * $redis2->expire($k,10); * */
psubscribe.php
<?php ini_set('default_socket_timeout', -1); //不超时 require_once 'Redis2.class.php'; $redis_db = '15'; $redis = new \Redis2('127.0.0.1','6379','',$redis_db); // 解决Redis客户端订阅时候超时情况 $redis->setOption(); //当key过期的时候就看到通知,订阅的key __keyevent@<db>__:expired 这个格式是固定的,db代表的是数据库的编号,由于订阅开启之后这个库的所有key过期时间都会被推送过来,所以最好单独使用一个数据库来进行隔离 $redis->psubscribe(array('__keyevent@'.$redis_db.'__:expired'), 'keyCallback'); // 回调函数,这里写处理逻辑 function keyCallback($redis, $pattern, $channel, $msg) { echo PHP_EOL; echo "Pattern: $pattern\n"; echo "Channel: $channel\n"; echo "Payload: $msg\n\n"; $list = explode(':',$msg); $order_sn = isset($list[0])?$list[0]:'0'; $use_mysql = isset($list[1])?$list[1]:'0'; if($use_mysql == 1){ require_once 'db.class.php'; $mysql = new \mysql(); $mysql->connect(); $where = "ordersn = '".$order_sn."'"; $mysql->select('order','',$where); $finds=$mysql->fetchAll(); print_r($finds); if(isset($finds[0]['status']) && $finds[0]['status']==0){ $data = array('status' => 3); $where = " id = ".$finds[0]['id']; $mysql->update('order',$data,$where); } } } //或者 /*$redis->psubscribe(array('__keyevent@'.$redis_db.'__:expired'), function ($redis, $pattern, $channel, $msg){ echo PHP_EOL; echo "Pattern: $pattern\n"; echo "Channel: $channel\n"; echo "Payload: $msg\n\n"; //................ });*/
Redis2.class.php
<?php class Redis2 { private $redis; public function __construct($host = '127.0.0.1', $port = '6379',$password = '',$db = '15') { $this->redis = new Redis(); $this->redis->connect($host, $port); //连接Redis $this->redis->auth($password); //密码验证 $this->redis->select($db); //选择数据库 } 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); } public function lRange($key,$start,$end) { return $this->redis->lRange($key,$start,$end); } public function lPush($key, $value1, $value2 = null, $valueN = null ){ return $this->redis->lPush($key, $value1, $value2 = null, $valueN = null ); } public function delete($key1, $key2 = null, $key3 = null) { return $this->redis->delete($key1, $key2 = null, $key3 = null); } }
内容版权声明:除非注明,否则皆为本站原创文章。