<?php
//用户自定义session类,使用memcache的方式来存储session信息
class MemSession{
private static $mem; //memcache对象
private static $maxlifetime; //文件最大存在时间
//自定义用户开启session的方式
public static function start(Memcache $mem){
self::$mem=$mem;
self::$maxlifetime=ini_get('session.gc_maxlifetime');
//将用户自定义session生命周期的方法注册到web服务器中
session_set_save_handler(
array(__CLASS__,'open'),
array(__CLASS__,'close'),
array(__CLASS__,'read'),
array(__CLASS__,'write'),
array(__CLASS__,'destroy'),
array(__CLASS__,'gc')
);
session_start();
}
//用户自定义session生命周期的方法如下:
public static function open($path,$name){
return true;
}
public static function close(){
return true;
}
public static function destroy($sid){
self::$mem->delete($sid,0);
}
public static function read($sid){
$data=self::$mem->get($sid);
if(empty($data)){
return '';
}else{
return $data;
}
}
public static function write($sid,$data){
self::$mem->set($sid,$data,MEMCACHE_COMPRESSED,self::$maxlifetime);
}
public static function gc($maxlifetime){
//此处不处理是因为memcache自己有设置自动清理保存时间
return true;
}
}
//start方法参数中传入一个已经连接上项目的memcache对象
$mem=new Memcache();
//向memcache连接池中添加主机
$mem->addServer('localhost',11211);
MemSession::start($mem);
?>
原文链接:https://blog.csdn.net/living_ren/article/details/78528618
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~