PHP session file exclusive lock causes blocking problem solution, _ PHP Tutorial

Source: Internet
Author: User
Tags php session
Php session file exclusive lock causes blocking problem solution ,. The PHP session file exclusive lock causes blocking. The Default session processor of PHP is session. save_handlerfiles (that is, the file ). If the same client concurrently sends multiple PHP session files with an exclusive lock, which causes blocking,

The default session processor of PHP is session. save_handler = files ). If the same client sends multiple requests concurrently (for example, ajax sends multiple requests simultaneously on the page) and the script execution takes a long time, the session file will be blocked and the performance will be affected. For each request, PHP executes session_start () to obtain the exclusive file lock. the exclusive lock is released only after the request is processed. In this way, multiple requests at the same time will cause blocking. The solution is as follows:

(1) after modifying the session variable, use session_write_close () to save the session data and release the filelock.

session_start(); $_SESSION['test'] = 'test';session_write_close(); //do something

(2) use the session_set_save_handler () function 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 (); // register the session_write_close () function as the register_shutdown_function () function. Session_set_save_handler ($ handler, true );

You can implement and encapsulate the above code and use mysql or other memory databases to manage session data. Cluster
Session Data sharing issues.

Http://www.bkjia.com/PHPjc/998560.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/998560.htmlTechArticlePHP session file exclusive lock causes blocking problem solution, PHP default session processor is session. save_handler = files (that is, the file ). If the same client sends multiple concurrent messages at the same time...

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.