Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/Session/Storage/Handler/MemcachedSessionHandler.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 /**
15 * Memcached based session storage handler based on the Memcached class
16 * provided by the PHP memcached extension.
17 *
18 * @see https://php.net/memcached
19 *
20 * @author Drak <[email protected]>
21 */
22 class MemcachedSessionHandler extends AbstractSessionHandler
23 {
24 /**
25 * Time to live in seconds.
26 */
27 private int|\Closure|null $ttl;
28
29 /**
30 * Key prefix for shared environments.
31 */
32 private string $prefix;
33
34 /**
35 * Constructor.
36 *
37 * List of available options:
38 * * prefix: The prefix to use for the memcached keys in order to avoid collision
39 * * ttl: The time to live in seconds.
40 *
41 * @throws \InvalidArgumentException When unsupported options are passed
42 */
43 public function __construct(
44 private \Memcached $memcached,
45 array $options = [],
46 ) {
47 if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime', 'ttl'])) {
48 throw new \InvalidArgumentException(\sprintf('The following options are not supported "%s".', implode(', ', $diff)));
49 }
50
51 $this->ttl = $options['expiretime'] ?? $options['ttl'] ?? null;
52 $this->prefix = $options['prefix'] ?? 'sf2s';
53 }
54
55 public function close(): bool
56 {
57 return $this->memcached->quit();
58 }
59
60 protected function doRead(#[\SensitiveParameter] string $sessionId): string
61 {
62 return $this->memcached->get($this->prefix.$sessionId) ?: '';
63 }
64
65 public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string $data): bool
66 {
67 $this->memcached->touch($this->prefix.$sessionId, $this->getCompatibleTtl());
68
69 return true;
70 }
71
72 protected function doWrite(#[\SensitiveParameter] string $sessionId, string $data): bool
73 {
74 return $this->memcached->set($this->prefix.$sessionId, $data, $this->getCompatibleTtl());
75 }
76
77 private function getCompatibleTtl(): int
78 {
79 $ttl = ($this->ttl instanceof \Closure ? ($this->ttl)() : $this->ttl) ?? \ini_get('session.gc_maxlifetime');
80
81 // If the relative TTL that is used exceeds 30 days, memcached will treat the value as Unix time.
82 // We have to convert it to an absolute Unix time at this point, to make sure the TTL is correct.
83 if ($ttl > 60 * 60 * 24 * 30) {
84 $ttl += time();
85 }
86
87 return $ttl;
88 }
89
90 protected function doDestroy(#[\SensitiveParameter] string $sessionId): bool
91 {
92 $result = $this->memcached->delete($this->prefix.$sessionId);
93
94 return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
95 }
96
97 public function gc(int $maxlifetime): int|false
98 {
99 // not required here because memcached will auto expire the records anyhow.
100 return 0;
101 }
102
103 /**
104 * Return a Memcached instance.
105 */
106 protected function getMemcached(): \Memcached
107 {
108 return $this->memcached;
109 }
110 }