Example analysis of session lock in php to prevent blocking requests

黄舟
Release: 2023-03-14 18:56:01
Original
1204 people have browsed it

This article mainly introduces PHP's method of preventing blocking requests based on session locks. It analyzes the use of phpsession locks and related operating techniques to prevent blocking requests in the form of examples. Friends in need can refer to this article

The example describes how PHP prevents blocking requests based on session locks. Share it with everyone for your reference, the details are as follows:

Description:

Previous article Session blocking problems and solutions in PHP programming After a brief analysis of the solution to PHP session blocking, here is another solution.

Text:

Modern browsers limit the number of concurrent connections to a host to generally 4 or 6. This means that if your web page loads dozens of assert files (js, images, css) from the same host, queuing will occur due to the limit of concurrency. The same or worse, this problem also occurs in PHP scripts that use sessions.

Question:

php sessions are stored in files by default. When requesting a php file that needs to operate the session (session_start( )), this file will be locked by the first process that operates the session, causing other requests to be blocked. Other requests will be suspended at session_start() until the session file is unlocked.

Solution:

Since the locked session file will not be unlocked until the script execution ends or the session is closed normally, in order to prevent a large number of php requests ( Need to use $_SESSION data) is locked and can be closed immediately after writing the session, thus releasing the lock.

Close session:


session_write_close();
Copy after login

This technique is very useful, especially for a script that takes a long time to process. And this function only closes the writing session, reading is still possible.


// 
session_start();
//可以读写session
$_SESSION['latestRequestTime'] = time();
//关闭session
session_write_close();
//读取session
$twitterId = $_SESSION['twitterId'];
Copy after login

Translation Note:

After php5.4, session_set_save_handler is supported In order to pass SessionHandlerInterface, the second parameter is to specify session_write_close() as the callback method (the default is true), and register the function session_write_close() It is the register_shutdown_function() function.

Using Memcache or Redis to store sessions can solve the "locking" problem, but if not handled properly, the number of connections will increase (if there are time-consuming operations after the session operation, the connection will not For recycling, you can actively perform the session_write_close() operation)

after the session write operation is completed.

The above is the detailed content of Example analysis of session lock in php to prevent blocking requests. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!