-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMemcachedHandler.php
More file actions
301 lines (285 loc) · 8.96 KB
/
Copy pathMemcachedHandler.php
File metadata and controls
301 lines (285 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php declare(strict_types=1);
/*
* This file is part of Aplus Framework Session Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Framework\Session\SaveHandlers;
use Framework\Log\LogLevel;
use Framework\Session\SaveHandler;
use Memcached;
use OutOfBoundsException;
use SensitiveParameter;
/**
* Class MemcachedHandler.
*
* @package session
*/
class MemcachedHandler extends SaveHandler
{
protected ?Memcached $memcached;
/**
* Prepare configurations to be used by the MemcachedHandler.
*
* @param array<string,mixed> $config Custom configs
*
* The custom configs are:
*
* ```php
* $configs = [
* // A custom prefix prepended in the keys
* 'prefix' => '',
* // A list of Memcached servers
* 'servers' => [
* [
* 'host' => '127.0.0.1', // host always is required
* 'port' => 11211, // port is optional, default to 11211
* 'weight' => 0, // weight is optional, default to 0
* ],
* ],
* // An associative array of Memcached::OPT_* constants
* 'options' => [
* Memcached::OPT_BINARY_PROTOCOL => true,
* ],
* // Maximum attempts to try lock a session id
* 'lock_attempts' => 60,
* // Interval between the lock attempts in microseconds
* 'lock_sleep' => 1_000_000,
* // TTL to the lock (valid for the current session only)
* 'lock_ttl' => 600,
* // The maxlifetime (TTL) used for cache item expiration
* 'maxlifetime' => null, // Null to use the ini value of session.gc_maxlifetime
* // Match IP?
* 'match_ip' => false,
* // Match User-Agent?
* 'match_ua' => false,
* ];
* ```
*/
protected function prepareConfig(#[SensitiveParameter] array $config) : void
{
$this->config = \array_replace_recursive([
'prefix' => '',
'servers' => [
[
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 0,
],
],
'options' => [
Memcached::OPT_BINARY_PROTOCOL => true,
],
'lock_attempts' => 60,
'lock_sleep' => 1_000_000,
'lock_ttl' => 600,
'maxlifetime' => null,
'match_ip' => false,
'match_ua' => false,
], $config);
foreach ($this->config['servers'] as $index => $server) {
if (!isset($server['host'])) {
throw new OutOfBoundsException(
"Memcached host not set on server config '{$index}'"
);
}
}
}
public function setMemcached(Memcached $memcached) : static
{
$this->setByExternal = true;
$this->memcached = $memcached;
return $this;
}
public function getMemcached() : ?Memcached
{
return $this->memcached ?? null;
}
/**
* Get expiration as a timestamp.
*
* Useful for Time To Live greater than a month (`60*60*24*30`).
*
* @param int $seconds
*
* @see https://www.php.net/manual/en/memcached.expiration.php
*
* @return int
*/
protected function getExpiration(int $seconds) : int
{
return \time() + $seconds;
}
/**
* Get a key for Memcached, using the optional
* prefix, match IP and match User-Agent configs.
*
* NOTE: The max key length allowed by Memcached is 250 bytes.
*
* @param string $id The session id
*
* @return string The final key
*/
protected function getKey(string $id) : string
{
return $this->config['prefix'] . $id . $this->getKeySuffix();
}
public function open($path, $name) : bool
{
if (isset($this->memcached)) {
return true;
}
$this->memcached = new Memcached();
$pool = [];
foreach ($this->config['servers'] as $server) {
$host = $server['host'] . ':' . ($server['port'] ?? 11211);
if (\in_array($host, $pool, true)) {
$this->log(
'Session (memcached): Server pool already has ' . $host,
LogLevel::DEBUG
);
continue;
}
$result = $this->memcached->addServer(
$server['host'],
$server['port'] ?? 11211,
$server['weight'] ?? 0,
);
if ($result === false) {
$this->log("Session (memcached): Could not add {$host} to server pool");
continue;
}
$pool[] = $host;
}
$result = $this->memcached->setOptions($this->config['options']);
if ($result === false) {
$this->log('Session (memcached): ' . $this->memcached->getLastErrorMessage());
}
if (!$this->memcached->getStats()) {
$this->log('Session (memcached): Could not connect to any server');
return false;
}
return true;
}
public function read($id) : string
{
if (!isset($this->memcached) || !$this->lock($id)) {
return '';
}
if (!isset($this->sessionId)) {
$this->sessionId = $id;
}
$data = (string) $this->memcached->get($this->getKey($id));
$this->setFingerprint($data);
return $data;
}
public function write($id, $data) : bool
{
if (!isset($this->memcached)) {
return false;
}
if ($id !== $this->sessionId) {
if (!$this->unlock() || !$this->lock($id)) {
return false;
}
$this->setFingerprint('');
$this->sessionId = $id;
}
if ($this->lockId === false) {
return false;
}
$this->memcached->replace(
$this->lockId,
\time(),
$this->getExpiration($this->config['lock_ttl'])
);
$maxlifetime = $this->getExpiration($this->getMaxlifetime());
if ($this->hasSameFingerprint($data)) {
return $this->memcached->touch($this->getKey($id), $maxlifetime);
}
if ($this->memcached->set($this->getKey($id), $data, $maxlifetime)) {
$this->setFingerprint($data);
return true;
}
return false;
}
public function updateTimestamp($id, $data) : bool
{
return $this->memcached->touch(
$this->getKey($id),
$this->getExpiration($this->getMaxlifetime())
);
}
public function close() : bool
{
if ($this->lockId) {
$this->memcached->delete($this->lockId);
}
if ($this->setByExternal === false) {
if (!$this->memcached->quit()) {
return false;
}
$this->memcached = null;
}
return true;
}
public function destroy($id) : bool
{
if (!$this->lockId) {
return false;
}
$destroyed = $this->memcached->delete($this->getKey($id));
return !($destroyed === false
&& $this->memcached->getResultCode() !== Memcached::RES_NOTFOUND);
}
public function gc($max_lifetime) : false | int
{
return 0;
}
protected function lock(string $id) : bool
{
$expiration = $this->getExpiration($this->config['lock_ttl']);
if ($this->lockId && $this->memcached->get($this->lockId)) {
return $this->memcached->replace($this->lockId, \time(), $expiration);
}
$lockId = $this->getKey($id) . ':lock';
$attempt = 0;
while ($attempt < $this->config['lock_attempts']) {
$attempt++;
if ($this->memcached->get($lockId)) {
\usleep($this->config['lock_sleep']);
continue;
}
if (!$this->memcached->set($lockId, \time(), $expiration)) {
$this->log('Session (memcached): Error while trying to lock ' . $lockId);
return false;
}
$this->lockId = $lockId;
break;
}
if ($attempt === $this->config['lock_attempts']) {
$this->log(
"Session (memcached): Unable to lock {$lockId} after {$attempt} attempts"
);
return false;
}
return true;
}
protected function unlock() : bool
{
if ($this->lockId === false) {
return true;
}
if (!$this->memcached->delete($this->lockId) &&
$this->memcached->getResultCode() !== Memcached::RES_NOTFOUND
) {
$this->log('Session (memcached): Error while trying to unlock ' . $this->lockId);
return false;
}
$this->lockId = false;
return true;
}
}