最新消息:欢迎加入小松的QQ群一起讨论一起学习,本站启用小松自己写的redis缓存,文章发布修改自动删除缓存,现在速度杠杠的

php session 引起的程序卡顿

PHP 1132浏览 0评论

最近公司客服反映内部系统有卡顿的情况,刚开始以为mysql锁了,但是不是,然后开始问题的分析,终于找到问题,原来是session锁了,然后到百度上去找了一下,session锁的相关问题
先说一下php session机制
session_start()是session机制的开始,它有一定概率开启垃圾回收,因为session是存放在文件中,
,SESSION的回收是要删文件的,这个概率是根据php.ini的配置决定的,session.gc_divisor 与 session.gc_probability 合起来定义了在每个会话初始化时启动 gc(garbage collection 垃圾回收)进程的概率。此概率用 gc_probability/gc_divisor 计算得来。例如 1/100 意味着在每个请求中有 1% 的概率启动 gc 进程。session.gc_divisor 默认为 100。
怎么解决这个
1、seesion 用完之后用session_write_close关闭session
2、session 保存到数据库或session保存到文件,SESSION存放在数据库示例

<?php 
/* 
CREATE TABLE `ws_sessions` ( 
 `session_id` varchar(255) binary NOT NULL default '', 
 `session_expires` int(10) unsigned NOT NULL default '0', 
 `session_data` text, 
 PRIMARY KEY (`session_id`) 
) TYPE=InnoDB; 
*/ 
class session { 
 // session-lifetime 
 var $lifeTime; 
 // mysql-handle 
 var $dbHandle; 
 function open($savePath, $sessName) { 
 // get session-lifetime 
 $this->lifeTime = get_cfg_var("session.gc_maxlifetime"); 
 // open database-connection 
 $dbHandle = @mysql_connect("localhost","root",""); 
 $dbSel = @mysql_select_db("test",$dbHandle); 
 // return success 
 if(!$dbHandle || !$dbSel) 
 return false; 
 $this->dbHandle = $dbHandle; 
 return true; 
 } 
 function close() { 
 $this->gc(ini_get('session.gc_maxlifetime')); 
 // close database-connection 
 return @mysql_close($this->dbHandle); 
 } 
 function read($sessID) { 
 // fetch session-data 
 $res = mysql_query("SELECT session_data AS d FROM ws_sessions 
 WHERE session_id = '$sessID' 
 AND session_expires > ".time(),$this->dbHandle); 
 // return data or an empty string at failure 
 if($row = mysql_fetch_assoc($res)) 
 return $row['d']; 
 return ""; 
 } 
 function write($sessID,$sessData) { 
 // new session-expire-time 
 $newExp = time() + $this->lifeTime; 
 // is a session with this id in the database? 
 $res = mysql_query("SELECT * FROM ws_sessions 
 WHERE session_id = '$sessID'",$this->dbHandle); 
 // if yes, 
 if(mysql_num_rows($res)) { 
 // ...update session-data 
 mysql_query("UPDATE ws_sessions 
 SET session_expires = '$newExp', 
 session_data = '$sessData' 
 WHERE session_id = '$sessID'",$this->dbHandle); 
 // if something happened, return true 
 if(mysql_affected_rows($this->dbHandle)) 
 return true; 
 } 
 // if no session-data was found, 
 else { 
 // create a new row 
 mysql_query("INSERT INTO ws_sessions ( 
 session_id, 
 session_expires, 
 session_data) 
 VALUES( 
 '$sessID', 
 '$newExp', 
 '$sessData')",$this->dbHandle); 
 // if row was created, return true 
 if(mysql_affected_rows($this->dbHandle)) 
 return true; 
 } 
 // an unknown error occured 
 return false; 
 } 
 function destroy($sessID) { 
 // delete session-data 
 mysql_query("DELETE FROM ws_sessions WHERE session_id = '$sessID'",$this->dbHandle); 
 // if session was deleted, return true, 
 if(mysql_affected_rows($this->dbHandle)) 
 return true; 
 // ...else return false 
 return false; 
 } 
 function gc($sessMaxLifeTime) { 
 // delete old sessions 
 mysql_query("DELETE FROM ws_sessions WHERE session_expires < ".time(),$this->dbHandle); 
 // return affected rows 
 return mysql_affected_rows($this->dbHandle); 
 } 
} 
$session = new session(); 
session_set_save_handler(array(&$session,"open"), 
 array(&$session,"close"), 
 array(&$session,"read"), 
 array(&$session,"write"), 
 array(&$session,"destroy"), 
 array(&$session,"gc")); 
session_start(); 
// etc... 
?>
QQ交流群:136351212

如无特别说明,本站文章皆为原创,若要转载,务必请注明以下原文信息:
转载保留版权:小松博客» php session 引起的程序卡顿
本文链接地址:https://www.phpsong.com/76.html

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
木有头像就木有JJ!点这里按步骤申请Gravatar头像吧!