Home  >  Article  >  Backend Development  >  How to implement php queuing

How to implement php queuing

藏色散人
藏色散人Original
2021-06-08 09:12:462697browse

php method to implement queuing: first customize the Session access method; then store the SessionID in a folder as a file name; finally perform database-related operations and delete the Session file.

How to implement php queuing

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to implement php queuing?

Example of PHP using user queuing mechanism to solve large website traffic

If some websites provide some query services, these data may be accessed by multiple users at the same time. If not Take action and the database may crash due to heavy access. The following code mainly solves the problem of allowing only the top ten in the queue to enter related operations at the same time, while other users can only wait for the previous people to complete their operations before they can access.

<?php
function mysession_open($save_path,$sesssion_name){
global $sesssion_save_path;   //定义session的存储位置
$sesssion_save_path = "你要存储的session地址";
return true;
}
function mysession_close(){
return true;
}
function mysession_read($key) {
global $sesssion_save_path;
global $online;  //全局变量,用于存取队列前面的人数
$online = 0;  //初始化
$dir = opendir($sesssion_save_path); //读取文件夹,获取文件夹数
while ($file = readdir($dir)) {
if($file == ".." || $file ==".") {
continue;
}
if(file_exists($sesssion_save_path.$key){  //如果当前session文件夹已经创建
$mytime = fileatime($sesssion_save_path.$key);
if(fileatime($sesssion_save_path.$file) < $mytime){ //计算队列前面人数的数目
$online++;
}
}
else{ //如果当前session文件没有创建
$online++;//计算队列中的人数
}
}
if(!file_exists($sesssion_save_path.$key)){ //如果文件不存在将当前数加1
$online++;
}
return true;
}
function mysession_write($key,$data){
global $sesssion_save_path;
$fileame = $sesssion_save_path.$key;
if(!file_exists($fileame)) {
$fp = fopen($fileame, "w");//文件不存在则创建
fputs($fp,"");
fclose($fp);
}
}
function mysession_destory($key) {
global $sesssion_save_path;
$fileame = $sesssion_save_path.$key;
if(file_exists($fileame)){
unlink($fileame);
}
return true;
}
function mysession_gc($expiry_time) {  //删除所有过期文件
global $sesssion_save_path;
$dir = opendir($sesssion_save_path);
while ($file = readdir($dir)) {
# code...
if($file == ".." || $file == "."){
continue;
}
if(fileatime($sesssion_save_path.$file) <= time() - $expiry_time){
unlink($sesssion_save_path.$file);
}
}
return true;
}
//设置用户自定义Session存储
session_set_save_handler(&#39;mysession_open&#39;, &#39;mysession_close&#39;, &#39;mysession_read&#39;, &#39;mysession_write&#39;, &#39;mysession_destory&#39;, &#39;mysession_gc&#39;);
//判断队列前面的人数并进行查询
session_start();
global $online;
echo $online;
if($online <= 10){   //如果队列在前十名,则进行操作
//进行数据库查询,
//......
//......
//......
//......
session_destroy();
}
?>

The above code saves the SessionID in a folder in the form of a file name by customizing the Session access method. Every time a page is opened, the current user's location is determined by the number of files in the current folder. If the location is in the top ten, perform database-related operations and delete the Session file. This can ensure the stability of the database under large-scale access.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement php queuing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn