Ashes 2020-01-08
今天帮忙解决问题时遇到的redis一个功能点
具体行为就是:某个键值到了过期时间自动触发回调函数,然后执行一些操作,比如订单15分钟未支付就自动取消。
<?php require_once 'Redis2.class.php'; // require_once 'db.class.php'; $redis2 = new \Redis2(); // $mysql = new \mysql(); // 注释是你的业务逻辑 // $mysql->connect(); // $order_sn='SN'.time().'T'.rand(10000000,99999999); // $data = ['ordersn'=>$order_sn,'status'=>0,'createtime'=>date('Y-m-d H:i:s',time())]; // $mysql->insert('order',$data); $res = $redis2->setex('kkk',10, "It is no pay"); var_dump($res);exit;
<?php require_once 'Redis2.class.php'; ini_set('default_socket_timeout', -1); //不超时 $redis = new Redis2(); // 解决Redis客户端订阅时候超时情况 $redis->setOption(); $redis->psubscribe(array('__:expired'), 'keyCallback'); // 回调函数,这里写处理逻辑 function keyCallback($redis, $pattern, $chan, $msg) { echo date('Y-M-D H:i:s'); // 这两行留作查看是否执行回调函数 // file_put_contents("d:/lizheng.log", "\n\n".print_r(123, true),8); // 下面写你的业务逻辑 //修改订单状态 // $order=model('order')->where(['order_no'=>$msg])->find(); // $order->status=-1; // $order->save(); // //库存还原 // $products=json_decode($order->snap_items,true); // foreach($products as $v){ // model('product')->where(['id'=>$v['id']])->setInc('stock',$v['count']); // } }
<?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('123456'); } 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); } }
<?php class mysql { private $mysqli; private $result; /** * 数据库连接 * @param $config 配置数组 */ public function connect() { $config=array( 'host'=>'127.0.0.1', 'username'=>'root', 'password'=>'123456qwerty', 'database'=>'marhal', 'port'=>3306, ); $host = $config['host']; //主机地址 $username = $config['username'];//用户名 $password = $config['password'];//密码 $database = $config['database'];//数据库 $port = $config['port']; //端口号 $this->mysqli = new mysqli($host, $username, $password, $database, $port); } /** * 数据查询 * @param $table 数据表 * @param null $field 字段 * @param null $where 条件 * @return mixed 查询结果数目 */ public function select($table, $field = null, $where = null) { $sql = "SELECT * FROM `{$table}`"; //echo $sql;exit; if (!empty($field)) { $field = '`' . implode('`,`', $field) . '`'; $sql = str_replace('*', $field, $sql); } if (!empty($where)) { $sql = $sql . ' WHERE ' . $where; } $this->result = $this->mysqli->query($sql); return $this->result; } /** * @return mixed 获取全部结果 */ public function fetchAll() { return $this->result->fetch_all(MYSQLI_ASSOC); } /** * 插入数据 * @param $table 数据表 * @param $data 数据数组 * @return mixed 插入ID */ public function insert($table, $data) { foreach ($data as $key => $value) { $data[$key] = $this->mysqli->real_escape_string($value); } $keys = '`' . implode('`,`', array_keys($data)) . '`'; $values = '\'' . implode("','", array_values($data)) . '\''; $sql = "INSERT INTO `{$table}`( {$keys} )VALUES( {$values} )"; $this->mysqli->query($sql); return $this->mysqli->insert_id; } /** * 更新数据 * @param $table 数据表 * @param $data 数据数组 * @param $where 过滤条件 * @return mixed 受影响记录 */ public function update($table, $data, $where) { foreach ($data as $key => $value) { $data[$key] = $this->mysqli->real_escape_string($value); } $sets = array(); foreach ($data as $key => $value) { $kstr = '`' . $key . '`'; $vstr = '\'' . $value . '\''; array_push($sets, $kstr . '=' . $vstr); } $kav = implode(',', $sets); $sql = "UPDATE `{$table}` SET {$kav} WHERE {$where}"; $this->mysqli->query($sql); return $this->mysqli->affected_rows; } /** * 删除数据 * @param $table 数据表 * @param $where 过滤条件 * @return mixed 受影响记录 */ public function delete($table, $where) { $sql = "DELETE FROM `{$table}` WHERE {$where}"; $this->mysqli->query($sql); return $this->mysqli->affected_rows; } }