vendor/symfony/http-foundation/Session/Storage/Handler/RedisSessionHandler.php line 74

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  11. use Predis\Response\ErrorInterface;
  12. use Symfony\Component\Cache\Traits\RedisClusterProxy;
  13. use Symfony\Component\Cache\Traits\RedisProxy;
  14. /**
  15.  * Redis based session storage handler based on the Redis class
  16.  * provided by the PHP redis extension.
  17.  *
  18.  * @author Dalibor Karlović <dalibor@flexolabs.io>
  19.  */
  20. class RedisSessionHandler extends AbstractSessionHandler
  21. {
  22.     private $redis;
  23.     /**
  24.      * @var string Key prefix for shared environments
  25.      */
  26.     private $prefix;
  27.     /**
  28.      * @var int Time to live in seconds
  29.      */
  30.     private $ttl;
  31.     /**
  32.      * List of available options:
  33.      *  * prefix: The prefix to use for the keys in order to avoid collision on the Redis server
  34.      *  * ttl: The time to live in seconds.
  35.      *
  36.      * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis
  37.      *
  38.      * @throws \InvalidArgumentException When unsupported client or options are passed
  39.      */
  40.     public function __construct($redis, array $options = [])
  41.     {
  42.         if (
  43.             !$redis instanceof \Redis &&
  44.             !$redis instanceof \RedisArray &&
  45.             !$redis instanceof \RedisCluster &&
  46.             !$redis instanceof \Predis\ClientInterface &&
  47.             !$redis instanceof RedisProxy &&
  48.             !$redis instanceof RedisClusterProxy
  49.         ) {
  50.             throw new \InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.'__METHOD__get_debug_type($redis)));
  51.         }
  52.         if ($diff array_diff(array_keys($options), ['prefix''ttl'])) {
  53.             throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".'implode(', '$diff)));
  54.         }
  55.         $this->redis $redis;
  56.         $this->prefix $options['prefix'] ?? 'sf_s';
  57.         $this->ttl $options['ttl'] ?? null;
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     protected function doRead(string $sessionId): string
  63.     {
  64.         return $this->redis->get($this->prefix.$sessionId) ?: '';
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     protected function doWrite(string $sessionIdstring $data): bool
  70.     {
  71.         $result $this->redis->setEx($this->prefix.$sessionId, (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')), $data);
  72.         return $result && !$result instanceof ErrorInterface;
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     protected function doDestroy(string $sessionId): bool
  78.     {
  79.         static $unlink true;
  80.         if ($unlink) {
  81.             try {
  82.                 $unlink false !== $this->redis->unlink($this->prefix.$sessionId);
  83.             } catch (\Throwable $e) {
  84.                 $unlink false;
  85.             }
  86.         }
  87.         if (!$unlink) {
  88.             $this->redis->del($this->prefix.$sessionId);
  89.         }
  90.         return true;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     #[\ReturnTypeWillChange]
  96.     public function close(): bool
  97.     {
  98.         return true;
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      *
  103.      * @return int|false
  104.      */
  105.     #[\ReturnTypeWillChange]
  106.     public function gc($maxlifetime)
  107.     {
  108.         return 0;
  109.     }
  110.     /**
  111.      * @return bool
  112.      */
  113.     #[\ReturnTypeWillChange]
  114.     public function updateTimestamp($sessionId$data)
  115.     {
  116.         return (bool) $this->redis->expire($this->prefix.$sessionId, (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
  117.     }
  118. }