PHP session file exclusive lock cause blocking problem resolution _php instance

Source: Internet
Author: User
Tags php session

The default session processor for PHP is Session.save_handler = files (i.e. file). If the same client concurrently sends multiple requests (such as Ajax sending multiple requests at the same time), and the script executes longer, it can cause the session file to block and affect performance. Because PHP executes session_start () for each request, it obtains a file exclusive lock that will not be released until the request processing is complete. In this way, multiple requests can cause blocking at the same time. The solution is as follows:

(1) After modifying the session variable, immediately use Session_write_close () to save the session data and release the file lock.

Session_Start ();
 
$_session[' test ' = ' test ';
Session_write_close ();
 
Do something

(2) using the Session_set_save_handler () function is to implement custom session processing.

function open ($savePath, $sessionName)
{
  echo ' open is called ';
  return true;
}
 
function Close ()
{
  echo ' close is called ';
  return true;
}
 
function Read ($sessionId)
{
  echo ' read is called ';
  return ';
}
 
function Write ($sessionId, $data)
{
  echo ' write is called ';
  return true;
}
 
function Destroy ($sessionId)
{
  echo ' destroy is called ';
  return true;
}
 
Function gc ($lifetime)
{
  echo ' GC is called ';
  return true;
}
 
Session_set_save_handler ("Open", "close", "read", "write", "Destroy", "GC");
Register_shutdown_function (' Session_write_close ');
 
Session_Start ();
 
$_session[' foo '] = "bar";

Of course, after PHP 5.4.0, you can use it by implementing the Sessionhandlerinterface interface or inheriting the Sessionhandler class.

Class Mysessionhandler extends Sessionhandler {public
 
  function __construct ()
  {
  } public
 
  function open ($save _path, $session _id)
  {
  } public
 
  function close ()
  {
 
  } public
 
  function Create_sid (
 
  ) {} public function Read ($id)
  {
  } public
 
  function write ($id, $data)
  {
  } public
 
  function destroy ($ ID)
  {
  }
}
 
$handler = new Mysessionhandler ();
 
The 2nd parameter registers the function Session_write_close () as the Register_shutdown_function () function.
Session_set_save_handler ($handler, true);

You can implement and encapsulate the above code, using MySQL or other memory databases to manage session data. can also solve the use of cluster
, session data sharing is a problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.