当前位置:网站首页>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吧!
记住!不要定时器循环查数据库判断!不要定时器循环查数据库判断!不要定时器循环查数据库判断!
说三遍,数据库压力很大的~
当然该方案也适用拼团到期自动关闭该团等,可以自行脑补。
边栏推荐
- Xu Xiang's wife Ying Ying responded to the "stock review": she wrote it!
- Stm32+esp8266+mqtt protocol connects onenet IOT platform
- wx小程序学习笔记day01
- R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
- On AAE
- Afnetworking framework_ Upload file or image server
- How word displays modification traces
- R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
- Penetration test information collection - WAF identification
- 视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
猜你喜欢
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
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: find numbers
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
Wx applet learning notes day01
Execution process of MySQL query request - underlying principle
Docker installation redis
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
Some understandings of tree LSTM and DGL code implementation
随机推荐
RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
QLabel 跑马灯文字显示
Collection of penetration test information -- use with nmap and other tools
应用使用Druid连接池经常性断链问题分析
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
Digital "new" operation and maintenance of energy industry
Specify flume introduction, installation and configuration
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
Self supervised heterogeneous graph neural network with CO comparative learning
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
2022.2.12
Some understandings of tree LSTM and DGL code implementation
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
人体骨骼点检测:自顶向下(部分理论)
被疫情占据的上半年,你还好么?| 2022年中总结
青龙面板最近的库
Penetration test information collection - basic enterprise information
Atcoder a mountaineer
渲大师携手向日葵,远控赋能云渲染及GPU算力服务