The Zend framework implements the method of storing the session in Memcache, zendmemcache_php tutorial

Source: Internet
Author: User
Tags zend framework

The Zend framework implements the method of storing the session in Memcache, Zendmemcache


This example describes how the Zend framework implements the session stored in the Memcache. Share to everyone for your reference, as follows:

In the Zend Framework, it is already possible to store the session in the database, but it does not support memcache, and I am simply implementing it.

Here is Savehandler, the file name is: memcached.php, put it in the/zend/session/savehandler directory, the code is as follows (need to have php_memcache support, because the character length limit, I removed some comments):

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 a instance of Zend_config or an array of key/value pairs containing configuration Options for * zend_session_savehandler_memcached.  These is 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 o f the current session * * modified = (string) session last modification Time column * * Lifetime =&GT ; (integer) Session Lifetime (optional; Default:ini_get (' session.gc_maxlifetime ')) * * Overridelifetime = (Boolean) Whether or not, the lifetime of an ex Isting session should be overridden * (optional; default:false) * * @param zend_config|array $config user-provi ded configuration * @return void * @throws zend_session_savehandler_exception */Public function __construct ($confi    g) {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 is an instance of Zend_config or array of Key/value PAI RS 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);   }}/** * Create memcached Connection object * * @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 a existing session should be overridden * * $lifetime = = = False resets lifetime to Session.gc_maxlifetime * * @param int $lifetime * @param boolean $override Lifetime (Optional) * @return zend_session_savehandler_memcached */PublIC function Setlifetime ($lifetime, $overrideLifetime = null) {if ($lifetime < 0) {/** * @see zend_sess      Ion_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->_lifet  Ime }/** * Set whether or not the lifetime of a existing session should be overridden * * @param boolean $overrideLif ETime * @return zend_session_savehandler_memcached */Public Function Setoverridelifetime ($overrideLifetime) {$t    His->_overridelifetime = (Boolean) $overrideLifetime;  return $this; } PubLic function Getoverridelifetime () {return $this->_overridelifetime; }/** * Retrieve session Lifetime considering * * @param array $value * @return int */Public function open ($s    Ave_path, $name) {$this->_sessionsavepath = $save _path;    $this->_sessionname = $name;  return true; }/** * Retrieve session Expiration Time * * @param * @param array $value * @return int */Public function CLO  Se () {return true;    Public function Read ($id) {$return = '; $value = $this->_memcached->get ($id);      Gets the 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 (' Modified ' = time (), ' data ' = = (string) $data); $value = $this->_memcached->get ($id); Gets 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 function Destroy ($id) {$return = false;    if ($this->_memcached->delete ($id)) {$return = true;  } return $return;  The 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 (can add more than one memcache server, do the distributed):

$config = Array (' memcached ' = = 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 using the same method as before, without the bottom of the tube is how to achieve!

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

Articles you may be interested in:

    • Zend Framework Framework Tutorial Zend_db_table_rowset Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table_row Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table Usage
    • Zend Framework Tutorial Zend_form component implement form submission and display Error prompt method
    • Introduction to Zend Framework Development Classic Tutorial
    • Zend Framework Smarty Extension Implementation method
    • Code analysis of routing mechanism of Zend framework framework
    • Zend Framework implementation of basic functions of the message book (with demo source download)
    • Zend Framework Paging Class usage
    • Zend Framework implements multi-file upload function instances
    • Zend Framework Introduction Environment configuration and the first Hello World example (with demo source download)
    • Zend Framework Tutorial to connect the database and perform additions and deletions of the method (attached to the demo source download)
    • Zend Framework Tutorial Zend_db_table Table Association Instance detailed

http://www.bkjia.com/PHPjc/1113723.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113723.html techarticle The Zend Framework implements the method of storing the session in Memcache, Zendmemcache This example describes how the Zend framework implements the session stored in Memcache. Share to everyone ...

  • Related Article

    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.