[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/session/src/Handler/ -> RedisHandler.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   * Redis session storage handler
  15   *
  16   * @since  2.0.0
  17   */
  18  class RedisHandler implements HandlerInterface
  19  {
  20      /**
  21       * Session ID prefix to avoid naming conflicts
  22       *
  23       * @var    string
  24       * @since  2.0.0
  25       */
  26      private $prefix;
  27  
  28      /**
  29       * Redis driver
  30       *
  31       * @var    \Redis
  32       * @since  2.0.0
  33       */
  34      private $redis;
  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   \Redis  $redis    A Redis instance
  48       * @param   array   $options  Associative array of options to configure the handler
  49       *
  50       * @since   2.0.0
  51       */
  52  	public function __construct(\Redis $redis, array $options = [])
  53      {
  54          $this->redis = $redis;
  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          // No need to close the connection to Redis server manually.
  74          return true;
  75      }
  76  
  77      /**
  78       * Destroy a session, called automatically when running session_regenerate_id().
  79       *
  80       * @param   integer  $session_id  The session ID being destroyed
  81       *
  82       * @return  boolean  True on success, false otherwise
  83       *
  84       * @since   2.0.0
  85       */
  86  	public function destroy($session_id): bool
  87      {
  88          $this->redis->del($this->prefix . $session_id);
  89  
  90          // Session callback must have a return value of type bool when session_regenerate_id() is called.
  91          return true;
  92      }
  93  
  94      /**
  95       * Cleanup old sessions
  96       *
  97       * @param   integer  $maxlifetime  Sessions that have not updated for the last maxlifetime seconds will be removed
  98       *
  99       * @return  boolean  True on success, false otherwise
 100       *
 101       * @since   2.0.0
 102       */
 103      #[\ReturnTypeWillChange]
 104      public function gc($maxlifetime)
 105      {
 106          return true;
 107      }
 108  
 109      /**
 110       * Test to see if the HandlerInterface is available
 111       *
 112       * @return  boolean  True on success, false otherwise
 113       *
 114       * @since   2.0.0
 115       */
 116  	public static function isSupported(): bool
 117      {
 118          return \extension_loaded('redis') && class_exists('Redis');
 119      }
 120  
 121      /**
 122       * Initialize session
 123       *
 124       * @param   string  $save_path   The path where to store/retrieve the session
 125       * @param   string  $session_id  The session id
 126       *
 127       * @return  boolean  True on success, false otherwise
 128       *
 129       * @since   2.0.0
 130       */
 131      #[\ReturnTypeWillChange]
 132  	public function open($save_path, $session_id)
 133      {
 134          return true;
 135      }
 136  
 137      /**
 138       * Read session data
 139       *
 140       * @param   string  $session_id  The session id to read data for
 141       *
 142       * @return  string  The session data
 143       *
 144       * @since   2.0.0
 145       */
 146      #[\ReturnTypeWillChange]
 147  	public function read($session_id)
 148      {
 149          return $this->redis->get($this->prefix . $session_id) ?: '';
 150      }
 151  
 152      /**
 153       * Write session data
 154       *
 155       * @param   string  $session_id    The session id
 156       * @param   string  $session_data  The encoded session data
 157       *
 158       * @return  boolean  True on success, false otherwise
 159       *
 160       * @since   2.0.0
 161       */
 162      #[\ReturnTypeWillChange]
 163  	public function write($session_id, $session_data)
 164      {
 165          if ($this->ttl > 0)
 166          {
 167              return $this->redis->setex($this->prefix . $session_id, $this->ttl, $session_data);
 168          }
 169  
 170          return $this->redis->set($this->prefix . $session_id, $session_data);
 171      }
 172  }


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