PHP Memcached 替换 系统$_SESSION
<?php /**Memcache替换SESSION类 * +--------------------------------- * @Created by PHPSTORM. * @User: schh Singee * Class MemcacheSession **+--------------------------------- */ class MemcacheSession extends MemcachePool{ //memcached服务器连接地址 private $_MEMCACHEAUTH = array( 'host' => 'localhost', 'port' => 11211, ); /* 获取一些初始化设置值 */ private $_SESSION_NAME = 'MEM'; //sessionid的名称 默认PHPSESSID private $_SESSION_TIME = 0; //sessionid这个cookie的最大保存时间 private $_SESSION_EXPIRE = 86400; //session键值对在memcached里面的过期时间 private $_SESSION_MEMKEY = ""; //sessionid值 /* 自定义的_session_start()方法,替换PHP的原生session_start()方法 逻辑应该是比较清楚的 */ function _session_start() { //开启Memcache服务 parent::connect($this->_MEMCACHEAUTH['host'],$this->_MEMCACHEAUTH['port']); //获取COOKIE值(KEY值)不存在则设置 存在则根据KEY值获取该内存下的内容 if ( empty($_COOKIE[$this->_SESSION_NAME]) ) { $_SESSION_MEMKEY = md5( uniqid() ); //唯一的KEY setcookie($this->_SESSION_NAME, $_SESSION_MEMKEY , $this->_SESSION_TIME, "/",'.cwddd.com'); $_SESSION = array(); } else { //获取COOKIE $_SESSION_MEMKEY = $_COOKIE[$this->_SESSION_NAME]; //获取该KEY值下的值 $_SESSION = parent::get($_SESSION_MEMKEY); if ( $_SESSION === FALSE ) { $_SESSION = array(); } } $this->_SESSION_MEMKEY = $_SESSION_MEMKEY; //注册一个handler,这个函数会在页面执行完的时候执行 register_shutdown_function(array($this, "_session_save_handler")); } /* 页面最后执行的方法,用于保存session值,以及关闭memcached连接 */ function _session_save_handler() { //执行完毕把当前SESSION值存入内存 parent::set($this->_SESSION_MEMKEY,$_SESSION,0,$this->_SESSION_EXPIRE); parent::close(); } //清空当前内存 function _destroy(){ parent::delete($_COOKIE[$this->_SESSION_NAME]); } } $mem = new MemcacheSession(); //自动开启MEM $mem->_session_start(); ?>
因本人使用的是ThinkPHP框架测试,所以只要在入口文件index.php引入
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // 应用入口文件 // 检测PHP环境 if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !'); // 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false define('APP_DEBUG',True); // 定义应用目录 define('APP_PATH','./Application/'); define('BIND_MODULE','Home'); //入口Memcache替换SESSION require './Memcache/MemcacheSession.class.php'; // 引入ThinkPHP入口文件 require './ThinkPHP/ThinkPHP.php'; // 亲^_^ 后面不需要任何代码了 就是如此简单
接下来创建一个测试控制器:
<?php /**++++++++++++++++++++++++++++++++++++++ * @Created by PhpStorm. * @User: schh Singee * @Date: 2015/11/4 * @Time: 18:04 **++++++++++++++++++++++++++++++++++++++ */ namespace Home\Controller; use Think\Controller; class TestController extends Controller{ function Iset(){ set_time_limit(0); for($i=0;$i<50000000;$i++){ $_SESSION['a'.$i] = array('status'=>array(1=>2),'tudde'=>array('1-2'));
} } function Iget(){ print_r(session()); } }
以上就大功告成了。
注意:
1、首先得安装Memcache服务,网上下载很多。内容默认使用64M,修改内存大小Windows下可直接修改注册表。通过stats可以查看当前Memcache详情。
2、修改php.ini打开memcache.dll。