关于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,是为了模拟同一个用户,同时在两个页面(请求)修改会话数据
执行结果表象:
第二次访问:
本书将PHP开发与MySQL应用相结合,分别对PHP和MySQL做了深入浅出的分析,不仅介绍PHP和MySQL的一般概念,而且对PHP和MySQL的Web应用做了较全面的阐述,并包括几个经典且实用的例子。 本书是第4版,经过了全面的更新、重写和扩展,包括PHP5.3最新改进的特性(例如,更好的错误和异常处理),MySQL的存储过程和存储引擎,Ajax技术与Web2.0以及Web应用需要注意的安全
400
第二次访问:
手册中有这样的描述:
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,那我们追溯一下源码就有答案了。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7">
<tr>
<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy7241')">复制代码</td>
</tr>
<tr>
<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7241><pre class="brush:php;toolbar:false;" title=""><?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;
}
}<!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>
同时发起2两个ajax请求
</body>
</html><!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>
同时发起2两个ajax请求
</body>
</html>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号