当前位置:网站首页>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 yes
find 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 .
边栏推荐
猜你喜欢
[depth first search] Ji suanke: Square
Implementation of AVL tree
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
How to improve website weight
应用使用Druid连接池经常性断链问题分析
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
The role of applet in industrial Internet
About static type, dynamic type, ID, instancetype
随机推荐
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
How does crmeb mall system help marketing?
wx小程序学习笔记day01
QLabel 跑马灯文字显示
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
Binary search tree
Multithreading Basics: basic concepts of threads and creation of threads
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
Interface test tool - postman
Penetration test information collection - basic enterprise information
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
Word如何显示修改痕迹
如何提高网站权重
Digital "new" operation and maintenance of energy industry
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
ACTF 2022圆满落幕,0ops战队二连冠!!
多线程基础:线程基本概念与线程的创建