-
Notifications
You must be signed in to change notification settings - Fork 4
/
CachePool.php
612 lines (534 loc) · 17.2 KB
/
CachePool.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
<?php
declare(strict_types=1);
namespace WpOop\TransientCache;
use DateInterval;
use DateTimeImmutable;
use Exception;
use Psr\SimpleCache\CacheInterface;
use RangeException;
use RuntimeException;
use wpdb;
use WpOop\TransientCache\Exception\CacheException;
use WpOop\TransientCache\Exception\InvalidArgumentException;
use Psr\SimpleCache\InvalidArgumentException as InvalidArgumentExceptionInterface;
use function is_int;
use function is_iterable;
use function is_null;
use function sprintf;
use function strlen;
use function strpos;
use function str_split;
use function substr;
use function delete_transient;
use function get_option;
use function get_transient;
use function set_transient;
use function array_map;
/**
* {@inheritDoc}
*
* Uses WordPress transients as storage medium.
*/
class CachePool implements CacheInterface
{
public const RESERVED_KEY_SYMBOLS = '{}()/\@:';
public const NAMESPACE_SEPARATOR = '/';
protected const TABLE_NAME_OPTIONS = 'options';
protected const FIELD_NAME_OPTION_NAME = 'option_name';
protected const OPTION_NAME_PREFIX_TRANSIENT = '_transient_';
protected const OPTION_NAME_PREFIX_TIMEOUT = 'timeout_';
protected const OPTION_NAME_MAX_LENGTH = 191;
/**
* @var wpdb
*/
protected $wpdb;
/**
* @var string
*/
protected $poolName;
/**
* @var mixed
*/
protected $defaultValue;
/**
* @var int|DateInterval
*/
protected $defaultTtl;
/**
* @param wpdb $wpdb The WP database object.
* @param string $poolName The name of this cache pool. Must be unique to this instance.
* @param mixed $defaultValue A random value. Used for false-negative detection. The more chaotic - the better.
* @param int|DateInterval $defaultTtl Default TTL to use when caching new entries.
*/
public function __construct(wpdb $wpdb, string $poolName, $defaultValue, $defaultTtl = 0)
{
if ($poolName === static::OPTION_NAME_PREFIX_TIMEOUT) {
throw new RangeException(sprintf('Pool name cannot be "%1$s"', static::OPTION_NAME_PREFIX_TIMEOUT));
}
$this->wpdb = $wpdb;
$this->poolName = $poolName;
$this->defaultValue = $defaultValue;
$this->defaultTtl = $defaultTtl;
}
/**
* @inheritDoc
*
* @throws CacheException If problem retrieving.
*/
public function get($key, $default = null)
{
$this->validateKey($key);
$transientKey = $this->prepareKey($key);
try {
$value = $this->getTransient($transientKey);
} catch (RangeException $e) {
return $default;
} catch (RuntimeException $e) {
$message = sprintf('Could not retrieve cache for key "%1$s": %2$s', $key, $e->getMessage());
throw new CacheException($message, 0, $e);
}
return $value;
}
/**
* @inheritDoc
*
* @throws CacheException If TTL cannot be normalized to a number of seconds.
* @throws InvalidArgumentException If TTL is invalid.
*/
public function set($key, $value, $ttl = null)
{
$this->validateKey($key);
$origKey = $key;
$key = $this->prepareKey($key);
$ttl = is_null($ttl) ? $this->defaultTtl : $ttl;
try {
$ttl = $ttl instanceof DateInterval
? $this->getIntervalDuration($ttl)
: $ttl;
} catch (Exception $e) {
throw new CacheException(sprintf('Could not normalize cache TTL: %s', $e->getMessage()));
}
if (!is_int($ttl)) {
throw new InvalidArgumentException('The specified cache TTL is invalid');
}
try {
$this->setTransient($key, $value, $ttl);
} catch (RuntimeException $e) {
$message = sprintf('Could not write value for key "%1$s" to cache: %2$s', $origKey, $e->getMessage());
throw new CacheException($message, 0, $e);
}
return true;
}
/**
* @inheritDoc
*
* @throws CacheException If problem deleting.
*/
public function delete($key)
{
$this->validateKey($key);
$origKey = $key;
$key = $this->prepareKey($key);
try {
$this->deleteTransient($key);
} catch (Exception $e) {
$message = sprintf('Failed to delete cache for key "%1$s": %2$s', $origKey, $e->getMessage());
throw new CacheException($message, 0, $e);
}
return true;
}
/**
* @inheritDoc
*
* @throws CacheException If problem clearing.
*/
public function clear()
{
try {
$keys = $this->getAllKeys();
$this->deleteMultiple($keys);
} catch (Exception|InvalidArgumentExceptionInterface $e) {
throw new CacheException(sprintf('Failed to clear cache: %s', $e->getMessage()), 0, $e);
}
return true;
}
/**
* @inheritDoc
*
* @throws CacheException If problem retrieving.
*/
public function getMultiple($keys, $default = null)
{
if (!is_iterable($keys)) {
throw new InvalidArgumentException('List of keys is not an iterable value');
}
$entries = [];
foreach ($keys as $key) {
$value = $this->get($key, $default);
$entries[$key] = $value;
}
return $entries;
}
/**
* @inheritDoc
*
* @throws CacheException If problem persisting.
*/
public function setMultiple($values, $ttl = null)
{
if (!is_iterable($values)) {
throw new InvalidArgumentException('List of keys is not an iterable value');
}
try {
$ttl = $ttl instanceof DateInterval
? $this->getIntervalDuration($ttl)
: $ttl;
} catch (Exception $e) {
throw new CacheException(sprintf('Could not normalize cache TTL: %s', $e->getMessage()));
}
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
}
return true;
}
/**
* @inheritDoc
*
* @throws CacheException If problem deleting.
*/
public function deleteMultiple($keys)
{
if (!is_iterable($keys)) {
throw new InvalidArgumentException('List of keys is not an iterable value');
}
foreach ($keys as $key) {
$this->delete($key);
}
return true;
}
/**
* @inheritDoc
*
* @throws CacheException If problem determining.
*/
public function has($key)
{
$default = $this->defaultValue;
$value = $this->get($key, $default);
return $value !== $default;
}
/**
* Retrieves a transient value, by key.
*
* @param string $key The transient key.
*
* @return mixed The transient value.
*
* @throws RangeException If transient for key not found.
* @throws RuntimeException If problem retrieving.
*/
protected function getTransient(string $key)
{
$value = $this->getTransientOriginal($key);
if ($value !== false) {
return $value;
}
$prefix = static::OPTION_NAME_PREFIX_TRANSIENT;
$optionKey = "{$prefix}{$key}";
try {
$this->getOption($optionKey);
} catch (RangeException $e) {
throw new RangeException(sprintf('Transient for key "%1$s" does not exist', $key), 0, $e);
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf('Could not verify existence of transient "%1$s"', $key), 0, $e);
}
return $value;
}
/**
* Retrieves a transient value as is.
*
* @param string $key The transient key.
*
* @return mixed The transient value.
*/
protected function getTransientOriginal(string $key)
{
$value = get_transient($key);
return $value;
}
/**
* Assigns a transient value, by key.
*
* @param string $key The transient key.
* @param mixed $value The transient value. Any serializable object.
* @param int $ttl The amount of seconds after which the transient will expire.
*
* @throws RangeException If key invalid.
* @throws RuntimeException If problem setting.
*/
protected function setTransient(string $key, $value, int $ttl): void
{
$this->validateTransientKey($key);
if(!set_transient($key, $value, $ttl)) {
throw new RuntimeException(sprintf('set_transient() failed with key "%1$s" with TTL %2$ss', $key, $ttl));
}
}
/**
* Retrieves an option value by name.
*
* @param string $key The option name.
*
* @return mixed The option value.
*
* @throws RangeException If option value does not exist.
* @throws RuntimeException If problem retrieving option.
*/
protected function getOption(string $key)
{
$errorValue = $this->defaultValue;
$value = $this->getOptionOriginal($key, $errorValue);
if ($value === $errorValue) {
throw new RangeException(sprintf('Option for key "%1$s" does not exist', $key));
}
return $value;
}
/**
* Retrieves an option value by name.
*
* @param string $key The option key.
* @param null $default The value to return if option not found.
*
* @return mixed The option value.
*/
protected function getOptionOriginal(string $key, $default = null)
{
return get_option($key, $default);
}
/**
* Deletes a transient with the specified key.
*
* @param string $key The key to delete a transient for.
*
* @throws RuntimeException If problem deleting.
*/
protected function deleteTransient(string $key): void
{
if (!delete_transient($key)) {
throw new RuntimeException(sprintf('delete_transient() failed for key "%1$s"', $key));
}
}
/**
* Validates a cache key.
*
* @param string $key The key to validate.
*
* @throws InvalidArgumentException If key is invalid.
*/
protected function validateKey(string $key)
{
$prefix = $this->getTimeoutOptionNamePrefix();
if (strlen("{$prefix}{$key}") > static::OPTION_NAME_MAX_LENGTH) {
throw new InvalidArgumentException(sprintf(
'Given the %1$d char length of this cache pool\'s name, the key length must not exceed %2$d chars',
strlen($this->poolName),
static::OPTION_NAME_MAX_LENGTH - strlen($prefix)
));
}
$reservedSymbols = str_split(static::RESERVED_KEY_SYMBOLS, 1);
foreach ($reservedSymbols as $symbol) {
if (strpos($key, $symbol) !== false) {
throw new InvalidArgumentException(sprintf('Cache key "%1$s" is invalid', $key));
}
}
}
/**
* Validates a transient key.
*
* @param string $key The key to validate.
*
* @throws RangeException If key is invalid.
*/
protected function validateTransientKey(string $key): void
{
$maxLength = $this->getTransientKeyMaxLength();
$keyLength = strlen($key);
if ($keyLength > $maxLength) {
throw new RangeException(sprintf('Transient key "%1$s" length is %2$d chars, which exceeds max length of %3$d chars', $key, $keyLength, $maxLength));
}
}
/**
* Retrieves the amount of characters at most allowed in a transient key.
*
* @return int The amount of characters.
*/
protected function getTransientKeyMaxLength(): int
{
$longestPrefix = $this->getTransientTimeoutOptionNamePrefix();
$keyMaxLength = static::OPTION_NAME_MAX_LENGTH - strlen($longestPrefix);
return $keyMaxLength;
}
/**
* Prepares a cache key, giving it a namespace.
*
* @param string $key The key to prepare.
*
* @return string The prepared key.
*/
protected function prepareKey(string $key): string
{
$namespace = $this->poolName;
$separator = static::NAMESPACE_SEPARATOR;
return "{$namespace}{$separator}{$key}";
}
/**
* Retrieves all keys that correspond to this cache pool.
*
* @throws Exception If problem retrieving.
*
* @return iterable A list of keys.
*/
protected function getAllKeys(): iterable
{
$tableName = $this->getTableName(static::TABLE_NAME_OPTIONS);
$fieldName = static::FIELD_NAME_OPTION_NAME;
$prefix = $this->getOptionNamePrefix();
$query = "SELECT `$fieldName` FROM `$tableName` WHERE `$fieldName` LIKE '$prefix%'";
$results = $this->selectColumn($query, $fieldName);
$keys = $this->getCacheKeysFromOptionNames($results);
return $keys;
}
/**
* Runs a SELECT query, and retrieves a list of values for a field with the specified name.
*
* @param string $query The SELECT query.
* @param string $columnName The name of the field to retrieve.
* @param array $args Query parameters.
*
* @return iterable The list of values for the specified field.
*/
protected function selectColumn(string $query, string $columnName, array $args = []): iterable
{
$query = $this->prepareQuery($query, $args);
$results = $this->wpdb->get_results($query, ARRAY_A);
return array_map(function ($row) use ($columnName) {
return $row[$columnName] ?? null;
}, $results);
}
/**
* Retrieve the name of a DB table by its identifier.
*
* @param string $identifier The table identifier.
*
* @return string The table name in the DB.
*/
protected function getTableName(string $identifier): string
{
$prefix = $this->wpdb->prefix;
$tableName = "{$prefix}{$identifier}";
return $tableName;
}
/**
* Prepares a parameterized query.
*
* @param string $query The query to prepare. May include placeholders.
* @param array $params The parameters that will replace corresponding placeholders in the query.
*
* @return string The prepared query. Parameters will be interpolated.
*/
protected function prepareQuery(string $query, array $params = []): string
{
if (empty($params)) {
return $query;
}
$prepared = $this->wpdb->prepare($query, ...$params);
return $prepared;
}
/**
* Retrieves all cache keys that correspond to the given list of option names
*
* @param iterable $optionNames
*
* @throws Exception If problem retrieving.
*
* @return iterable A list of cache keys.
*/
protected function getCacheKeysFromOptionNames(iterable $optionNames): iterable
{
$keys = [];
foreach ($optionNames as $name) {
$key = $this->getCacheKeyFromOptionName($name);
$keys[] = $key;
}
return $keys;
}
/**
* Retrieves the prefix of option names that represent transients of this cache pool.
*
* @return string The prefix.
*/
protected function getOptionNamePrefix(): string
{
$transientPrefix = static::OPTION_NAME_PREFIX_TRANSIENT;
$separator = static::NAMESPACE_SEPARATOR;
$namespace = $this->poolName;
$prefix = "{$transientPrefix}{$namespace}{$separator}";
return $prefix;
}
/**
* Retrieves the prefix of option names that represent transient timeouts of this cache pool.
*
* @return string The prefix.
*/
protected function getTimeoutOptionNamePrefix(): string
{
$transientPrefix = $this->getTransientTimeoutOptionNamePrefix();
$separator = static::NAMESPACE_SEPARATOR;
$namespace = $this->poolName;
$prefix = "{$transientPrefix}{$namespace}{$separator}";
return $prefix;
}
/**
* Retrieves the prefix of an option name that represents a transient timeout.
*
* This is the longest prefix of transient options.
*
* @return string The prefix.
*/
protected function getTransientTimeoutOptionNamePrefix(): string
{
return static::OPTION_NAME_PREFIX_TRANSIENT . static::OPTION_NAME_PREFIX_TIMEOUT;
}
/**
* Retrieves the cache key that corresponds to the specified option name.
*
* @param string $name The option name.
*
* @return string The cache key.
*
* @throws Exception If problem determining key.
*/
protected function getCacheKeyFromOptionName(string $name): string
{
$prefix = $this->getOptionNamePrefix();
if (strpos($name, $prefix) !== 0) {
throw new RangeException(sprintf('Option name "%1$s" is not formed according to this cache pool', $name));
}
$key = substr($name, strlen($prefix));
return $key;
}
/**
* Retrieves the total duration from an interval.
*
* @param DateInterval $interval The interval.
*
* @throws Exception If problem retrieving.
*
* @return int The duration in seconds.
*/
protected function getIntervalDuration(DateInterval $interval): int
{
$reference = new DateTimeImmutable();
$endTime = $reference->add($interval);
return $endTime->getTimestamp() - $reference->getTimestamp();
}
}