[ Indice ]

Riferimento incrociato di Joomla! 1.5.14 - VM 1.1.4

Servizio fornito da VMItalia
Classe:   Funzione:   Variabile:   Costante:  
Storico Ricerche +

titolo

Corpo

[chiudi]

/libraries/joomla/session/storage/ -> memcache.php (sorgente)

   1  <?php
   2  /**
   3  * @version        $Id:eaccelerator.php 6961 2007-03-15 16:06:53Z tcp $
   4  * @package        Joomla.Framework
   5  * @subpackage    Session
   6  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * Memcache session storage handler for PHP
  20   *
  21   * -- Inspired in both design and implementation by the Horde memcache handler --
  22   *
  23   * @package        Joomla.Framework
  24   * @subpackage    Session
  25   * @since        1.5
  26   * @see http://www.php.net/manual/en/function.session-set-save-handler.php
  27   */
  28  class JSessionStorageMemcache extends JSessionStorage
  29  {
  30      /**
  31       * Resource for the current memcached connection.
  32       *
  33       * @var resource
  34       */
  35      var $_db;
  36  
  37      /**
  38       * Use compression?
  39       *
  40       * @var int
  41       */
  42      var $_compress = null;
  43  
  44      /**
  45       * Use persistent connections
  46       *
  47       * @var boolean
  48       */
  49      var $_persistent = false;
  50  
  51      /**
  52      * Constructor
  53      *
  54      * @access protected
  55      * @param array $options optional parameters
  56      */
  57  	function __construct( $options = array() )
  58      {
  59          if (!$this->test()) {
  60              return JError::raiseError(404, "The memcache extension isn't available");
  61          }
  62  
  63          parent::__construct($options);
  64  
  65          $config =& JFactory::getConfig();
  66          $params = $config->getValue('config.memcache_settings');
  67          if (!is_array($params))
  68          {
  69              $params = unserialize(stripslashes($params));
  70          }
  71  
  72          if (!$params)
  73          {
  74              $params = array();
  75          }
  76  
  77          $this->_compress    = (isset($params['compression'])) ? $params['compression'] : 0;
  78          $this->_persistent    = (isset($params['persistent'])) ? $params['persistent'] : false;
  79  
  80          // This will be an array of loveliness
  81          $this->_servers    = (isset($params['servers'])) ? $params['servers'] : array();
  82      }
  83  
  84      /**
  85       * Open the SessionHandler backend.
  86       *
  87       * @access public
  88       * @param string $save_path     The path to the session object.
  89       * @param string $session_name  The name of the session.
  90       * @return boolean  True on success, false otherwise.
  91       */
  92  	function open($save_path, $session_name)
  93      {
  94          $this->_db = new Memcache;
  95          for ($i=0, $n=count($this->_servers); $i < $n; $i++)
  96          {
  97              $server = $this->_servers[$i];
  98              $this->_db->addServer($server['host'], $server['port'], $this->_persistent);
  99          }
 100          return true;
 101      }
 102  
 103      /**
 104       * Close the SessionHandler backend.
 105       *
 106       * @access public
 107       * @return boolean  True on success, false otherwise.
 108       */
 109  	function close()
 110      {
 111          return $this->_db->close();
 112      }
 113  
 114       /**
 115        * Read the data for a particular session identifier from the
 116        * SessionHandler backend.
 117        *
 118        * @access public
 119        * @param string $id  The session identifier.
 120        * @return string  The session data.
 121        */
 122  	function read($id)
 123      {
 124          $sess_id = 'sess_'.$id;
 125          $this->_setExpire($sess_id);
 126          return $this->_db->get($sess_id);
 127      }
 128  
 129      /**
 130       * Write session data to the SessionHandler backend.
 131       *
 132       * @access public
 133       * @param string $id            The session identifier.
 134       * @param string $session_data  The session data.
 135       * @return boolean  True on success, false otherwise.
 136       */
 137  	function write($id, $session_data)
 138      {
 139          $sess_id = 'sess_'.$id;
 140          if ($this->_db->get($sess_id.'_expire')) {
 141              $this->_db->replace($sess_id.'_expire', time(), 0);
 142          } else {
 143              $this->_db->set($sess_id.'_expire', time(), 0);
 144          }
 145          if ($this->_db->get($sess_id)) {
 146              $this->_db->replace($sess_id, $session_data, $this->_compress);
 147          } else {
 148              $this->_db->set($sess_id, $session_data, $this->_compress);
 149          }
 150          return;
 151      }
 152  
 153      /**
 154        * Destroy the data for a particular session identifier in the
 155        * SessionHandler backend.
 156        *
 157        * @access public
 158        * @param string $id  The session identifier.
 159        * @return boolean  True on success, false otherwise.
 160        */
 161  	function destroy($id)
 162      {
 163          $sess_id = 'sess_'.$id;
 164          $this->_db->delete($sess_id.'_expire');
 165          return $this->_db->delete($sess_id);
 166      }
 167  
 168      /**
 169       * Garbage collect stale sessions from the SessionHandler backend.
 170       *
 171       *    -- Not Applicable in memcache --
 172       *
 173       * @access public
 174       * @param integer $maxlifetime  The maximum age of a session.
 175       * @return boolean  True on success, false otherwise.
 176       */
 177      function gc($maxlifetime)
 178      {
 179          return true;
 180      }
 181  
 182      /**
 183       * Test to see if the SessionHandler is available.
 184       *
 185       * @static
 186       * @access public
 187       * @return boolean  True on success, false otherwise.
 188       */
 189  	function test()
 190      {
 191          return (extension_loaded('memcache') && class_exists('Memcache'));
 192      }
 193  
 194      /**
 195       * Set expire time on each call since memcache sets it on cache creation.
 196       *
 197       * @access private
 198       *
 199       * @param string  $key   Cache key to expire.
 200       * @param integer $lifetime  Lifetime of the data in seconds.
 201       */
 202  	function _setExpire($key)
 203      {
 204          $lifetime    = ini_get("session.gc_maxlifetime");
 205          $expire        = $this->_db->get($key.'_expire');
 206  
 207          // set prune period
 208          if ($expire + $lifetime < time()) {
 209              $this->_db->delete($key);
 210              $this->_db->delete($key.'_expire');
 211          } else {
 212              $this->_db->replace($key.'_expire', time());
 213          }
 214      }
 215  }


Generato il: Mon Oct 19 20:29:27 2009 Generato con PHPXref 0.7