Zend Framework實(shí)現(xiàn)將session存儲(chǔ)在memcache中的方法
本文實(shí)例講述了Zend Framework實(shí)現(xiàn)將session存儲(chǔ)在memcache中的方法。分享給大家供大家參考,具體如下:
在zend framework中,已經(jīng)可以將session存儲(chǔ)在數(shù)據(jù)庫(kù)中了,不過(guò)還不支持memcache,我簡(jiǎn)單得實(shí)現(xiàn)了一下。
下面是SaveHandler,文件名為 :Memcached.php ,將其放在 /Zend/Session/SaveHandler 目錄下,代碼如下(需要有php_memcache支持,因?yàn)樽址L(zhǎng)度限制,我把部分注釋去掉了):
require_once 'Zend/Session.php'; require_once 'Zend/Config.php'; class Zend_Session_SaveHandler_Memcached implements Zend_Session_SaveHandler_Interface { const LIFETIME = 'lifetime'; const OVERRIDE_LIFETIME = 'overrideLifetime'; const MEMCACHED = 'memcached'; protected $_lifetime = false; protected $_overrideLifetime = false; protected $_sessionSavePath; protected $_sessionName; protected $_memcached; /** * Constructor * * $config is an instance of Zend_Config or an array of key/value pairs containing configuration options for * Zend_Session_SaveHandler_Memcached . These are the configuration options for * Zend_Session_SaveHandler_Memcached: * * * sessionId => The id of the current session * sessionName => The name of the current session * sessionSavePath => The save path of the current session * * modified => (string) Session last modification time column * * lifetime => (integer) Session lifetime (optional; default: ini_get('session.gc_maxlifetime')) * * overrideLifetime => (boolean) Whether or not the lifetime of an existing session should be overridden * (optional; default: false) * * @param Zend_Config|array $config User-provided configuration * @return void * @throws Zend_Session_SaveHandler_Exception */ public function __construct($config) { if ($config instanceof Zend_Config) { $config = $config->toArray(); } else if (!is_array($config)) { /** * @see Zend_Session_SaveHandler_Exception */ require_once 'Zend/Session/SaveHandler/Exception.php'; throw new Zend_Session_SaveHandler_Exception( '$config must be an instance of Zend_Config or array of key/value pairs containing ' . 'configuration options for Zend_Session_SaveHandler_Memcached .'); } foreach ($config as $key => $value) { do { switch ($key) { case self::MEMCACHED: $this->createMemcached($value); break; case self::LIFETIME: $this->setLifetime($value); break; case self::OVERRIDE_LIFETIME: $this->setOverrideLifetime($value); break; default: // unrecognized options passed to parent::__construct() break 2; } unset($config[$key]); } while (false); } } /** * 創(chuàng)建memcached連接對(duì)象 * * @return void */ public function createMemcached($config){ $mc = new Memcache; foreach ($config as $value){ $mc->addServer($value['ip'], $value['port']); } $this->_memcached = $mc; } public function __destruct() { Zend_Session::writeClose(); } /** * Set session lifetime and optional whether or not the lifetime of an existing session should be overridden * * $lifetime === false resets lifetime to session.gc_maxlifetime * * @param int $lifetime * @param boolean $overrideLifetime (optional) * @return Zend_Session_SaveHandler_Memcached */ public function setLifetime($lifetime, $overrideLifetime = null) { if ($lifetime < 0) { /** * @see Zend_Session_SaveHandler_Exception */ require_once 'Zend/Session/SaveHandler/Exception.php'; throw new Zend_Session_SaveHandler_Exception(); } else if (empty($lifetime)) { $this->_lifetime = (int) ini_get('session.gc_maxlifetime'); } else { $this->_lifetime = (int) $lifetime; } if ($overrideLifetime != null) { $this->setOverrideLifetime($overrideLifetime); } return $this; } /** * Retrieve session lifetime * * @return int */ public function getLifetime() { return $this->_lifetime; } /** * Set whether or not the lifetime of an existing session should be overridden * * @param boolean $overrideLifetime * @return Zend_Session_SaveHandler_Memcached */ public function setOverrideLifetime($overrideLifetime) { $this->_overrideLifetime = (boolean) $overrideLifetime; return $this; } public function getOverrideLifetime() { return $this->_overrideLifetime; } /** * Retrieve session lifetime considering * * @param array $value * @return int */ public function open($save_path, $name) { $this->_sessionSavePath = $save_path; $this->_sessionName = $name; return true; } /** * Retrieve session expiration time * * @param * @param array $value * @return int */ public function close() { return true; } public function read($id) { $return = ''; $value = $this->_memcached->get($id); //獲取數(shù)據(jù) if ($value) { if ($this->_getExpirationTime($value) > time()) { $return = $value['data']; } else { $this->destroy($id); } } return $return; } public function write($id, $data) { $return = false; $insertDate = array('modified' => time(), 'data' => (string) $data); $value = $this->_memcached->get($id); //獲取數(shù)據(jù) if ($value) { $insertDate['lifetime'] = $this->_getLifetime($value); if ($this->_memcached->replace($id,$insertDate)) { $return = true; } } else { $insertDate['lifetime'] = $this->_lifetime; if ($this->_memcached->add($id, $insertDate,false,$this->_lifetime)) { $return = true; } } return $return; } public function destroy($id) { $return = false; if ($this->_memcached->delete($id)) { $return = true; } return $return; } public function gc($maxlifetime) { return true; } protected function _getLifetime($value) { $return = $this->_lifetime; if (!$this->_overrideLifetime) { $return = (int) $value['lifetime']; } return $return; } protected function _getExpirationTime($value) { return (int) $value['modified'] + $this->_getLifetime($value); } }
配置(可以添加多臺(tái)memcache服務(wù)器,做分布式):
$config = array( 'memcached'=> array( array( 'ip'=>'192.168.0.1', 'port'=>11211 ) ), 'lifetime' =>123334 ); //create your Zend_Session_SaveHandler_DbTable and //set the save handler for Zend_Session Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_Memcached($config)); //start your session! Zend_Session::start();
配置好后,session的使用方法和以前一樣,不用管底層是怎么實(shí)現(xiàn)的!
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門(mén)教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《Yii框架入門(mén)及常用技巧總結(jié)》、《ThinkPHP入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
- Zend Framework實(shí)現(xiàn)多服務(wù)器共享SESSION數(shù)據(jù)的方法
- Zend Framework入門(mén)教程之Zend_Mail用法示例
- Zend Framework入門(mén)教程之Zend_Db數(shù)據(jù)庫(kù)操作詳解
- ZendFramework框架實(shí)現(xiàn)連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的方法
- Zend Framework框架實(shí)現(xiàn)類似Google搜索分頁(yè)效果
- Zend Framework上傳文件重命名的實(shí)現(xiàn)方法
- Zend Framework開(kāi)發(fā)入門(mén)經(jīng)典教程
- Zend Framework教程之Zend_Config_Ini用法分析
- Zend Framework教程之Zend_Config_Xml用法分析
- Zend Framework教程之Zend_Registry對(duì)象用法分析
- Zend Framework入門(mén)教程之Zend_Session會(huì)話操作詳解
相關(guān)文章
PHP支付系統(tǒng)設(shè)計(jì)與典型案例分享
這篇文章主要為大家詳細(xì)介紹了PHP支付系統(tǒng)設(shè)計(jì)與典型案例,可以用作小型支付系統(tǒng),也可以用做第三方應(yīng)用接入開(kāi)放平臺(tái)時(shí)的支付流水系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08tp5.1 框架數(shù)據(jù)庫(kù)-數(shù)據(jù)集操作實(shí)例分析
這篇文章主要介紹了tp5.1 框架數(shù)據(jù)庫(kù)-數(shù)據(jù)集操作,結(jié)合實(shí)例形式分析了tp5.1 框架數(shù)據(jù)庫(kù)查詢結(jié)果數(shù)據(jù)集獲取、遍歷相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-05-05Bo-Blog專用的給Windows服務(wù)器的IIS Rewrite程序
Bo-Blog專用的給Windows服務(wù)器的IIS Rewrite程序...2007-08-08用php制作簡(jiǎn)單分頁(yè)(從數(shù)據(jù)庫(kù)讀取記錄)的方法詳解
本篇文章介紹了,使用php制作簡(jiǎn)單分頁(yè)(從數(shù)據(jù)庫(kù)讀取記錄)的方法詳解。需要的朋友參考下2013-05-05CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫(kù)增刪改查操作總結(jié)
這篇文章主要介紹了CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫(kù)增刪改查操作,結(jié)合實(shí)例形式總結(jié)分析了CI框架針對(duì)mysql數(shù)據(jù)庫(kù)增刪改查操作的模型、控制器及視圖相關(guān)定義與使用技巧,需要的朋友可以參考下2018-05-05PHPMailer發(fā)送HTML內(nèi)容、帶附件的郵件實(shí)例
這篇文章主要介紹了PHPMailer發(fā)送HTML內(nèi)容、帶附件的郵件實(shí)例,發(fā)送的內(nèi)容包含圖片和文字,附件則發(fā)送的一個(gè)EXCEL表,需要的朋友可以參考下2014-07-07