| 148 |
lars |
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 |
|
|
|
12 |
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
|
|
13 |
|
|
|
14 |
use Predis\Response\ErrorInterface;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Redis based session storage handler based on the Redis class
|
|
|
18 |
* provided by the PHP redis extension.
|
|
|
19 |
*
|
|
|
20 |
* @author Dalibor Karlović <dalibor@flexolabs.io>
|
|
|
21 |
*/
|
|
|
22 |
class RedisSessionHandler extends AbstractSessionHandler
|
|
|
23 |
{
|
|
|
24 |
/**
|
|
|
25 |
* Key prefix for shared environments.
|
|
|
26 |
*/
|
|
|
27 |
private string $prefix;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Time to live in seconds.
|
|
|
31 |
*/
|
|
|
32 |
private int|\Closure|null $ttl;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* List of available options:
|
|
|
36 |
* * prefix: The prefix to use for the keys in order to avoid collision on the Redis server
|
|
|
37 |
* * ttl: The time to live in seconds.
|
|
|
38 |
*
|
|
|
39 |
* @throws \InvalidArgumentException When unsupported client or options are passed
|
|
|
40 |
*/
|
|
|
41 |
public function __construct(
|
|
|
42 |
private \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis,
|
|
|
43 |
array $options = [],
|
|
|
44 |
) {
|
|
|
45 |
if ($diff = array_diff(array_keys($options), ['prefix', 'ttl'])) {
|
|
|
46 |
throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$this->prefix = $options['prefix'] ?? 'sf_s';
|
|
|
50 |
$this->ttl = $options['ttl'] ?? null;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
protected function doRead(string $sessionId): string
|
|
|
54 |
{
|
|
|
55 |
return $this->redis->get($this->prefix.$sessionId) ?: '';
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
protected function doWrite(string $sessionId, string $data): bool
|
|
|
59 |
{
|
|
|
60 |
$ttl = ($this->ttl instanceof \Closure ? ($this->ttl)() : $this->ttl) ?? \ini_get('session.gc_maxlifetime');
|
|
|
61 |
$result = $this->redis->setEx($this->prefix.$sessionId, (int) $ttl, $data);
|
|
|
62 |
|
|
|
63 |
return $result && !$result instanceof ErrorInterface;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
protected function doDestroy(string $sessionId): bool
|
|
|
67 |
{
|
|
|
68 |
static $unlink = true;
|
|
|
69 |
|
|
|
70 |
if ($unlink) {
|
|
|
71 |
try {
|
|
|
72 |
$unlink = false !== $this->redis->unlink($this->prefix.$sessionId);
|
|
|
73 |
} catch (\Throwable) {
|
|
|
74 |
$unlink = false;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if (!$unlink) {
|
|
|
79 |
$this->redis->del($this->prefix.$sessionId);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return true;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
#[\ReturnTypeWillChange]
|
|
|
86 |
public function close(): bool
|
|
|
87 |
{
|
|
|
88 |
return true;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function gc(int $maxlifetime): int|false
|
|
|
92 |
{
|
|
|
93 |
return 0;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public function updateTimestamp(string $sessionId, string $data): bool
|
|
|
97 |
{
|
|
|
98 |
$ttl = ($this->ttl instanceof \Closure ? ($this->ttl)() : $this->ttl) ?? \ini_get('session.gc_maxlifetime');
|
|
|
99 |
|
|
|
100 |
return $this->redis->expire($this->prefix.$sessionId, (int) $ttl);
|
|
|
101 |
}
|
|
|
102 |
}
|