ZendFramework implements the method for storing sessions in memcache

Source: Internet
Author: User
This article mainly introduces the ZendFramework method for storing sessions in memcache, and analyzes the implementation skills of storing sessions in memcache in the ZendFramework framework in combination with instances, for more information about Zend Framework, see the following example. We will share this with you for your reference. The details are as follows:

In zend framework, sessions can be stored in the database, but memcache is not supported yet. I have to implement it simply.

The following is SaveHandler named Memcached. php: put it in the/Zend/Session/SaveHandler Directory. the code is as follows (php_memcache is required. due to the character length limit, I have removed some comments ):

Require_once 'zend/Session. php '; require_once' Zend/Config. php '; class implements failed {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 sessio N ** 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 shocould 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 ining '. 'configuration options for Zend_Session_SaveHandler_Memcached. ');} foreach ($ config as $ key => $ value) {do {switch ($ key) {case self: MEMC ACHED: $ 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);}/*** create memcached connection object ** @ return void */public function createMemcached ($ config) {$ mc = new Memcache; fo Reach ($ 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 shocould 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 shocould be overridden ** @ param boolean $ overrideLifetime * @ return response */public function setOverrideLifetime ($ overrideLife Time) {$ 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 ti Me ** @ param * @ param array $ value * @ return int */public function close () {return true;} public function read ($ id) {$ return = ''; $ value = $ this-> _ memcached-> get ($ id); // get data 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 ('mod Ified '=> time (), 'data' => (string) $ data); $ value = $ this-> _ memcached-> get ($ id ); // obtain the data 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 fun Ction 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 );}}

Configuration (multiple memcache servers can be added for distribution ):

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

After the configuration, the session is used in the same way as before, regardless of how the underlying layer is implemented!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.