国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Zend Framework實(shí)現(xiàn)將session存儲(chǔ)在memcache中的方法

2024-05-04 23:44:15
字體:
供稿:網(wǎng)友
這篇文章主要介紹了Zend Framework實(shí)現(xiàn)將session存儲(chǔ)在memcache中的方法,結(jié)合實(shí)例形式分析了Zend Framework框架下將session存儲(chǔ)在memcache的實(shí)現(xiàn)技巧,需要的朋友可以參考下
 

本文實(shí)例講述了Zend Framework實(shí)現(xiàn)將session存儲(chǔ)在memcache中的方法。分享給大家供大家參考,具體如下:

在zend framework中,已經(jīng)可以將session存儲(chǔ)在數(shù)據(jù)庫中了,不過還不支持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_SessionZend_Session::setSaveHandler(new Zend_Session_SaveHandler_Memcached($config));//start your session!Zend_Session::start();

配置好后,session的使用方法和以前一樣,不用管底層是怎么實(shí)現(xiàn)的!



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 永城市| 招远市| 共和县| 阜康市| 宜都市| 南阳市| 西乌珠穆沁旗| 景洪市| 通河县| 竹北市| 乐业县| 奇台县| 德江县| 探索| 山东| 英吉沙县| 阿鲁科尔沁旗| 新乡县| 南陵县| 武功县| 那坡县| 逊克县| 屏边| 蒙阴县| 内丘县| 萝北县| 铜川市| 诸暨市| 周宁县| 绍兴县| 虎林市| 循化| 砀山县| 晴隆县| 吕梁市| 门头沟区| 广州市| 金山区| 大余县| 乐陵市| 贵阳市|