Home >Backend Development >PHP Tutorial >Solution to blocking problem caused by PHP session file exclusive lock_PHP tutorial

Solution to blocking problem caused by PHP session file exclusive lock_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:53:37770browse

Solution to the blocking problem caused by PHP session file exclusive lock

This article mainly introduces the blocking caused by PHP session file exclusive lock. This article explains how easy it is to use the default file session processor in PHP. Friends in need can refer to the solution to the blocking problem

PHP’s default session handler is session.save_handler = files (i.e. files). If the same client sends multiple requests simultaneously (such as ajax sending multiple requests at the same time on the page) and the script execution time is long, the session file will be blocked and performance will be affected. Because for each request, PHP executes session_start(), it will obtain the file exclusive lock, and the exclusive lock will be 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, immediately use session_write_close() 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

1

2

3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

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";

4 5 6
session_start(); $_SESSION['test'] = 'test'; session_write_close(); //do something
(2) Use the session_set_save_handler() function to implement custom session processing.  ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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

21

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();

 

//第2个参数将函数 session_write_close() 注册为 register_shutdown_function() 函数。

session_set_save_handler($handler, true);

1

2

3 4

56 7 8 9 10 11
12
13
14 15 16 17 18 19 20 21 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 second 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 and use mysql or other in-memory database to manage session data. It can also be solved using clusters When, session data sharing problem. http://www.bkjia.com/PHPjc/1000119.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000119.htmlTechArticleSolution to the blocking problem caused by the PHP session file exclusive lock. This article mainly introduces the blocking caused by the PHP session file exclusive lock. This article explains how easy it is to cause PHP to use the default file session processor...
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