[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/SessionHandler/ -> memcache.php (source)

   1  <?php
   2  /**
   3   * SessionHandler:: implementation for memcache.
   4   * memcache website: http://www.danga.com/memcached/
   5   *
   6   * Required parameters:<pre>
   7   *   'hostspec'  The hostname of the memcache server.
   8   *   'port'      The port on which to connect to the memcache server.</pre>
   9   *
  10   * Optional parameters:<pre>
  11   *   'persistent'  Use persistent DB connections? (boolean)
  12   *   'compression' Use compression when storing sessions.</pre>
  13   *
  14   * $Horde: framework/SessionHandler/SessionHandler/memcache.php,v 1.1.2.1 2006/05/02 17:27:49 chuck Exp $
  15   *
  16   * Copyright 2005-2006 Rong-En Fan <rafan@infor.org>
  17   *
  18   * See the enclosed file COPYING for license information (LGPL). If you
  19   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  20   *
  21   * @author  Rong-En Fan <rafan@infor.org>
  22   * @since   Horde 3.1
  23   * @package Horde_SessionHandler
  24   */
  25  class SessionHandler_memcache extends SessionHandler {
  26  
  27      /**
  28       * Current memcache connection.
  29       *
  30       * @var object
  31       */
  32      var $_db;
  33  
  34      /**
  35       * Boolean indicating whether or not we're connected to the
  36       * memcache server.
  37       *
  38       * @var boolean
  39       */
  40      var $_connected = false;
  41  
  42      /**
  43       * Lock handle.
  44       *
  45       * @var resource
  46       */
  47      var $_fp;
  48  
  49      /**
  50       * Close the SessionHandler backend.
  51       *
  52       * @return boolean  True on success, false otherwise.
  53       */
  54      function close()
  55      {
  56          if ($this->_connected) {
  57              $this->_connected = false;
  58              @memcache_close($this->_db);
  59          }
  60  
  61          $this->_unlockSession();
  62          return true;
  63      }
  64  
  65      /**
  66       * Read the data for a particular session identifier from the
  67       * SessionHandler backend.
  68       *
  69       * @param string $id  The session identifier.
  70       *
  71       * @return string  The session data.
  72       */
  73      function read($id)
  74      {
  75          /* Obtain session lock. */
  76          $this->_lockSession($id);
  77  
  78          /* Make sure we have a valid memcache connection. */
  79          $this->_connect();
  80  
  81          $result = @memcache_get($this->_db, $id);
  82          if (!$result) {
  83              Horde::logMessage('Error retrieving session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
  84              $this->_unlockSession();
  85              @unlink($this->_params['lock_dir'] . '/lock_' . $id);
  86              return false;
  87          }
  88  
  89          Horde::logMessage('Read session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_DEBUG);
  90          return $result;
  91      }
  92  
  93      /**
  94       * Write session data to the SessionHandler backend.
  95       *
  96       * @param string $id            The session identifier.
  97       * @param string $session_data  The session data.
  98       *
  99       * @return boolean  True on success, false otherwise.
 100       */
 101      function write($id, $session_data)
 102      {
 103          /* Make sure we have a valid memcache connection. */
 104          $this->_connect();
 105  
 106          $lifetime = ini_get('session.gc_maxlifetime');
 107          $flags = $this->_params['compression'] ? MEMCACHE_COMPRESSED : 0;
 108          $result = @memcache_set($this->_db, $id, $session_data, $flags, $lifetime);
 109  
 110          if (!$result) {
 111              Horde::logMessage('Error writing session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
 112              return false;
 113          }
 114          Horde::logMessage('Wrote session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_DEBUG);
 115  
 116          return true;
 117      }
 118  
 119      /**
 120       * Destroy the data for a particular session identifier in the
 121       * SessionHandler backend.
 122       *
 123       * @param string $id  The session identifier.
 124       *
 125       * @return boolean  True on success, false otherwise.
 126       */
 127      function destroy($id)
 128      {
 129          /* Make sure we have a valid memcache connection. */
 130          $this->_connect();
 131  
 132          $result = @memcache_delete($this->_db, $id);
 133  
 134          if (!$result) {
 135              Horde::logMessage('Failed to delete session (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
 136              return false;
 137          }
 138          Horde::logMessage('Deleted session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_DEBUG);
 139  
 140          $this->_unlockSession();
 141          @unlink($this->_params['lock_dir'] . '/lock_' . $id);
 142          return true;
 143      }
 144  
 145      /**
 146       * Garbage collect stale sessions from the SessionHandler backend.
 147       *
 148       * @param integer $maxlifetime  The maximum age of a session.
 149       *
 150       * @return boolean  True on success, false otherwise.
 151       */
 152      function gc($maxlifetime = 300)
 153      {
 154          return true;
 155      }
 156  
 157      /**
 158       * Attempts to open a connection to the memcache server(s).
 159       *
 160       * @access private
 161       */
 162      function _connect()
 163      {
 164          if ($this->_connected) {
 165              Horde::logMessage('Already connected to a memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_DEBUG);
 166              return;
 167          }
 168  
 169          Horde::assertDriverConfig($this->_params, 'sessionhandler',
 170                                    array('hostspec', 'port'),
 171                                    'session handler memcache');
 172  
 173          if (empty($this->_params['persistent'])) {
 174              $connect_persistent = false;
 175          } else {
 176              $connect_persistent = true;
 177          }
 178          $con = new Memcache;
 179  
 180          for ($i = 0, $n = count($this->_params['hostspec']); $i < $n; ++$i) {
 181              if (!@$con->addServer($this->_params['hostspec'][$i],
 182                                    $this->_params['port'][$i],
 183                                    $connect_persistent)) {
 184                  Horde::logMessage('Could not add [' . $this->_params['hostspec'][$i] . ':' . $this->_params['port'][$i] . '] as memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_ERR);
 185              } else {
 186                  Horde::logMessage('Added [' . $this->_params['hostspec'][$i] . ':' . $this->_params['port'][$i] . '] as memcache server for memcache SessionHandler', __FILE__, __LINE__, PEAR_LOG_DEBUG);
 187                  $this->_connected = true;
 188              }
 189          }
 190  
 191          /* Check if any of the pooled connections works. */
 192          if ($con->getVersion()) {
 193              $this->_db = $con;
 194              $this->_connected = true;
 195              Horde::logMessage('Connected to a memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_DEBUG);
 196          } else {
 197              $this->_connected = false;
 198              Horde::logMessage('Could not connect to any memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_ERR);
 199          }
 200      }
 201  
 202      /**
 203       * @access private
 204       */
 205      function _lockSession($id)
 206      {
 207          $this->_fp = @fopen($this->_params['lock_dir'] . '/lock_' . $id, 'w+');
 208          if (!$this->_fp) {
 209              Horde::logMessage('Could not open session lock for ' . $id, __FILE__, __LINE__, PEAR_LOG_ERR);
 210              return false;
 211          }
 212          if (!@flock($this->_fp, LOCK_EX)) {
 213              Horde::logMessage('Could not lock session ' . $id, __FILE__, __LINE__, PEAR_LOG_ERR);
 214              return false;
 215          }
 216  
 217          return true;
 218      }
 219  
 220      /**
 221       * @access private
 222       */
 223      function _unlockSession()
 224      {
 225          if (is_resource($this->_fp)) {
 226              if (!@flock($this->_fp, LOCK_UN)) {
 227                  Horde::logMessage('Could not unlock session', __FILE__, __LINE__, PEAR_LOG_ERR);
 228              }
 229              @fclose($this->_fp);
 230          }
 231      }
 232  
 233  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7