在秒杀,抢购等并发场景下,可能会涌现超卖的征象,在PHP语言中并没有原生供应并发的解决方案,因而就须要借助其他体式格局来完成并发掌握。
列出罕见的解决方案有:
- 运用行列,分外起一个历程处置惩罚行列,并发要求都放到行列中,由分外历程串行处置惩罚,并发题目就不存在了,然则要分外历程支撑以及处置惩罚耽误严峻,本文不先不议论这类要领。
- 应用数据库事件特征,做原子更新,此要领须要依靠数据库的事件特征。
- 借助文件排他锁,在处置惩罚下单要求的时刻,用flock锁定一个文件,胜利拿到锁的才处置惩罚定单。
一、应用 Redis 事件特征
redis 事件是原子操纵,能够保证定单处置惩罚的过程当中数据没有被别的并发的历程修正。
示例代码:
<?php $http = new swoole_http_server("0.0.0.0", 9509); // 监听 9509 $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); // 模仿唯一用户ID $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // 衔接 redis $redis->watch('rest_count'); // 监测 rest_count 是不是被别的的历程变动 $rest_count = intval($redis->get("rest_count")); // 模仿唯一定单ID if ($rest_count > 0){ $value = "{$rest_count}-{$uniqid}"; // 示意当前定单,被当前用户抢到了 // do something ... 主假如模仿用户抢到单后可能要举行的一些麋集运算 $rand = rand(100, 1000000); $sum = 0; for ($i = 0; $i < $rand; $i++) {$sum += $i;} // redis 事件 $redis->multi(); $redis->lPush('uniqids', $value); $redis->decr('rest_count'); $replies = $redis->exec(); // 实行以上 redis 事件 // 假如 rest_count 的值被别的的并发历程变动了,以上事件将回滚 if (!$replies) { echo "定单 {$value} 回滚" . PHP_EOL; } } $redis->unwatch(); }); $http->start();
运用 ab 测试
$ ab -t 20 -c 10 http://192.168.1.104:9509/
二、应用文件排他锁(壅塞形式)
壅塞形式下,假如历程在猎取文件排他锁时,别的历程正在占用锁的话,此历程会挂起守候别的历程开释锁后,并本身猎取到锁后,再往下实行。
示例代码:
<?php $http = new swoole_http_server("0.0.0.0", 9510); $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $fp = fopen("lock.txt", "w+"); // 壅塞(守候)形式, 要获得独有锁定(写入的顺序) if (flock($fp,LOCK_EX)) { //锁定当前指针 // 胜利获得锁后,宁神处置惩罚定单 $rest_count = intval($redis->get("rest_count")); $value = "{$rest_count}-{$uniqid}"; if ($rest_count > 0) { // do something ... $rand = rand(100, 1000000); $sum = 0; for ($i = 0; $i < $rand; $i++) {$sum += $i;} $redis->lPush('uniqids', $value); $redis->decr('rest_count'); } // 定单处置惩罚完成后,再开释锁 flock($fp, LOCK_UN); } fclose($fp); }); $http->start();
运用 ab 测试
$ ab -t 20 -c 10 http://192.168.1.104:9510/
三、应用文件排他锁(非壅塞形式)
非壅塞形式下,假如历程在猎取文件排他锁时,别的历程正在占用锁的话,此历程会立时推断猎取锁失利,而且继承往下实行。\
示例代码:
<?php $http = new swoole_http_server("0.0.0.0", 9511); $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $fp = fopen("lock.txt", "w+"); // 非壅塞形式, 假如不愿望 flock() 在锁定时梗塞,则给 lock 加上 LOCK_NB if(flock($fp,LOCK_EX | LOCK_NB)) //锁定当前指针 { // 胜利获得锁后,宁神处置惩罚定单 $rest_count = intval($redis->get("rest_count")); $value = "{$rest_count}-{$uniqid}"; if($rest_count > 0){ // do something ... $rand = rand(100, 1000000); $sum=0; for ($i=0;$i<$rand;$i++){ $sum+=$i; } $redis->lPush('uniqids', $value); $redis->decr('rest_count'); } // 定单处置惩罚完成后,再开释锁 flock($fp,LOCK_UN); } else { // 假如猎取锁失利,立时进入这里实行 echo "{$uniqid} - 体系忙碌,请稍后再试".PHP_EOL; } fclose($fp); }); $http->start();
运用 ab 测试
$ ab -t 20 -c 10 http://192.168.1.104:9511/
末了给出三种处置惩罚体式格局的测试效果比较
redis 事件体式格局:
...... Concurrency Level: 10 Time taken for tests: 20.005 seconds Complete requests: 17537 Failed requests: 0 Total transferred: 2578380 bytes HTML transferred: 0 bytes Requests per second: 876.62 [#/sec] (mean) Time per request: 11.407 [ms] (mean) Time per request: 1.141 [ms] (mean, across all concurrent requests) Transfer rate: 125.86 [Kbytes/sec] received ......
文件排他锁(壅塞形式):
...... Concurrency Level: 10 Time taken for tests: 20.003 seconds Complete requests: 8205 Failed requests: 0 Total transferred: 1206282 bytes HTML transferred: 0 bytes Requests per second: 410.19 [#/sec] (mean) Time per request: 24.379 [ms] (mean) Time per request: 2.438 [ms] (mean, across all concurrent requests) Transfer rate: 58.89 [Kbytes/sec] received ......
文件排他锁(非壅塞形式):
...... Concurrency Level: 10 Time taken for tests: 20.002 seconds Complete requests: 8616 Failed requests: 0 Total transferred: 1266846 bytes HTML transferred: 0 bytes Requests per second: 430.77 [#/sec] (mean) Time per request: 23.214 [ms] (mean) Time per request: 2.321 [ms] (mean, across all concurrent requests) Transfer rate: 61.85 [Kbytes/sec] received ......
经测试效果对照,redis 事件体式格局优于文件排他锁体式格局,而文件排他锁体式格局中,非壅塞形式优于壅塞形式。
更多PHP相干技术文章,请接见PHP教程栏目举行进修!
以上就是PHP并发场景的几种解决方案的细致内容,更多请关注ki4网别的相干文章!