当前位置:网站首页>Php+redis realizes the function of canceling orders over time
Php+redis realizes the function of canceling orders over time
2022-07-06 19:04:00 【Student Li】
Business scenario : Second kill case
Second kill business logic : Multiple users rush orders at the same time , adopt mysql The user who gets the row lock enters the page to be paid ( count down ). When the user fails to pay the order and times out, the order will be canceled and the inventory will be returned .
application
thinkphp+redis+workerman( You can customize the command resident )
1、thinkphp install workerman. It's just over here ,thinkphp Find the manual .
2、 Install well redis And expansion . Use the pagoda directly to finish , too .
3、 producer : Users create orders to redis Insert an order data .
$redis = new \Redis();
$redis->connect('127.0.0.1',6379);
//$redis->auth(' password ');//redis If you have a password, add
/**
*seckill_time Is the queue name
*time() + $seckill['pay_time'] Is the expiration timestamp
*$newSeckillOrder->id For the order id, Sure json String store
*/
$redis->zAdd('seckill_time', time() + $seckill['pay_time'], $newSeckillOrder->id);4、 consumer : Here we need a resident memory to always query whether there is any message in the queue , If you have it, consume it .
workerman
/**
* Every process starts
* @param $worker
*/
public function onWorkerStart($worker)
{
// Prevent time problems
date_default_timezone_set('PRC');
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
//redis password
//$redis->auth(' password ');//redis Add this sentence if you have a password
if($worker->id === 0){
// Other tasks
}
// Second kill processing process
if($worker->id === 1){
echo " Start the second kill task !\n";
//workerman Timer , Once a second .
Timer::add(1, function() use($redis) {
// adopt zRangeByScore Inquire about seckill_time In line 0 Data to the current timestamp .
$res = $redis->zRangeByScore('seckill_time', 0, time());
// There is data
if (count($res) > 0) {
foreach ($res as $k=>$v){
// Process orders ,$res[$k] Orders deposited for producers id Or data .
//.....
// Consume row data in the queue
$redis->zRem('seckill_time', $res[$k]);
}
// Needless to say, I understand this step ! After all, resident memory , We should manage the memory well ~
unset($res);
}
});
}
}test result :

My side is delay 20 Second consumption
as follows :

After consumption, the queue will be empty ~
The reason for using this scheme :
Maybe some people will ask ,workerman With timer , You can delay the operation directly ? Why is it so unnecessary ?
The reason is simple , If workerman Suddenly something went wrong , Does some data delayed by the timer terminate ? This will cause the lost data to be unable to close the order ! So using redis Delay message queuing , Store data redis in , Even if workerman Something went wrong , Restart workerman You can also consume data that is not consumed !
About redis The problem of data loss during restart requires modifying the configuration as follows .
find appendonly no Change it to appendonly yesfind appendfsync Set to appendfsync everysec
solve redis The problem of losing data after restarting .
Of course, other message oriented middleware can also be used to solve , such as mq, However, the introduction of pagoda user installation is still redis Well !
remember ! Don't use the timer to check the database repeatedly ! Don't use the timer to check the database repeatedly ! Don't use the timer to check the database repeatedly !
Say it three times , The database is under great pressure ~
Of course, this scheme is also applicable to the automatic closing of the group when the group is due , You can make up your brain by yourself .
边栏推荐
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- 线代笔记....
- MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
- 关于静态类型、动态类型、id、instancetype
- 三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
- 如何提高网站权重
- test about BinaryTree
- ROS自定义消息发布订阅示例
- Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
- 爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
猜你喜欢

二叉搜索树

Hongke shares | plate by plate ar application in Beijing Winter Olympics

朗坤智慧冲刺科创板:年营收4亿 拟募资7亿

一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】

Based on butterfly species recognition

Solve DoS attack production cases

Breadth first traversal of graph

None of the strongest kings in the monitoring industry!
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理

同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
随机推荐
Describe the process of key exchange
test about BinaryTree
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
node の SQLite
Medical image segmentation
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
Use map function and split function to type multiple elements in one line
QPushButton绑定快捷键的注意事项
美庐生物IPO被终止:年营收3.85亿 陈林为实控人
ACTF 2022圆满落幕,0ops战队二连冠!!
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
Understanding disentangling in β- VAE paper reading notes
Specify flume introduction, installation and configuration
Qlabel marquee text display
安装及管理程序
Analysis of frequent chain breaks in applications using Druid connection pools