Home > php教程 > php手册 > body text

php之session最优将信息写入memcache中管理

WBOY
Release: 2016-06-13 09:10:41
Original
919 people have browsed it

php之session最优将信息写入memcache中管理

前面也讲到了用memcache存储数据信息缓存的方法和好处,这样能够减少访问数据库的次数,减少访问量大时对数据库的压力

将session存储到memcache中管理需要了解memcache、session的使用和session_set_save_handler()

同样先编写一个公用的类,当然采用静态的成员方法

memcache 指令用telnet操作

 

同样现在根目录下建立需要用到的文件

其中memsession.class.php 是公用的memcache存储类文件,one.php、two.php和three.php是测试文件,items.php 是输出数据数组的

session.class.php中:

首先定义连接memcache用到的变量并初始化

 

<!--?php

    class MemSession{

    	private static $handler=null;
    	private static $lifetime=null;
    	private static $time=null;
    	const NS=&#39;session_&#39;;  //定义下标
     ...
    	&#160;...
}

&#160; &#160; $memcache=new Memcache;
&#160; &#160; //连接memcache
&#160; &#160; $memcache--->connect("localhost",11211) or die("could not connect");
    MemSession::start($memcache);
Copy after login


注意的是 NS 为常量,定义下标

再初始化方法

    	//初始化方法
    	private static function init($handler){
    		self::$handler=$handler;
    		self::$lifetime=ini_get(&#39;session.gc_maxlifetime&#39;);
    		self::$time=time();
    	}
Copy after login


开启session,并定义调用本类中的open、close等方法

    	//开启session
    	public static function start(Memcache $memcache){
    		//首先将属性初始化
    		self::init($memcache);  //调用handler,以后调用handler时都是用memcache
    		session_set_save_handler(
    			array(__CLASS__,&#39;open&#39;),//调用本类的open方法
    			array(__CLASS__,&#39;close&#39;),
    			array(__CLASS__,&#39;read&#39;),
    			array(__CLASS__,&#39;write&#39;),
    			array(__CLASS__,&#39;destroy&#39;),
    			array(__CLASS__,&#39;gc&#39;)
    			);

    		//调用session_start()
    		session_start();
    	}
Copy after login

接下来就是定义上面调用的这些方法

open() 和 close() 只要返回真就可以,但 open() 的参数为 路径(path) 和 名称(name)

    	public static function open($path, $name){
    		return true;
    	}
    	public static function close(){
    		return true;
    	}
Copy after login

read() 只需要有PHPSESSID参数即可

但要判断传入的out 参数是否有值,有值就返回out的数据

    	public static function read($PHPSESSID){
    		$out=self::$handler->get(self::session_key($PHPSESSID));  //得到该下标输出的数据
    		if($out===false || $out ==null){
    			return &#39;&#39;;  //out得到数据没有,返回空
    		}
    		return $out;  //返回得到的数据
    	}
Copy after login

write() :

返回自身的id,数据,和生命时长

    	public static function write($PHPSESSID, $data){
    		//判断是否有数据
    		$method=$data ? &#39;set&#39; : &#39;relpace&#39;;
    		return self::$handler->$method(self::session_key($PHPSESSID), $data, MEMCACHE_COMPRESSED, self::$lifetime);

    	}
Copy after login

destroy() 和 gc() :

destroy()调用自身的delete方法

    	public static function destroy($PHPSESSID){
    		return self::$handler->delete(self::session_key($PHPSESSID));  //调用delete方法

    	}
    	public static function gc($lifetime){
    			return true;
    	}
Copy after login

接下来需要定义一个传入PHPSESSID的方法

    	private static function session_key($PHPSESSID){
    		$session_key=self::NS.$PHPSESSID; //键值为自身和传进来的phpsessid

    		return $session_key;
    	}
Copy after login

 

 

 

结果显示

如果成功,在telnet中显示

 

 

表示session数据信息储存到memcache成功

 


 

 

 

 

 

 

 

 

 

 

 

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!