Open Journal Systems  3.3.0
RedisSessionHandler.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
14 use Predis\Response\ErrorInterface;
15 use Symfony\Component\Cache\Traits\RedisClusterProxy;
16 use Symfony\Component\Cache\Traits\RedisProxy;
17 
25 {
26  private $redis;
27 
31  private $prefix;
32 
36  private $ttl;
37 
47  public function __construct($redis, array $options = [])
48  {
49  if (
50  !$redis instanceof \Redis &&
51  !$redis instanceof \RedisArray &&
52  !$redis instanceof \RedisCluster &&
53  !$redis instanceof \Predis\ClientInterface &&
54  !$redis instanceof RedisProxy &&
55  !$redis instanceof RedisClusterProxy
56  ) {
57  throw new \InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
58  }
59 
60  if ($diff = array_diff(array_keys($options), ['prefix', 'ttl'])) {
61  throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
62  }
63 
64  $this->redis = $redis;
65  $this->prefix = $options['prefix'] ?? 'sf_s';
66  $this->ttl = $options['ttl'] ?? null;
67  }
68 
72  protected function doRead($sessionId): string
73  {
74  return $this->redis->get($this->prefix.$sessionId) ?: '';
75  }
76 
80  protected function doWrite($sessionId, $data): bool
81  {
82  $result = $this->redis->setEx($this->prefix.$sessionId, (int) ($this->ttl ?? ini_get('session.gc_maxlifetime')), $data);
83 
84  return $result && !$result instanceof ErrorInterface;
85  }
86 
90  protected function doDestroy($sessionId): bool
91  {
92  $this->redis->del($this->prefix.$sessionId);
93 
94  return true;
95  }
96 
100  public function close(): bool
101  {
102  return true;
103  }
104 
108  public function gc($maxlifetime): bool
109  {
110  return true;
111  }
112 
116  public function updateTimestamp($sessionId, $data)
117  {
118  return (bool) $this->redis->expire($this->prefix.$sessionId, (int) ($this->ttl ?? ini_get('session.gc_maxlifetime')));
119  }
120 }
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler\doDestroy
doDestroy($sessionId)
Definition: RedisSessionHandler.php:96
Symfony\Component\HttpFoundation\Session\Storage\Handler
Definition: lib/vendor/symfony/http-foundation/Session/Storage/Handler/MemcachedSessionHandler.php:12
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler\doRead
doRead($sessionId)
Definition: RedisSessionHandler.php:78
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler\__construct
__construct($redis, array $options=[])
Definition: RedisSessionHandler.php:53
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler\gc
gc($maxlifetime)
Definition: RedisSessionHandler.php:114
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
Definition: RedisSessionHandler.php:24
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler\updateTimestamp
updateTimestamp($sessionId, $data)
Definition: RedisSessionHandler.php:122
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler\close
close()
Definition: RedisSessionHandler.php:106
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler\doWrite
doWrite($sessionId, $data)
Definition: RedisSessionHandler.php:86
Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler
Definition: AbstractSessionHandler.php:23