PHP session file exclusive lock cause blocking problem Workaround _php Tutorial

Source: Internet
Author: User
Tags php session

Workaround for blocking problems caused by exclusive lock of PHP session file


This article mainly introduces the PHP session file exclusive lock caused by blocking, this article explains PHP using the default file session processor is easy to cause blocking problem resolution, the need for friends can refer to the following

PHP Default Session processor is Session.save_handler = files (that is, file). If the same client concurrently sends multiple requests (such as Ajax sending multiple requests at the same time on the page), and the script executes longer, it can cause the session file to block and affect performance. Because PHP executes session_start () for each request, the file exclusive lock is taken, and the exclusive lock is released only after the request processing has ended. In this way, multiple requests can cause blocking at the same time. The solution is as follows:

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

?

1

2

3

4

5

6

Session_Start ();

$_session[' test ' = ' test ';

Session_write_close ();

Do something

(2) using the Session_set_save_handler () function is the implementation of custom session processing.

?

1

2

3

4

5

6

7

8

9

13 /p>

+

+

/

/

/

+

+

+

+

25

+

+

$

+

+

+

+

+

+

+

[+]

+

+

All

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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 a register_shutdown_function () function.

Session_set_save_handler ($handler, true);

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

The session data sharing issue.

http://www.bkjia.com/PHPjc/1000119.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000119.html techarticle workaround for blocking problems caused by exclusive lock of PHP session file This article mainly introduces the PHP session file exclusive lock caused by blocking, this article explains PHP using the default file session processor is easy to guide ...

  • 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.