关于PHP session并发及session读写锁问题估计各大程序员都不会想到这个问题,因为一般情况我们不会使用session来做并发操作了,但有时也有可能用到,下面整理一个session并发及session读写锁文章供各位参考。
PHP这门程序设计语言简单得令人发指,那是因为PHP的作者们太神通。今天我来谈谈所有的phper都熟悉的session(会话)。
需要说明的是:
4./session/indexFile和/session/indexRedis模板中两个ajax请求,/session/setUserFile和/session/setUserRedis立即执行,/session/setLoginFile和/session/setLoginRedis延迟300ms,是为了模拟同一个用户,同时在两个页面(请求)修改会话数据
执行结果表象:
第一次访问:
第二次访问:
请求:/session/indexredis
第一次访问:

第二次访问:

手册中有这样的描述:
void session_write_close ( void )
Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.
也就是说session是有锁的,为防止并发的写会话数据。php自带的的文件保存会话数据是加了一个互斥锁(session_start()的时候),从而解释了上面呈现的两个请求响应时间相同。但是以redis保存会话数据时,第二个ajax虽然没有阻塞,但是会话数据并没有写入到redis,那我们追溯一下源码就有答案了。

代码如下 | 复制代码 |
<?php final class SessionController extends YafController_Abstract { public function setUserFileAction() { session_start(); $_SESSION['user_name'] = 'xudianyang'; $_SESSION['user_id'] = '123'; sleep(3); echo json_encode($_SESSION); return false; } public function setLoginFileAction() { session_start(); $_SESSION['last_time'] = time(); echo json_encode($_SESSION); return false; } public function indexFileAction() { // Auto Rend View } public function getSessionFileAction() { session_start(); var_dump($_SESSION); return false; } public function setUserRedisAction() { $session = CoreFactory::session(); $session->set('user_name', 'xudianyang'); $session->set('user_id', '123'); sleep(3); echo json_encode($_SESSION); return false; } public function setLoginRedisAction() { $session = CoreFactory::session(); $session->set('last_time', time()); echo json_encode($_SESSION); return false; } public function indexRedisAction() { // Auto Rend View } public function getSessionRedisAction() { $session = CoreFactory::session(); var_dump($_SESSION); return false; } } indexfile.phtml
<title>测试session并发锁问题</title> <meta charset="utf-8"> <script type="text/javascript" src="/assets/js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $.ajax({ url: "/session/setUserFile", type: "get", dataType: "json", success: function(response){ console.info(response.last_time); } }); setTimeout(function(){ $.ajax({ url: "/session/setLoginFile", type: "get", dataType: "json", success: function(response){ console.info(response.last_time); } }); }, 300); </script> 同时发起2两个ajax请求 indexredis.phtml
<title>测试session并发锁问题</title> <meta charset="utf-8"> <script type="text/javascript" src="/assets/js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $.ajax({ url: "/session/setUserRedis", type: "get", dataType: "json", success: function(response){ console.info(response.last_time); } }); setTimeout(function(){ $.ajax({ url: "/session/setLoginRedis", type: "get", dataType: "json", success: function(response){ console.info(response.last_time); } }); }, 300); </script> 同时发起2两个ajax请求 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
