当前位置:网站首页>php+redis实现超时取消订单功能
php+redis实现超时取消订单功能
2022-07-06 11:11:00 【Student Li】
业务场景:秒杀案例
秒杀业务逻辑:多个用户同时抢单,通过mysql行锁抢到的用户进入待支付页面(倒计时)。当用户没有支付订单超时时则取消该订单并归还库存。
应用
thinkphp+redis+workerman(可以自定义命令常驻)
1、thinkphp安装workerman。这里就过了,thinkphp手册去找。
2、安装好redis及扩展。用宝塔的直接搞就完了,过。
3、生产者:用户创建订单向redis插入一条订单数据。
$redis = new \Redis();
$redis->connect('127.0.0.1',6379);
//$redis->auth('密码');//redis有密码就加
/**
*seckill_time为列队名称
*time() + $seckill['pay_time']为到期时间戳
*$newSeckillOrder->id为订单id,可以json字符串存储
*/
$redis->zAdd('seckill_time', time() + $seckill['pay_time'], $newSeckillOrder->id);4、消费者:这里我们需要一个常驻内存一直来查询这个列队是否有消息,如果有就消费掉。
workerman
/**
* 每个进程启动
* @param $worker
*/
public function onWorkerStart($worker)
{
//防止时间出问题
date_default_timezone_set('PRC');
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
//redis密码
//$redis->auth('密码');//redis有密码就加此句
if($worker->id === 0){
//其他任务
}
//秒杀处理进程
if($worker->id === 1){
echo "启动秒杀任务!\n";
//workerman定时器,每秒执行一次。
Timer::add(1, function() use($redis) {
//通过zRangeByScore查询seckill_time列队中0到当前时间戳的数据。
$res = $redis->zRangeByScore('seckill_time', 0, time());
//存在数据
if (count($res) > 0) {
foreach ($res as $k=>$v){
//处理订单,$res[$k]为生产者存的订单id或数据。
//.....
//消费掉列队中的行数据
$redis->zRem('seckill_time', $res[$k]);
}
//这步不用说都懂的吧!毕竟常驻内存,我们要管理好内存哦~
unset($res);
}
});
}
}测试结果:

我这边是延迟20秒消费
如下:

消费掉后列队就空了哦~
使用该方案的原因:
可能有些人会问,workerman有定时器,可以直接延迟操作呀?为什么如此多此一举呢?
原因很简单,如果workerman突然出问题掉了,那定时器延迟的一些数据是不是就终止了呢?这样会导致丢掉的数据无法去关闭订单啦!所以采用redis延迟消息列队,将数据存入redis中,即使workerman出问题掉了,重新启动workerman也可以将没消费的数据消费掉!
关于redis重启丢数据问题需要修改配置如下。
找到appendonly no 改为 appendonly yes找到appendfsync 设置为 appendfsync everysec
解决redis重启了就丢数据的问题。
当然也可以使用其他的消息中间件来解决,比如mq,不过对于宝塔用户安装简介还是redis吧!
记住!不要定时器循环查数据库判断!不要定时器循环查数据库判断!不要定时器循环查数据库判断!
说三遍,数据库压力很大的~
当然该方案也适用拼团到期自动关闭该团等,可以自行脑补。
边栏推荐
- pytorch常见损失函数
- Use map function and split function to type multiple elements in one line
- AcWing 3537.树查找 完全二叉树
- Jushan database was among the first batch of financial information innovation solutions!
- If you have any problems, you can contact me. A rookie ~
- How to improve website weight
- Describe the process of key exchange
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- [Sun Yat sen University] information sharing of postgraduate entrance examination and re examination
- openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
猜你喜欢

多线程基础:线程基本概念与线程的创建

Summary of performance knowledge points

Self supervised heterogeneous graph neural network with CO comparative learning

Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services

C#/VB.NET 给PDF文档添加文本/图像水印

监控界的最强王者,没有之一!

Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers

wx小程序学习笔记day01

Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法

2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
随机推荐
[matlab] Simulink the input and output variables of the same module cannot have the same name
Analysis of frequent chain breaks in applications using Druid connection pools
手写一个的在线聊天系统(原理篇1)
How does crmeb mall system help marketing?
Word如何显示修改痕迹
用于远程医疗的无创、无袖带血压测量【翻译】
Bonecp uses data sources
AvL树的实现
抽象类与抽象方法
驼峰式与下划线命名规则(Camel case With hungarian notation)
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
Deep circulation network long-term blood pressure prediction [translation]
[depth first search] Ji suanke: a joke of replacement
Handwritten online chat system (principle part 1)
被疫情占据的上半年,你还好么?| 2022年中总结
QLabel 跑马灯文字显示
test about BinaryTree
Oracle advanced (IV) table connection explanation
A wearable arm device for night and sleeveless blood pressure measurement [translation]