Home > Backend Development > PHP Tutorial > PHP session optimally writes information to memcache for management_PHP tutorial

PHP session optimally writes information to memcache for management_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:01:59
Original
990 people have browsed it

PHP session optimally writes information into memcache for management

I also talked about the methods and benefits of using memcache to store data information cache, which can reduce the number of database accesses and reduce the pressure on the database when the number of visits is large

To store and manage sessions in memcache, you need to understand the use of memcache, session and session_set_save_handler()

Similarly write a public class first, using static member methods of course

The memcache command is operated by telnet

Similarly create the required files in the root directory

Memsession.class.php is the public memcache storage class file, one.php, two.php and three.php are test files, and items.php is the output data array

session.class.php:

First define the variables used to connect to memcache and initialize them

<!--?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


Note that NS is a constant, define subscript

Reinitialization method

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


Open the session and define calls to open, close and other methods in this class

    	//开启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

The next step is to define the methods called above

open() and close() only need to return true, but the parameters of open() are path and name

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

read() only needs to have the PHPSESSID parameter

But it is necessary to determine whether the incoming out parameter has a value, and if it has a value, return the out data

    	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():

Returns its own id, data, and lifespan

    	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() and gc():

destroy() calls its own delete method

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

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

Next you need to define a method to pass in PHPSESSID

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

    		return $session_key;
    	}
Copy after login

The results show

If successful, display in telnet

Indicates that the session data information is successfully stored in memcache


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/971083.htmlTechArticlePHP session optimally writes information into memcache for management. We also talked about the method of using memcache to store data information cache. And benefits, this can reduce the number of database accesses and reduce access...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template