<?php
require_once 'Redis.class.php';
//require_once '../libraries/Index.php';
$redis = new \Redis2();
$order_id = 1;
$redis->setex('1',10,$order_id);
$order_id2 = 2;
$redis->setex('2',10,$order_id2);
$order_id3 = 3;
$redis->setex('3',10,$order_id3);
<?php
class Redis2
{
private $redis;
public function __construct($host = '127.0.0.1', $port = 6379)
{
$this->redis = new Redis();
$this->redis->connect($host, $port);
$this->redis->auth("mxh@#a12.");
}
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);
}
}
<?php
ini_set('default_socket_timeout', -1);
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 "Payload: $msg\n\n";
echo "开始执行其他代码";
$a["Pattern"] = $pattern;
$a["Channel"] = $chan;
$a["Payload"] = $msg;
$a["开始"] = date("y-m-d h:i:s");
$fp = fopen('zdong.txt', 'a+b');
fwrite($fp, var_export($a, true));
fclose($fp);
//keyCallback为订阅事件后的回调函数,这里写业务处理逻辑,
//比如前面提到的商品不支付自动撤单,这里就可以根据订单id,来实现自动撤单
}
https://www.cnblogs.com/idjl/p/9610538.html