-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathRedisBackend.php
618 lines (548 loc) · 20.4 KB
/
RedisBackend.php
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<?php
declare(strict_types=1);
namespace Neos\Cache\Backend;
/*
* This file is part of the Neos.Cache package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Cache\Backend\AbstractBackend as IndependentAbstractBackend;
use Neos\Cache\EnvironmentConfiguration;
use Neos\Cache\Exception as CacheException;
use Neos\Error\Messages\Error;
use Neos\Error\Messages\Notice;
use Neos\Error\Messages\Result;
/**
* A caching backend which stores cache entries in Redis using the phpredis PHP extension.
* Redis is a noSQL database with very good scaling characteristics
* in proportion to the amount of entries and data size.
*
* @see http://redis.io/
* @see https://github.com/phpredis/phpredis
*
* Available backend options:
* - defaultLifetime: The default lifetime of a cache entry
* - hostname: The hostname (or socket filepath) of the redis server
* - port: The TCP port of the redis server (will be ignored if connecting to a socket)
* - database: The database index that will be used. By default,
* Redis has 16 databases with index number 0 - 15
* - password: The password needed for redis clients to connect to the server (hostname)
* - batchSize: Maximum number of parameters per query for batch operations
*
* Requirements:
* - Redis 6.0.0+
* - phpredis with Redis 6.0 support
*
* Implementation based on ext:rediscache by Christopher Hlubek - networkteam GmbH
*
* Each Redis key contains a prefix built from the cache identifier,
* so one single database can be used for different caches.
*
* Cache entry data is stored in a simple key. Tags are stored in Sets.
*
* @api
*/
class RedisBackend extends IndependentAbstractBackend implements TaggableBackendInterface, IterableBackendInterface, FreezableBackendInterface, PhpCapableBackendInterface, WithStatusInterface
{
use RequireOnceFromValueTrait;
public const MIN_REDIS_VERSION = '6.0.0';
/**
* @var \Redis
*/
protected $redis;
protected ?bool $frozen = null;
protected string $hostname = '127.0.0.1';
protected int $port = 6379;
protected int $database = 0;
protected string $password = '';
protected int $compressionLevel = 0;
/**
* Redis allows a maximum of 1024 * 1024 parameters, but we use a lower limit to prevent long blocking calls.
*/
protected int $batchSize = 100000;
/**
* @var \ArrayIterator|null
*/
private $entryIterator;
/**
* Constructs this backend
*
* @param EnvironmentConfiguration $environmentConfiguration
* @param array $options Configuration options - depends on the actual backend
* @throws CacheException
*/
public function __construct(EnvironmentConfiguration $environmentConfiguration, array $options)
{
parent::__construct($environmentConfiguration, $options);
if (!$this->redis instanceof \Redis) {
$this->redis = $this->getRedisClient();
}
}
/**
* Saves data in the cache.
*
* @param string $entryIdentifier An identifier for this specific cache entry
* @param string $data The data to be stored
* @param array $tags Tags to associate with this cache entry. If the backend does not support tags, this option can be ignored.
* @param integer|null $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
* @throws \RuntimeException
* @throws CacheException
* @api
*/
public function set(string $entryIdentifier, string $data, array $tags = [], int $lifetime = null): void
{
if ($this->isFrozen()) {
throw new \RuntimeException(sprintf('Cannot add or modify cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1323344192);
}
if ($lifetime === null) {
$lifetime = $this->defaultLifetime;
}
$setOptions = [];
if ($lifetime > 0) {
$setOptions['ex'] = $lifetime;
}
$redisTags = array_reduce($tags, function ($redisTags, $tag) use ($lifetime, $entryIdentifier) {
$expire = $this->calculateExpires($this->getPrefixedIdentifier('tag:' . $tag), $lifetime);
$redisTags[] = ['key' => $this->getPrefixedIdentifier('tag:' . $tag), 'value' => $entryIdentifier, 'expire' => $expire];
$expire = $this->calculateExpires($this->getPrefixedIdentifier('tags:' . $entryIdentifier), $lifetime);
$redisTags[] = ['key' => $this->getPrefixedIdentifier('tags:' . $entryIdentifier), 'value' => $tag, 'expire' => $expire];
return $redisTags;
}, []);
$this->redis->multi();
$result = $this->redis->set($this->getPrefixedIdentifier('entry:' . $entryIdentifier), $this->compress($data), $setOptions);
if (!$result instanceof \Redis) {
$this->verifyRedisVersionIsSupported();
}
foreach ($redisTags as $tag) {
$this->redis->sAdd($tag['key'], $tag['value']);
if ($tag['expire'] > 0) {
$this->redis->expire($tag['key'], $tag['expire']);
} else {
$this->redis->persist($tag['key']);
}
}
$this->redis->exec();
}
/**
* Calculate the max lifetime for a tag
*/
private function calculateExpires(string $tag, int $lifetime): int
{
$ttl = (int)$this->redis->ttl($tag);
if ($ttl < 0 || $lifetime === self::UNLIMITED_LIFETIME) {
return -1;
}
return max($ttl, $lifetime);
}
/**
* Loads data from the cache.
*
* @param string $entryIdentifier An identifier which describes the cache entry to load
* @return bool|string The cache entry's content as a string or false if the cache entry could not be loaded
* @api
*/
public function get(string $entryIdentifier): string|bool
{
$tries = 1;
do {
try {
return $this->uncompress($this->redis->get($this->getPrefixedIdentifier('entry:' . $entryIdentifier)));
} catch (\RedisException $exception) {
if ($tries > 8) {
throw $exception;
}
usleep($tries^2 * 100000);
$tries++;
}
} while (true);
}
/**
* Checks if a cache entry with the specified identifier exists.
*
* @param string $entryIdentifier An identifier specifying the cache entry
* @return boolean true if such an entry exists, false if not
* @api
*/
public function has(string $entryIdentifier): bool
{
// exists returned true or false in phpredis versions < 4.0.0, now it returns the number of keys
return (bool)$this->redis->exists($this->getPrefixedIdentifier('entry:' . $entryIdentifier));
}
/**
* Removes all cache entries matching the specified identifier.
* Usually this only affects one entry but if - for what reason ever -
* old entries for the identifier still exist, they are removed as well.
*
* @param string $entryIdentifier Specifies the cache entry to remove
* @throws \RuntimeException
* @return boolean true if (at least) an entry could be removed or false if no entry was found
* @api
*/
public function remove(string $entryIdentifier): bool
{
if ($this->isFrozen()) {
throw new \RuntimeException(sprintf('Cannot remove cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1323344192);
}
do {
$tagsKey = $this->getPrefixedIdentifier('tags:' . $entryIdentifier);
$this->redis->watch($tagsKey);
$tags = $this->redis->sMembers($tagsKey);
$this->redis->multi();
$this->redis->del($this->getPrefixedIdentifier('entry:' . $entryIdentifier));
foreach ($tags as $tag) {
$this->redis->sRem($this->getPrefixedIdentifier('tag:' . $tag), $entryIdentifier);
}
$this->redis->del($this->getPrefixedIdentifier('tags:' . $entryIdentifier));
$result = $this->redis->exec();
} while ($result === false);
// Reset iterator because it will be out of sync after a removal
$this->entryIterator = null;
return true;
}
/**
* Removes all cache entries of this cache
*
* The flush method will use the EVAL command to flush all entries and tags for this cache
* in an atomic way.
*
* @throws \RuntimeException
* @api
*/
public function flush(): void
{
// language=lua
$script = "
local keys = redis.call('KEYS', ARGV[1] .. '*')
for k1,key in ipairs(keys) do
redis.call('DEL', key)
end
";
$this->redis->eval($script, [$this->getPrefixedIdentifier('')], 0);
$this->frozen = null;
$this->entryIterator = null;
}
/**
* This backend does not need an externally triggered garbage collection
*
* @api
*/
public function collectGarbage(): void
{
}
/**
* Removes all cache entries of this cache which are tagged by the specified tag.
*
* @param string $tag The tag the entries must have
* @throws \RuntimeException
* @return integer The number of entries which have been affected by this flush
* @api
*/
public function flushByTag(string $tag): int
{
if ($this->isFrozen()) {
throw new \RuntimeException(sprintf('Cannot add or modify cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1323344192);
}
// language=lua
$script = "
local entries = redis.call('SMEMBERS', KEYS[1])
for k1,entryIdentifier in ipairs(entries) do
redis.call('DEL', ARGV[1]..'entry:'..entryIdentifier)
local tags = redis.call('SMEMBERS', ARGV[1]..'tags:'..entryIdentifier)
for k2,tagName in ipairs(tags) do
redis.call('SREM', ARGV[1]..'tag:'..tagName, entryIdentifier)
end
redis.call('DEL', ARGV[1]..'tags:'..entryIdentifier)
end
redis.call('DEL', KEYS[1])
return #entries
";
return $this->redis->eval($script, [$this->getPrefixedIdentifier('tag:' . $tag), $this->getPrefixedIdentifier('')], 1);
}
/**
* Removes all cache entries of this cache which are tagged by the specified tags.
*
* @param array<string> $tags The tag the entries must have
* @throws \RuntimeException
* @return integer The number of entries which have been affected by this flush
* @api
*/
public function flushByTags(array $tags): int
{
if ($this->isFrozen()) {
throw new \RuntimeException(sprintf('Cannot add or modify cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1647642328);
}
// language=lua
$script = "
local total_entries = 0
local num_arg = #ARGV
for i = 1, num_arg do
local entries = redis.call('SMEMBERS', KEYS[i])
for k1,entryIdentifier in ipairs(entries) do
redis.call('UNLINK', ARGV[i]..'entry:'..entryIdentifier)
local tags = redis.call('SMEMBERS', ARGV[i]..'tags:'..entryIdentifier)
for k2,tagName in ipairs(tags) do
redis.call('SREM', ARGV[i]..'tag:'..tagName, entryIdentifier)
end
redis.call('UNLINK', ARGV[i]..'tags:'..entryIdentifier)
end
redis.call('UNLINK', KEYS[i])
total_entries = total_entries + #entries
end
return total_entries
";
$flushedEntriesTotal = 0;
// Flush tags in batches
for ($i = 0, $iMax = count($tags); $i < $iMax; $i += $this->batchSize) {
$tagList = array_slice($tags, $i, $this->batchSize);
$keys = array_map(function ($tag) {
return $this->getPrefixedIdentifier('tag:' . $tag);
}, $tagList);
$values = array_fill(0, count($keys), $this->getPrefixedIdentifier(''));
$flushedEntries = $this->redis->eval($script, array_merge($keys, $values), count($keys));
$flushedEntriesTotal = is_int($flushedEntries) ? $flushedEntries : 0;
}
return $flushedEntriesTotal;
}
/**
* Finds and returns all cache entry identifiers which are tagged by the
* specified tag.
*
* @param string $tag The tag to search for
* @return string[] An array with identifiers of all matching entries. An empty array if no entries matched
* @api
*/
public function findIdentifiersByTag(string $tag): array
{
return $this->redis->sMembers($this->getPrefixedIdentifier('tag:' . $tag));
}
/**
* {@inheritdoc}
*/
public function current(): string|bool
{
return $this->get($this->getEntryIterator()->current());
}
/**
* {@inheritdoc}
*/
public function next(): void
{
$this->getEntryIterator()->next();
}
/**
* {@inheritdoc}
*/
public function key(): string|bool
{
$entryIdentifier = $this->getEntryIterator()->current();
if (!$entryIdentifier || !$this->has($entryIdentifier)) {
return false;
}
return $entryIdentifier;
}
/**
* {@inheritdoc}
*/
public function valid(): bool
{
return $this->key() !== false;
}
/**
* {@inheritdoc}
*/
public function rewind(): void
{
$this->getEntryIterator()->rewind();
}
/**
* Freezes this cache backend.
*
* All data in a frozen backend remains unchanged and methods which try to add
* or modify data result in an exception thrown. Possible expiry times of
* individual cache entries are ignored.
*
* A frozen backend can only be thawn by calling the flush() method.
*
* @throws \RuntimeException
*/
public function freeze(): void
{
if ($this->isFrozen()) {
throw new \RuntimeException(sprintf('Cannot add or modify cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1323344192);
}
do {
$iterator = $this->getEntryIterator();
$this->redis->multi();
foreach ($iterator as $entryIdentifier) {
$this->redis->persist($this->getPrefixedIdentifier('entry:' . $entryIdentifier));
}
/** @var array|bool $result */
$result = $this->redis->exec();
$this->redis->set($this->getPrefixedIdentifier('frozen'), 1);
} while ($result === false);
$this->frozen = true;
}
/**
* Tells if this backend is frozen.
*/
public function isFrozen(): bool
{
if (null === $this->frozen) {
$this->frozen = (bool)$this->redis->exists($this->getPrefixedIdentifier('frozen'));
}
return $this->frozen;
}
/**
* Sets the hostname or the socket of the Redis server
* @api
*/
public function setHostname(string $hostname): void
{
$this->hostname = $hostname;
}
/**
* Sets the port of the Redis server.
*
* Unused if you want to connect to a socket (i.e. hostname contains a /)
* @api
*/
public function setPort(int|string $port): void
{
$this->port = (int)$port;
}
/**
* Sets the database that will be used for this backend
* @api
*/
public function setDatabase(int|string $database): void
{
$this->database = (int)$database;
}
public function setPassword(string $password): void
{
$this->password = $password;
}
public function setCompressionLevel(int|string $compressionLevel): void
{
$this->compressionLevel = (int)$compressionLevel;
}
/**
* Sets the Maximum number of items for batch operations
*
* @api
*/
public function setBatchSize(int|string $batchSize): void
{
$this->batchSize = (int)$batchSize;
}
public function setRedis(\Redis $redis = null): void
{
if ($redis !== null) {
$this->redis = $redis;
}
}
private function uncompress(bool|string $value): bool|string
{
if (empty($value)) {
return $value;
}
return $this->useCompression() ? gzdecode((string) $value) : $value;
}
private function compress(string $value): string
{
return $this->useCompression() ? gzencode($value, $this->compressionLevel) : $value;
}
private function useCompression(): bool
{
return $this->compressionLevel > 0;
}
/**
* @throws CacheException
*/
private function getRedisClient(): \Redis
{
$redis = new \Redis();
try {
$connected = false;
// keep the assignment above! the connect calls below leaves the variable undefined, if an error occurs.
if (str_starts_with($this->hostname, '/')) {
// if the "hostname" starts with a slash, assume it's a socket and omit port.
$connected = $redis->connect($this->hostname);
} else {
$connected = $redis->connect($this->hostname, $this->port);
}
} finally {
if ($connected === false) {
throw new CacheException('Could not connect to Redis.', 1391972021);
}
}
if ($this->password !== '' && !$redis->auth($this->password)) {
throw new CacheException('Redis authentication failed.', 1502366200);
}
$redis->select($this->database);
return $redis;
}
/**
* @throws CacheException
*/
protected function verifyRedisVersionIsSupported(): void
{
// Redis client could be in multi mode, discard for checking the version
$this->redis->discard();
$serverInfo = (array)$this->redis->info('SERVER');
if (!isset($serverInfo['redis_version'])) {
throw new CacheException('Unsupported Redis version, the Redis cache backend needs at least version ' . self::MIN_REDIS_VERSION, 1438251553);
}
if (version_compare($serverInfo['redis_version'], self::MIN_REDIS_VERSION) < 0) {
throw new CacheException('Redis version ' . $serverInfo['redis_version'] . ' not supported, the Redis cache backend needs at least version ' . self::MIN_REDIS_VERSION, 1438251628);
}
}
/**
* Validates that the configured redis backend is accessible and returns some details about its configuration if that's the case
*
* @api
*/
public function getStatus(): Result
{
$result = new Result();
try {
$this->verifyRedisVersionIsSupported();
} catch (CacheException $exception) {
$result->addError(new Error($exception->getMessage(), (int)$exception->getCode(), [], 'Redis Version'));
return $result;
}
$serverInfo = (array)$this->redis->info('SERVER');
if (isset($serverInfo['redis_version'])) {
$result->addNotice(new Notice((string)$serverInfo['redis_version'], null, [], 'Redis version'));
}
if (isset($serverInfo['tcp_port'])) {
$result->addNotice(new Notice((string)$serverInfo['tcp_port'], null, [], 'TCP Port'));
}
if (isset($serverInfo['uptime_in_seconds'])) {
$result->addNotice(new Notice((string)$serverInfo['uptime_in_seconds'], null, [], 'Uptime (seconds)'));
}
return $result;
}
/**
* Create iterator over all entry keys in the cache, prefixed by its identifier
*/
private function getEntryIterator(): \Iterator
{
if (!$this->entryIterator) {
$prefix = $this->getPrefixedIdentifier('entry:');
$prefixLength = strlen($prefix);
$keys = $this->redis->keys($prefix . '*');
if (is_array($keys)) {
$entryIdentifiers = array_map(static fn (string $key) => substr($key, $prefixLength), $keys);
} else {
$entryIdentifiers = [];
}
$this->entryIterator = new \ArrayIterator($entryIdentifiers);
}
return $this->entryIterator;
}
}