Zend Framework实现将session存储在memcache中的方法
本文实例讲述了ZendFramework实现将session存储在memcache中的方法。分享给大家供大家参考,具体如下:
在zendframework中,已经可以将session存储在数据库中了,不过还不支持memcache,我简单得实现了一下。
下面是SaveHandler,文件名为:Memcached.php,将其放在/Zend/Session/SaveHandler目录下,代码如下(需要有php_memcache支持,因为字符长度限制,我把部分注释去掉了):
require_once'Zend/Session.php';
require_once'Zend/Config.php';
classZend_Session_SaveHandler_MemcachedimplementsZend_Session_SaveHandler_Interface
{
constLIFETIME='lifetime';
constOVERRIDE_LIFETIME='overrideLifetime';
constMEMCACHED='memcached';
protected$_lifetime=false;
protected$_overrideLifetime=false;
protected$_sessionSavePath;
protected$_sessionName;
protected$_memcached;
/**
*Constructor
*
*$configisaninstanceofZend_Configoranarrayofkey/valuepairscontainingconfigurationoptionsfor
*Zend_Session_SaveHandler_Memcached.Thesearetheconfigurationoptionsfor
*Zend_Session_SaveHandler_Memcached:
*
*
*sessionId=>Theidofthecurrentsession
*sessionName=>Thenameofthecurrentsession
*sessionSavePath=>Thesavepathofthecurrentsession
*
*modified=>(string)Sessionlastmodificationtimecolumn
*
*lifetime=>(integer)Sessionlifetime(optional;default:ini_get('session.gc_maxlifetime'))
*
*overrideLifetime=>(boolean)Whetherornotthelifetimeofanexistingsessionshouldbeoverridden
*(optional;default:false)
*
*@paramZend_Config|array$configUser-providedconfiguration
*@returnvoid
*@throwsZend_Session_SaveHandler_Exception
*/
publicfunction__construct($config)
{
if($configinstanceofZend_Config){
$config=$config->toArray();
}elseif(!is_array($config)){
/**
*@seeZend_Session_SaveHandler_Exception
*/
require_once'Zend/Session/SaveHandler/Exception.php';
thrownewZend_Session_SaveHandler_Exception(
'$configmustbeaninstanceofZend_Configorarrayofkey/valuepairscontaining'
.'configurationoptionsforZend_Session_SaveHandler_Memcached.');
}
foreach($configas$key=>$value){
do{
switch($key){
caseself::MEMCACHED:
$this->createMemcached($value);
break;
caseself::LIFETIME:
$this->setLifetime($value);
break;
caseself::OVERRIDE_LIFETIME:
$this->setOverrideLifetime($value);
break;
default:
//unrecognizedoptionspassedtoparent::__construct()
break2;
}
unset($config[$key]);
}while(false);
}
}
/**
*创建memcached连接对象
*
*@returnvoid
*/
publicfunctioncreateMemcached($config){
$mc=newMemcache;
foreach($configas$value){
$mc->addServer($value['ip'],$value['port']);
}
$this->_memcached=$mc;
}
publicfunction__destruct()
{
Zend_Session::writeClose();
}
/**
*Setsessionlifetimeandoptionalwhetherornotthelifetimeofanexistingsessionshouldbeoverridden
*
*$lifetime===falseresetslifetimetosession.gc_maxlifetime
*
*@paramint$lifetime
*@paramboolean$overrideLifetime(optional)
*@returnZend_Session_SaveHandler_Memcached
*/
publicfunctionsetLifetime($lifetime,$overrideLifetime=null)
{
if($lifetime<0){
/**
*@seeZend_Session_SaveHandler_Exception
*/
require_once'Zend/Session/SaveHandler/Exception.php';
thrownewZend_Session_SaveHandler_Exception();
}elseif(empty($lifetime)){
$this->_lifetime=(int)ini_get('session.gc_maxlifetime');
}else{
$this->_lifetime=(int)$lifetime;
}
if($overrideLifetime!=null){
$this->setOverrideLifetime($overrideLifetime);
}
return$this;
}
/**
*Retrievesessionlifetime
*
*@returnint
*/
publicfunctiongetLifetime()
{
return$this->_lifetime;
}
/**
*Setwhetherornotthelifetimeofanexistingsessionshouldbeoverridden
*
*@paramboolean$overrideLifetime
*@returnZend_Session_SaveHandler_Memcached
*/
publicfunctionsetOverrideLifetime($overrideLifetime)
{
$this->_overrideLifetime=(boolean)$overrideLifetime;
return$this;
}
publicfunctiongetOverrideLifetime()
{
return$this->_overrideLifetime;
}
/**
*Retrievesessionlifetimeconsidering
*
*@paramarray$value
*@returnint
*/
publicfunctionopen($save_path,$name)
{
$this->_sessionSavePath=$save_path;
$this->_sessionName=$name;
returntrue;
}
/**
*Retrievesessionexpirationtime
*
*@param*@paramarray$value
*@returnint
*/
publicfunctionclose()
{
returntrue;
}
publicfunctionread($id)
{
$return='';
$value=$this->_memcached->get($id);//获取数据
if($value){
if($this->_getExpirationTime($value)>time()){
$return=$value['data'];
}else{
$this->destroy($id);
}
}
return$return;
}
publicfunctionwrite($id,$data)
{
$return=false;
$insertDate=array('modified'=>time(),
'data'=>(string)$data);
$value=$this->_memcached->get($id);//获取数据
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;
}
publicfunctiondestroy($id)
{
$return=false;
if($this->_memcached->delete($id)){
$return=true;
}
return$return;
}
publicfunctiongc($maxlifetime)
{
returntrue;
}
protectedfunction_getLifetime($value)
{
$return=$this->_lifetime;
if(!$this->_overrideLifetime){
$return=(int)$value['lifetime'];
}
return$return;
}
protectedfunction_getExpirationTime($value)
{
return(int)$value['modified']+$this->_getLifetime($value);
}
}
配置(可以添加多台memcache服务器,做分布式):
$config=array( 'memcached'=>array( array( 'ip'=>'192.168.0.1', 'port'=>11211 ) ), 'lifetime'=>123334 ); //createyourZend_Session_SaveHandler_DbTableand //setthesavehandlerforZend_Session Zend_Session::setSaveHandler(newZend_Session_SaveHandler_Memcached($config)); //startyoursession! Zend_Session::start();
配置好后,session的使用方法和以前一样,不用管底层是怎么实现的!
更多关于zend相关内容感兴趣的读者可查看本站专题:《ZendFrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于ZendFramework框架的PHP程序设计有所帮助。