Memcache Session 共享背景与动机
一、相关背景介绍 1. Memcache及其多服务器数据共享机制的详细介绍,可参考链接:http://www.guigui8.com/index.php/archives/206.html。 2. Session机制概述: Session是服务端的一种会话管理机制,服务器通常采用类似散列表的数据结构(或直接用散列表)来存储状态信息。 当程序需要为特定客户端请求创建Session时,服务器首先检查该请求中是否包含Session标识——即SessionID。若已存在SessionID,说明此前已为该客户端建立过Session,服务器便依据此ID检索并使用对应的Session(若未找到则可能新建);若请求中不包含SessionID,服务器将为此客户端新建一个Session,并生成一个既唯一又难以被猜测仿造的SessionID与之关联。该SessionID将在本次响应中返回给客户端保存。 保存SessionID的方式通常采用Cookie,以便在交互过程中浏览器能自动按规则将该标识发送给服务器。 一般此类Cookie的名称类似于SEESIONID。例如WebLogic为Web应用程序生成的Cookie,如PHPSESSID=ByOK3vjFD75aPnrF3K2HmdnV6QZcEbzWoWiBYEnLerj,其名称即为PHPSESSID。
二、设计动机 在实际Web生产环境中,应用系统往往将不同业务分布到不同的服务器上进行负载均衡处理。 当需要跟踪当前在线用户信息时,若处于同一主域名下,可通过全域Cookie处理数据共享;若处于不同主域名下,则可通过观察者模式的中心化概念来解决相应问题,由此衍生出多种解决方案。本文将重点讲解前一种方案,即通过Memcache的多服务器数据共享技术来模拟Session,以实现当前在线用户数据在多服务器间的共享。 关于多服务器统一Session信息,主要要求如下: 1. 能够在Memcached指定的几台服务器上保存Session信息(依托前述Memcache多服务器数据共享功能); 2. 能够像Zend定义的session_start()之前通过session_id($sessid)那样,自定义SessionID的值。 3. 能在系统运行时,方便地在Memcached存储的Session信息与文件存储的Session信息之间切换。
三、代码实现 实现思路较为简单,即通过Memcache模拟Session机制,仅将存储介质替换为共享服务器的内存,从而达到多台分布式部署服务器间共享Session信息的目的。调用接口设计与Zend提供的Session操作函数有所区别,因此可以方便地在Memcache和文件的Session信息操作间切换。 以下代码已经过多次实际测试,能够满足上述功能需求。代码如下: 复制代码 代码如下: /** *=---------------------------------------------------------------------------= * MemcacheSession.class.php *=---------------------------------------------------------------------------= * * 实现基于Memcache存储的 Session 功能 * (模拟session机制,只是利用memcache将存储媒介换成共享服务器的内存) * * 缺点:暂时没有引入不同主域的session共享机制的实现策略。即只支持同主域下的实现。 * * Copyright(c) 2008 by guigui. All rights reserved. * @author guigui * @version $Id: MemcacheSession.class.php, v 1.0 2008/12/22 $ * @package systen * @link http://www.guigui8.com */ /** * class MemcacheSession * * 1. 设置客户端的Cookie来保存SessionID * 2. 把用户的数据保存在服务器端,通过Cookie中的Session Id来确定一个数据是否是用户的 */ class MemcacheSession { // {{{ 类成员属性定义 public $memObject = null; //memcache操作对象句柄 private $_sessId = ''; private $_sessKeyPrefix = 'sess_'; private $_sessExpireTime = 86400; private $_cookieDomain = '.guigui8.com'; //全域cookie域名 private $_cookieName = '_PROJECT_MEMCACHE_SESS'; private $_cookieExpireTime = ''; private $_memServers = array('192.168.0.3' => 11211, '192.168.0.4' => 11211); private $_sessContainer = array(); //当前用户的session信息 private static $_instance = null; //本类单例对象 // }}} /** * 单例对象获取的静态方法。 * (可以顺便提供memcache信息存储的服务器参数) * * @param string $host - memcache数据存储的服务器ip * @param integer $port - memcache数据存储的服务器端口号 * @param bool $isInit - 是否实例化对象的时候启动Session */ public static function getInstance($host='', $port=11211, $isInit = true) { if (null === self::$_instance) { self::$_instance = new self($host, $port, $isInit); } return self::$_instance; } /** * 构造函数 * * @param bool $isInit - 是否实例化对象的时候启动Session */ private function __construct($host='', $port=11211, $isInit = false){ !empty($host) && $this->_memServers = array(trim($host) => $port); $isInit && $this->start(); } /** *=-----------------------------------------------------------------------= *=-----------------------------------------------------------------------= * Public Methods *=-----------------------------------------------------------------------= *=-----------------------------------------------------------------------= */ /** * 启动Session操作 * * @param int $expireTime - Session失效时间,缺省是0,当浏览器关闭的时候失效, 该值单位是秒 */ public function start($expireTime = 0){ $_sessId = $_COOKIE[$this->_cookieName]; if (!$_sessId){ $this->_sessId = $this->_getId(); $this->_cookieExpireTime = ($expireTime > 0) ? time() + $expireTime : 0; setcookie($this->_cookieName, $this->_sessId, $this->_cookieExpireTime, "/", $this->_cookieDomain); $this->_initMemcacheObj(); $this->_sessContainer = array(); $this->_saveSession(); } else { $this->_sessId = $_sessId; $this->_sessContainer = $this->_getSession($_sessId); } } /** * setSessId * * 自定义的session id,通常没有必要经过cookie操作处理的(所以省略了cookie记录session_id) * * @param string $sess_id * @return boolean */ public function setSessId($sess_id){ $_sessId = trim($sess_id); if (!$_sessId){ return false; } else { $this->_sessId = $_sessId; $this->_sessContainer = $this->_getSession($_sessId); } } /** * 判断某个Session变量是否注册 * * @param string $varName - * @return bool 存在返回true, 不存在返回false */ public function isRegistered($varName){ if (!isset($this->_sessContainer[$varName])){ return false; } return true; } /** * 注册一个Session变量 * * @param string $varName - 需要注册成Session的变量名 * @param mixed $varValue - 注册成Session变量的值 * @return bool - 该变量名已经存在返回false, 注册成功返回true */ public function set($varName, $varValue){ $this->_sessContainer[$varName] = $varValue; $this->_saveSession(); return true; } /** * 获取一个已注册的Session变量值 * * @param string $varName - Session变量的名称 * @return mixed - 不存在的变量返回false, 存在变量返回变量值 */ public function get($varName){ if (!isset($this->_sessContainer[$varName])){ return false; } return $this->_sessContainer[$varName]; } /** * 销毁一个已注册的Session变量 * * @param string $varName - 需要销毁的Session变量名 * @return bool 销毁成功返回true */ public function delete($varName){ unset($this->_sessContainer[$varName]); $this->_saveSession(); return true; } /** * 销毁所有已经注册的Session变量 * * @return 销毁成功返回true */ public function destroy(){ $this->_sessContainer = array(); $this->_saveSession(); return true; } /** * 获取所有Session变量 * * @return array - 返回所有已注册的Session变量值 */ public function getAll(){ return $this->_sessContainer; } /** * 获取当前的Session ID * * @return string 获取的SessionID */ public function getSid(){ return $this->_sessId; } /** * 获取Memcache的服务器信息 * * @return array Memcache配置数组信息 */ public function getMemServers(){ return $this->_memServers; } /** * 设置Memcache的服务器信息 * * @param string $host - Memcache服务器的IP * @param int $port - Memcache服务器的端口 */ public function setMemServers($arr){ $this->_memServers = $arr; } /** * 添加Memcache服务器 * * @param string $host - Memcache服务器的IP * @param int $port - Memcache服务器的端口 */ public function addMemServer($host, $port){ $this->_memServers[trim($host)] = trim($port); $this->memObject->addServer($host, $port); } /** * 移除Memcache服务器(注意,这个只是移除配置,并不能实际从memcached的连接池移除) * * @param string $host - Memcache服务器的IP * @param int $port - Memcache服务器的端口 */ public function removeMemServer($host){ unset($this->_memServers[trim($host)]); } /** *=-----------------------------------------------------------------------= *=-----------------------------------------------------------------------= * Private Methods *=-----------------------------------------------------------------------= *=-----------------------------------------------------------------------= */ /** * 生成一个Session ID * * @return string 返回一个32位的Session ID */ private function _getId(){ return md5(uniqid(microtime())); } /** * 获取一个保存在Memcache的Session Key * * @param string $_sessId - 是否指定Session ID * @return string 获取到的Session Key */ private function _getSessKey($_sessId = ''){ $sessKey = ($_sessId == '') ? $this->_sessKeyPrefix.$this->_sessId : $this->_sessKeyPrefix.$_sessId; return $sessKey; } /** * 检查保存Session数据的路径是否存在 * * @return bool 成功返回true */ private function _initMemcacheObj(){ if (!class_exists('Memcache') || !function_exists('memcache_connect')){ $this->_showMessage('Failed: Memcache extension not install, please from http://pecl.php.net download and install'); } if ($this->memObject && is_object($this->memObject)){ return true; } $this->memObject = new Memcache; if (!empty($this->_memServers)) { foreach ($this->_memServers as $_host => $_port) { $this->memObject->addServer($_host, $_port); } } return true; } /** * 获取Session文件中的数据 * * @param string $_sessId - 需要获取Session数据的SessionId * @return unknown */ private function _getSession($_sessId = ''){ $this->_initMemcacheObj(); $sessKey = $this->_getSessKey($_sessId); $sessData = $this->memObject->get($sessKey); if (!is_array($sessData) || empty($sessData)){ //this must be $_COOKIE['__SessHandler'] error! return array(); } return $sessData; } /** * 把当前的Session数据保存到Memcache * * @param string $_sessId - Session ID * @return 成功返回true */ private function _saveSession($_sessId = ''){ $this->_initMemcacheObj(); $sessKey = $this->_getSessKey($_sessId); if (empty($this->_sessContainer)){ $ret = @$this->memObject->set($sessKey, $this->_sessContainer, false, $this->_sessExpireTime); }else{ $ret = @$this->memObject->replace($sessKey, $this->_sessContainer, false, $this->_sessExpireTime); } if (!$ret){ $this->_showMessage('Failed: Save sessiont data failed, please check memcache server'); } return true; } /** * 显示提示信息 * * @param string $strMessage - 需要显示的信息内容 * @param bool $isFailed - 是否是失败信息, 缺省是true */ private function _showMessage($strMessage, $isFailed = true){ return; if ($isFailed){ echo ($strMessage); } echo $strMessage; } 四、应用场景 1. 本地Session存储,与原始Session操作方式一样,没有任何改变。例如: 复制代码 代码如下: session_start(); $_SESSION['file_session_info']= '本地文件保存的session信息'; //本地文件保存的session 2. Memcache共享服务器的Session存储 复制代码 代码如下: $mem= MemcacheSession::getInstance('192.168.0.4', 11211); $mem->addMemServer('192.168.0.4',11211); $mem->addMemServer('192.168.0.5',11211); //如果cookie功能不可用,则根据其他参数传递的唯一信息,设置映射为session_id if(1) { $sn= '838ece1033bf7c7468e873e79ba2a3ec'; $mem->setSessId($sn); } $mem->set('name','guigui'); //多台memcache服务器共享的session $mem->set('addr','wuhan'); //多台memcache服务器共享的session //$mem->destroy(); 3. 分别获取本地和Memcache存储的Session信息 复制代码 代码如下: $addr= $mem->get('addr'); $_MEM_SESSION= $mem->getAll(); echo"localhost file session:"; var_dump($_SESSION); echo"memcache session:"; var_dump($_MEM_SESSION); //$res= $mem->delete('name');