订单是咱们在平常开发中常常会遇到的一个功能,最近在作一个订单过时与超时的开发。订单过时与超时就不用我解释了吧,其实二者都是同一个问题来着,就是订单未支付的处理,咱们要作的是对这些未支付的订单到了必定时间就自动取消,好了,你第一反应那确定就是作一个定时任务了!是的,就是定时任务,可是哪一个才会是最佳方案呢,下面来看看:php
1、前端到时间请求取消html
你确定不会想着在前端来作定时请求取消该订单的,由于若是客户禁用了该应用或者没联网,那到了必定时间时,还一直都是未处理状态的。因此前端通常都是手动去触发取消订单的。前端
2、服务端定时查询有没有须要取消的订单,而后批量处理。node
这种方法咱们用得最多的,不过存在准确度的问题,还有须要确认定时任务的周期,可是咱们能够结合redis来实现,咱们能够在存redis时候顺便存在mysql这样的数据库作长久存储而后用服务端定时查询,这里我用到了redis的订阅功能。mysql
首先先修改一下配置redis
而后再重启redissql
这里建立4个文件shell
index.php 建立订单,发布消息,10s后查询订单状态并更新订单数据库
<?php
require_once 'Redis2.class.php';
require_once 'db.class.php';
$redis2 = new \Redis2();
$list = $redis2->lRange('order',0,9999999);
$mysql = new \mysql();
$mysql->connect();
$data = ['ordersn'=>'SN'.time().'T'.rand(10000000,99999999),'status'=>0,'createtime'=>date('Y-m-d H:i:s',time())];
$mysql->insert('order',$data);
$order_sn = $data['ordersn'];
$redis2->setex($order_sn,10,$order_sn);
psubscribe.php,发布redis订阅的一些逻辑处理数组
这里我把redis的一些操做和mysql的一些处理作了封装处理,这个大家能够用到大家本身的项目当中的
Redis2.class.php
<?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);
}
}
db.class.php,对sql处理的一些封装
<?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;
}
}
对每一次订单的访问咱们作了服务器监放任务,以下:
设置本地域名并访问http://www.startphp.cn/index.php
此时,每访问一次index.php,就会建立一个订单,10s钟后,若是该订单依旧处于未支付状态,就设置订单失效。
可是php的cli模式在服务器运行后,老是会掉线,处理这个问题的方法,咱们不妨写一个脚本
1.编写shell脚本,定时检查进程是否存在,不存在的话就开启服务,而且将运行状况写入日志
脚本文件以下:
#!/bin/sh
PIDS=`pidof php`
if [ "$PIDS" != "" ]; then
echo "在运行"
echo -e $(date +%Y"."%m"."%d" "%k":"%M":"%S)" running....." >> /mytask/task.run.log
else
echo "不在运行,开始启动"
echo -e $(date +%Y"."%m"."%d" "%k":"%M":"%S)" start php start....." >> /mytask/task.start.log
cd /var/www/html/redis
php psubscribe.php &
echo -e $(date +%Y"."%m"."%d" "%k":"%M":"%S)" start php success....." >> /mytask/task.start.log
fi
在crontab任务里建立任务,这里设定的是每5s检查一次,crontab -e
效果你能够查看task.run.log
结果以下:
有须要学习交流的友人请加入交流群的我们一块儿,群内都是1-7年的开发者,但愿能够一块儿交流,探讨PHP,swoole这块的技术 或者有其余问题 也能够问,获取swoole或者php进阶相关资料私聊管理便可
别忘了点赞哦,按期分享干货