当前位置:网站首页>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吧!
记住!不要定时器循环查数据库判断!不要定时器循环查数据库判断!不要定时器循环查数据库判断!
说三遍,数据库压力很大的~
当然该方案也适用拼团到期自动关闭该团等,可以自行脑补。
边栏推荐
- Stm32+esp8266+mqtt protocol connects onenet IOT platform
- Atcoder a mountaineer
- Based on butterfly species recognition
- Penetration test information collection - App information
- 2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
- Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
- Breadth first traversal of graph
- [paper notes] transunet: transformers make strongencoders for medical image segmentation
- 关于静态类型、动态类型、id、instancetype
- Afnetworking framework_ Upload file or image server
猜你喜欢

Helm deploy etcd cluster
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic

2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目

Breadth first traversal of graph

Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars

Human bone point detection: top-down (part of the theory)

巨杉数据库首批入选金融信创解决方案!

Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast

手写一个的在线聊天系统(原理篇1)

Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
随机推荐
Some understandings of tree LSTM and DGL code implementation
Analysis of frequent chain breaks in applications using Druid connection pools
Multithreading Basics: basic concepts of threads and creation of threads
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
被疫情占据的上半年,你还好么?| 2022年中总结
Specify flume introduction, installation and configuration
根据PPG估算血压利用频谱谱-时间深度神经网络【翻】
上海部分招工市场对新冠阳性康复者拒绝招录
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
ROS custom message publishing subscription example
C#/VB.NET 给PDF文档添加文本/图像水印
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
Oracle advanced (IV) table connection explanation
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
How word displays modification traces
Solve DoS attack production cases
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
[depth first search] Ji suanke: Square