/libraries/joomla/session/storage/memcache.php

https://github.com/pacoqueen/callao_chico · PHP · 210 lines · 88 code · 19 blank · 103 comment · 11 complexity · e48506fadd27b43f575f10a23012cc5a MD5 · raw file

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