[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/session/src/Handler/ -> MemcachedHandler.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Session Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Session\Handler;
  10  
  11  use Joomla\Session\HandlerInterface;
  12  
  13  /**
  14   * Memcached session storage handler
  15   *
  16   * @since  2.0.0
  17   */
  18  class MemcachedHandler implements HandlerInterface
  19  {
  20      /**
  21       * Memcached driver
  22       *
  23       * @var    \Memcached
  24       * @since  2.0.0
  25       */
  26      private $memcached;
  27  
  28      /**
  29       * Session ID prefix to avoid naming conflicts
  30       *
  31       * @var    string
  32       * @since  2.0.0
  33       */
  34      private $prefix;
  35  
  36      /**
  37       * Time to live in seconds
  38       *
  39       * @var    integer
  40       * @since  2.0.0
  41       */
  42      private $ttl;
  43  
  44      /**
  45       * Constructor
  46       *
  47       * @param   \Memcached  $memcached  A Memcached instance
  48       * @param   array       $options    Associative array of options to configure the handler
  49       *
  50       * @since   2.0.0
  51       */
  52  	public function __construct(\Memcached $memcached, array $options = [])
  53      {
  54          $this->memcached = $memcached;
  55  
  56          // Set the default time-to-live based on the Session object's default configuration
  57          $this->ttl = isset($options['ttl']) ? (int) $options['ttl'] : 900;
  58  
  59          // Namespace our session IDs to avoid potential conflicts
  60          $this->prefix = $options['prefix'] ?? 'jfw';
  61      }
  62  
  63      /**
  64       * Close the session
  65       *
  66       * @return  boolean  True on success, false otherwise
  67       *
  68       * @since   2.0.0
  69       */
  70      #[\ReturnTypeWillChange]
  71  	public function close()
  72      {
  73          return true;
  74      }
  75  
  76      /**
  77       * Destroy a session
  78       *
  79       * @param   integer  $session_id  The session ID being destroyed
  80       *
  81       * @return  boolean  True on success, false otherwise
  82       *
  83       * @since   2.0.0
  84       */
  85  	public function destroy($session_id): bool
  86      {
  87          return $this->memcached->delete($this->prefix . $session_id);
  88      }
  89  
  90      /**
  91       * Cleanup old sessions
  92       *
  93       * @param   integer  $maxlifetime  Sessions that have not updated for the last maxlifetime seconds will be removed
  94       *
  95       * @return  boolean  True on success, false otherwise
  96       *
  97       * @since   2.0.0
  98       */
  99      #[\ReturnTypeWillChange]
 100      public function gc($maxlifetime)
 101      {
 102          // Memcached manages garbage collection on its own
 103          return true;
 104      }
 105  
 106      /**
 107       * Test to see if the HandlerInterface is available
 108       *
 109       * @return  boolean  True on success, false otherwise
 110       *
 111       * @since   2.0.0
 112       */
 113  	public static function isSupported(): bool
 114      {
 115          /*
 116           * GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded.
 117           * If the class is there, we can assume it works.
 118           */
 119          return class_exists('Memcached');
 120      }
 121  
 122      /**
 123       * Initialize session
 124       *
 125       * @param   string  $save_path   The path where to store/retrieve the session
 126       * @param   string  $session_id  The session id
 127       *
 128       * @return  boolean  True on success, false otherwise
 129       *
 130       * @since   2.0.0
 131       */
 132      #[\ReturnTypeWillChange]
 133  	public function open($save_path, $session_id)
 134      {
 135          return true;
 136      }
 137  
 138      /**
 139       * Read session data
 140       *
 141       * @param   string  $session_id  The session id to read data for
 142       *
 143       * @return  string  The session data
 144       *
 145       * @since   2.0.0
 146       */
 147      #[\ReturnTypeWillChange]
 148  	public function read($session_id)
 149      {
 150          return $this->memcached->get($this->prefix . $session_id) ?: '';
 151      }
 152  
 153      /**
 154       * Write session data
 155       *
 156       * @param   string  $session_id    The session id
 157       * @param   string  $session_data  The encoded session data
 158       *
 159       * @return  boolean  True on success, false otherwise
 160       *
 161       * @since   2.0.0
 162       */
 163      #[\ReturnTypeWillChange]
 164  	public function write($session_id, $session_data)
 165      {
 166          return $this->memcached->set($this->prefix . $session_id, $session_data, time() + $this->ttl);
 167      }
 168  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer