使用zend framework 搭建網站(六)–為Zend_Session 添加一個Memcache_ SaveHandler

來源:互聯網
上載者:User
使用zend framework 搭建網站(六)--為Zend_Session 添加一個Memcache_ SaveHandler2008/11/25 10:05 A.M.
        在zend framework中,已經可以將session儲存在資料庫中了,不過還不支援memcache,我簡單得實現了一下。 

        下面是SaveHandler,檔案名稱為 :Memcached.php ,將其放在 /Zend/Session/SaveHandler 目錄下,代碼如下(需要有php_memcache支援,因為字元長度限制,我把部分注釋去掉了):

<?php

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);

        }

    }

   

    /**

     * 建立memcached連線物件

     * 

     * @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);                        //擷取資料


        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);                        //擷取資料


        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);

    }       

}


配置(可以添加多台memcache伺服器,做分布式):
$config = array(
    'memcached'=> array(
        array(
            'ip'=>'192.168.0.1', 
            'port'=>11211
        )
    ),
    'lifetime' =>123334
);
//create your Zend_Session_SaveHandler_Memcached
//set the save handler for Zend_Session 
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_Memcached($config));
//start your session!
Zend_Session::start(); 

配置好後,session的使用方法和以前一樣,不用管底層是怎麼實現的!


冷鋒: http://hi.baidu.com/billdkj/blog/item/049d04f0b2a896c47831aa1c.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.