当前位置:   article > 正文

redis过期回调处理订单超时_redis缓存过期触发回调函数处理订单超时关闭

redis缓存过期触发回调函数处理订单超时关闭

目录

1、redis.config配置--重启redis

2、四个文件

3.执行监听文件

index.php

psubscribe.php

Redis2.class.php

db.class.php

1、redis.config配置--重启redis

  1. notify-keyspace-events “Ex”。
  2. #x 代表了过期事件。

2、四个文件

index.php  创建订单,发布消息,10s后查询订单状态并更新订单

psubscribe.php 超时回调函数

Redis2.class.php redis类封装

db.class.php mysql封装

3.执行监听文件

cd /var/www/html/redis

  退出监听 ctrl+c   暂停监听: ctrl+z   

  即时监听:php psubscribe.php

  后台监听: php psubscribe.php &

index.php

  1. require_once 'Redis2.class.php';
  2. require_once 'db.class.php';
  3. $redis2 = new \Redis2();
  4. $mysql = new \mysql();
  5. $mysql->connect();
  6. $order_sn='SN'.time().'T'.rand(10000000,99999999);
  7. $data = ['ordersn'=>$order_sn,'status'=>0,'createtime'=>date('Y-m-d H:i:s',time())];
  8. $mysql->insert('order',$data);
  9. $redis2->setex('order_sn',10,$order_sn);

psubscribe.php

  1. <?php
  2. use app\lib\redis\Redis;
  3. ini_set('default_socket_timeout', -1); //不超时
  4. $redis = new Redis();
  5. // 解决Redis客户端订阅时候超时情况
  6. $redis->setOption();
  7. $redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback');
  8. // 回调函数,这里写处理逻辑
  9. function keyCallback($redis, $pattern, $chan, $msg)
  10. {
  11. //修改订单状态
  12. $order=model('order')->where(['order_no'=>$msg])->find();
  13. $order->status=-1;
  14. $order->save();
  15. //库存还原
  16. $products=json_decode($order->snap_items,true);
  17. foreach($products as $v){
  18. model('product')->where(['id'=>$v['id']])->setInc('stock',$v['count']);
  19. }
  20. }

Redis2.class.php

  1. class Redis2
  2. {
  3. private $redis;
  4. public function __construct($host = '127.0.0.1', $port = 6379)
  5. {
  6. $this->redis = new Redis();
  7. $this->redis->connect($host, $port);
  8. $this->redis->auth('123456');
  9. }
  10. public function setex($key, $time, $val)
  11. {
  12. return $this->redis->setex($key, $time, $val);
  13. }
  14. public function set($key, $val)
  15. {
  16. return $this->redis->set($key, $val);
  17. }
  18. public function get($key)
  19. {
  20. return $this->redis->get($key);
  21. }
  22. public function expire($key = null, $time = 0)
  23. {
  24. return $this->redis->expire($key, $time);
  25. }
  26. public function psubscribe($patterns = array(), $callback)
  27. {
  28. $this->redis->psubscribe($patterns, $callback);
  29. }
  30. public function setOption()
  31. {
  32. $this->redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
  33. }
  34. public function lRange($key,$start,$end)
  35. {
  36. return $this->redis->lRange($key,$start,$end);
  37. }
  38. public function lPush($key, $value1, $value2 = null, $valueN = null ){
  39. return $this->redis->lPush($key, $value1, $value2 = null, $valueN = null );
  40. }
  41. public function delete($key1, $key2 = null, $key3 = null)
  42. {
  43. return $this->redis->delete($key1, $key2 = null, $key3 = null);
  44. }
  45. }

db.class.php

  1. class mysql
  2. {
  3. private $mysqli;
  4. private $result;
  5. /**
  6. * 数据库连接
  7. * @param $config 配置数组
  8. */
  9. public function connect()
  10. {
  11. $config=array(
  12. 'host'=>'127.0.0.1',
  13. 'username'=>'root',
  14. 'password'=>'123456qwerty',
  15. 'database'=>'marhal',
  16. 'port'=>3306,
  17. );
  18. $host = $config['host']; //主机地址
  19. $username = $config['username'];//用户名
  20. $password = $config['password'];//密码
  21. $database = $config['database'];//数据库
  22. $port = $config['port']; //端口号
  23. $this->mysqli = new mysqli($host, $username, $password, $database, $port);
  24. }
  25. /**
  26. * 数据查询
  27. * @param $table 数据表
  28. * @param null $field 字段
  29. * @param null $where 条件
  30. * @return mixed 查询结果数目
  31. */
  32. public function select($table, $field = null, $where = null)
  33. {
  34. $sql = "SELECT * FROM `{$table}`";
  35. //echo $sql;exit;
  36. if (!empty($field)) {
  37. $field = '`' . implode('`,`', $field) . '`';
  38. $sql = str_replace('*', $field, $sql);
  39. }
  40. if (!empty($where)) {
  41. $sql = $sql . ' WHERE ' . $where;
  42. }
  43. $this->result = $this->mysqli->query($sql);
  44. return $this->result;
  45. }
  46. /**
  47. * @return mixed 获取全部结果
  48. */
  49. public function fetchAll()
  50. {
  51. return $this->result->fetch_all(MYSQLI_ASSOC);
  52. }
  53. /**
  54. * 插入数据
  55. * @param $table 数据表
  56. * @param $data 数据数组
  57. * @return mixed 插入ID
  58. */
  59. public function insert($table, $data)
  60. {
  61. foreach ($data as $key => $value) {
  62. $data[$key] = $this->mysqli->real_escape_string($value);
  63. }
  64. $keys = '`' . implode('`,`', array_keys($data)) . '`';
  65. $values = '\'' . implode("','", array_values($data)) . '\'';
  66. $sql = "INSERT INTO `{$table}`( {$keys} )VALUES( {$values} )";
  67. $this->mysqli->query($sql);
  68. return $this->mysqli->insert_id;
  69. }
  70. /**
  71. * 更新数据
  72. * @param $table 数据表
  73. * @param $data 数据数组
  74. * @param $where 过滤条件
  75. * @return mixed 受影响记录
  76. */
  77. public function update($table, $data, $where)
  78. {
  79. foreach ($data as $key => $value) {
  80. $data[$key] = $this->mysqli->real_escape_string($value);
  81. }
  82. $sets = array();
  83. foreach ($data as $key => $value) {
  84. $kstr = '`' . $key . '`';
  85. $vstr = '\'' . $value . '\'';
  86. array_push($sets, $kstr . '=' . $vstr);
  87. }
  88. $kav = implode(',', $sets);
  89. $sql = "UPDATE `{$table}` SET {$kav} WHERE {$where}";
  90. $this->mysqli->query($sql);
  91. return $this->mysqli->affected_rows;
  92. }
  93. /**
  94. * 删除数据
  95. * @param $table 数据表
  96. * @param $where 过滤条件
  97. * @return mixed 受影响记录
  98. */
  99. public function delete($table, $where)
  100. {
  101. $sql = "DELETE FROM `{$table}` WHERE {$where}";
  102. $this->mysqli->query($sql);
  103. return $this->mysqli->affected_rows;
  104. }
  105. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/685113
推荐阅读
相关标签
  

闽ICP备14008679号