-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-cache.php
More file actions
716 lines (550 loc) · 21.6 KB
/
object-cache.php
File metadata and controls
716 lines (550 loc) · 21.6 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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
<?php
declare( strict_types=1 );
/**
* Plugin Name: WP Object Cache Drop-In
* Plugin URI: https://github.com/Jazz-Man/wp-object-cache
* Description: Redis, Memcached or Apcu backend for the WP Object Cache
* Author: Vasyl Sokolyk.
*
* @return true
*/
namespace {
use JazzMan\WPObjectCache\RedisAdapter;
function wp_cache_close(): bool {
return true;
}
function wp_cache_add( int|string $key, mixed $value, string $group = 'default', int $expiration = 0 ): bool|int|Redis|null {
return wp_object_cache()->set( $key, $value, $group );
}
/**
* @param array{string: mixed} $data
*/
function wp_cache_add_multiple( array $data, string $group = 'default', int $expiration = 0 ): bool|Redis|null {
return wp_object_cache()->set_multiple( $data, $group );
}
function wp_cache_decr( int|string $key, float|int $offset = 1, string $group = 'default' ): false|float|int|Redis|null {
return wp_object_cache()->decrement( $key, $offset, $group );
}
function wp_cache_delete( int|string $key, string $group = 'default' ): bool|int|Redis|null {
return wp_object_cache()->delete( $key, $group );
}
/**
* @param string[] $keys
*/
function wp_cache_delete_multiple( array $keys, string $group = 'default' ): bool|int|Redis|null {
return wp_object_cache()->delete( $keys, $group );
}
function wp_cache_flush(): bool|Redis|null {
return wp_object_cache()->flush();
}
/**
* @param string[]|string $group
*/
function wp_cache_flush_group( array|string $group ): false|int|Redis|null {
return wp_object_cache()->flush_group( $group );
}
function wp_cache_get( int|string $key, string $group = 'default', bool $force = false, ?bool &$found = null ): mixed {
return wp_object_cache()->get( $key, $group );
}
/**
* @param string[] $keys
*/
function wp_cache_get_multiple( array $keys, string $group = 'default', bool $force = false, ?bool &$found = null ): array|false|Redis|null {
return wp_object_cache()->get_multiple( $keys, $group );
}
function wp_cache_incr( string $key, float|int $offset = 1, string $group = 'default' ): false|float|int|Redis|null {
return wp_object_cache()->increment( $key, $offset, $group );
}
function wp_cache_replace( int|string $key, mixed $value = null, string $group = 'default', int $expiration = 0 ): bool|int|Redis|null {
return wp_object_cache()->set( $key, $value, $group );
}
function wp_cache_set( int|string $key, mixed $value = null, string $group = 'default', int $expiration = 0 ): bool|int|Redis|null {
return wp_object_cache()->set( $key, $value, $group );
}
function wp_cache_set_multiple( array $data, string $group = 'default', int $expire = 0 ): bool|Redis|null {
return wp_object_cache()->set_multiple( $data, $group );
}
/**
* Switch blog prefix, which changes the cache that is accessed.
*/
function wp_cache_switch_to_blog( int|string $blogId ): bool {
return wp_object_cache()->switch_to_blog( $blogId );
}
/**
* Adds a group or set of groups to the list of non-persistent groups.
*
* @param string|string[] $groups a group or an array of groups to add
*/
function wp_cache_add_global_groups( array|string $groups ): void {
wp_object_cache()->add_global_groups( $groups );
}
/**
* Adds a group or set of groups to the list of non-persistent groups.
*
* @param string|string[] $groups a group or an array of groups to add
*/
function wp_cache_add_non_persistent_groups( array|string $groups ): void {
wp_object_cache()->add_non_persistent_groups( $groups );
}
/**
* Sets up Object Cache Global and assigns it.
*/
function wp_cache_init(): void {
global $wp_object_cache;
if ( ! $wp_object_cache instanceof RedisAdapter ) {
$wp_object_cache = new RedisAdapter();
}
}
function wp_object_cache_get_stats(): void {
wp_object_cache()->stats();
}
function wp_object_cache(): RedisAdapter {
global $wp_object_cache;
wp_cache_init();
/** @var RedisAdapter $wp_object_cache */
return $wp_object_cache;
}
function wp_object_cache_instance(): ?Redis {
return wp_object_cache()->get_redis();
}
function wp_object_redis_status(): bool {
return wp_object_cache()->redis_status();
}
function wp_cache_supports( string $feature ): bool {
return match ( $feature ) {
'add_multiple',
'set_multiple',
'get_multiple',
'delete_multiple',
// 'flush_runtime',
'flush_group' => true,
default => false,
};
}
}
namespace JazzMan\WPObjectCache {
use JetBrains\PhpStorm\ExpectedValues;
use Redis;
use RedisException;
class RedisAdapter {
/**
* List of global groups.
*
* @var string[]
*/
private array $global_groups = [
'blog-details',
'blog-id-cache',
'blog-lookup',
'global-posts',
'networks',
'rss',
'sites',
'site-details',
'site-lookup',
'site-options',
'site-transient',
'users',
'useremail',
'userlogins',
'usermeta',
'user_meta',
'userslugs',
];
/**
* List of groups not saved to Redis.
*
* @var string[]
*/
private array $ignored_groups = [ 'counts', 'plugins' ];
/**
* Prefix used for non-global groups.
*/
private int|string $blog_prefix = '';
private ?Redis $redis = null;
/**
* Track if Redis is available.
*/
private bool $is_connected = false;
private readonly bool $is_multisite;
public function __construct() {
/**
* @var string|null $table_prefix
* @var int|string|null $blog_id
*/
global $blog_id, $table_prefix;
$this->is_multisite = \function_exists( 'is_multisite' ) && is_multisite();
$this->blog_prefix = $this->is_multisite ? (int) $blog_id : (string) $table_prefix;
$this->connect();
}
/**
* Is Redis available?
*/
public function redis_status(): bool {
return $this->is_connected;
}
/**
* Returns the Redis instance.
*/
public function get_redis(): ?Redis {
return $this->redis_status() ? $this->redis : null;
}
/**
* Remove the item from the cache.
*
* @param string|string[] $keys the key under which to store the value
* @param string $group the group value appended to the $key
*/
public function delete( array|int|string $keys, string $group = 'default' ): bool|int|Redis|null {
if ( ! $this->can_modify( $group ) ) {
return false;
}
$_keys = array_map( static fn ( int|string $key ): string => self::sanitize_key( $key ), (array) $keys );
try {
$result = $this->redis?->hDel( $group, ...$_keys );
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
$result = false;
}
return $result;
}
/**
* @param int $delay number of seconds to wait before invalidating the items
*/
public function flush( int $delay = 0 ): bool|Redis|null {
if ( ! $this->redis_status() ) {
return false;
}
$delay = abs( $delay );
if ( 0 !== $delay ) {
sleep( $delay );
}
try {
$results = $this->redis?->flushDB();
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
$results = false;
}
return $results;
}
/**
* @param string|string[] $groups
*/
public function flush_group( array|string $groups ): false|int|Redis|null {
$list = array_filter( (array) $groups, fn ( $group ): bool => $this->can_modify( $group ) );
if ( empty( $list ) ) {
return false;
}
try {
$result = $this->redis?->del( ...$list );
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
$result = false;
}
return $result;
}
/**
* Retrieve object from cache.
*
* Gets an object from cache based on $key and $group.
*/
public function get( int|string $key, string $group = 'default' ): mixed {
$key = self::sanitize_key( $key );
try {
$result = $this->redis?->hGet( $group, $key );
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
return false;
}
return $result;
}
/**
* @param string[] $keys
*/
public function get_multiple( array $keys, string $group = 'default' ): array|false|Redis|null {
$keys = array_map( static fn ( int|string $key ): string => self::sanitize_key( $key ), $keys );
try {
$result = $this->redis?->hMGet( $group, $keys );
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
return false;
}
return $result;
}
/**
* Sets a value in cache.
*
* The value is set whether or not this key already exists in Redis.
*/
public function set( int|string $key, mixed $value, string $group = 'default' ): bool|int|Redis|null {
if ( $this->is_suspend_cache() ) {
return false;
}
if ( ! $this->can_modify( $group ) ) {
return false;
}
$key = self::sanitize_key( $key );
try {
$result = $this->redis?->hSet( $group, $key, $value );
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
$result = false;
}
return $result;
}
/**
* @param array{string: mixed} $data
*/
public function set_multiple( array $data, string $group = 'default' ): bool|Redis|null {
if ( ! $this->can_modify( $group ) ) {
return false;
}
/**
* @var string|int $key
* @var mixed $value
*/
foreach ( $data as $key => $value ) {
$key = self::sanitize_key( $key );
$data[ $key ] = $value;
}
try {
$result = $this->redis?->hMSet( $group, $data );
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
$result = false;
}
return $result;
}
public function increment( int|string $key, float|int $offset = 1, string $group = 'default' ): false|float|int|Redis|null {
if ( ! $this->can_modify( $group ) ) {
return false;
}
$key = self::sanitize_key( $key );
$is_float = \is_float( $offset );
try {
$result = $is_float ? $this->redis?->hIncrByFloat( $group, $key, (float) $offset ) : $this->redis?->hIncrBy( $group, $key, (int) $offset );
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
return false;
}
return $result;
}
public function decrement( int|string $key, float|int $offset = 1, string $group = 'default' ): false|float|int|Redis|null {
$offset = -1 * abs( $offset );
return $this->increment( $key, $offset, $group );
}
public function stats(): void {
if ( ! $this->redis_status() ) {
echo '<p><strong>Redis Status:</strong> Not Connected</p>';
return;
}
try {
/** @var array{keyspace_hits: int|float, keyspace_misses: int|float}|null $info */
$info = $this->redis?->info( 'STATS' );
if ( empty( $info ) ) {
return;
}
$hits = $info['keyspace_hits'];
$misses = $info['keyspace_misses'];
$ratio = $hits / ( $hits + $misses );
printf(
'<p>
<strong>Redis Status:</strong> Connected <br/>
<strong>Cache Hits:</strong> %s <br/>
<strong>Cache misses:</strong> %s <br/>
<strong>Hit/Miss Ratio:</strong> %s <br/>
</p>',
esc_attr( (string) $hits ),
esc_attr( (string) $misses ),
esc_attr( (string) $ratio )
);
} catch ( RedisException ) {
return;
}
}
public function switch_to_blog( int|string $blogId ): bool {
if ( ! $this->is_multisite ) {
return false;
}
$this->blog_prefix = $blogId;
return true;
}
/**
* @param string|string[] $groups list of groups that are global
*/
public function add_global_groups( array|string $groups ): void {
$groups = (array) $groups;
if ( $this->redis_status() ) {
$this->global_groups = array_unique( [ ...$this->global_groups, ...$groups ] );
return;
}
$this->add_non_persistent_groups( $groups );
}
/**
* Sets the list of groups not to be cached by Redis.
*
* @param string|string[] $groups list of groups that are to be ignored
*/
public function add_non_persistent_groups( array|string $groups ): void {
$groups = (array) $groups;
$this->ignored_groups = array_unique( [ ...$this->ignored_groups, ...$groups ] );
}
private function is_suspend_cache(): bool {
return \function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition();
}
private function connect(): void {
try {
$this->redis = new Redis();
$params = $this->get_connection_params();
$this->redis->pconnect(
host: $params['host'],
port: $params['port'],
timeout: $params['timeout'],
persistent_id: $params['persistent_id'],
retry_interval: $params['retry_interval'],
read_timeout: $params['read_timeout']
);
$this->redis->setOption( Redis::OPT_SERIALIZER, $params['serializer'] );
$this->redis->setOption( Redis::OPT_COMPRESSION, $params['compression'] );
if ( Redis::COMPRESSION_NONE !== $params['compression'] && ! empty( $params['compression_level'] ) ) {
$this->redis->setOption( Redis::OPT_COMPRESSION_LEVEL, $params['compression_level'] );
}
$this->redis->setOption( Redis::OPT_SCAN, Redis::SCAN_PREFIX );
$this->redis->setOption( Redis::OPT_PREFIX, "{$params['prefix']}:" );
$this->is_connected = $this->redis->isConnected();
} catch ( RedisException $redisException ) {
$this->handle_exception( $redisException );
}
}
/**
* @return array{host: string, port: int, timeout: int, persistent_id: ?string, prefix: string, retry_interval: int, read_timeout: int, serializer: int, compression: int, compression_level: ?int}
*/
private function get_connection_params(): array {
/**
* @param string $constant_name
* @param string[] $list
*
* @return array{string,int}
*/
$get_valid_redis_list = /**
* @return int[]
*
* @psalm-return array<string, int>
*/
static function (
#[ExpectedValues( values: [
'SERIALIZER',
'COMPRESSION',
] )]
string $constant_name,
array $list
): array {
$data = [];
/** @var string[] $list */
foreach ( $list as $item ) {
$constant = sprintf( Redis::class.'::%s_%s', $constant_name, strtoupper( $item ) );
if ( \defined( $constant ) ) {
$value = \constant( $constant );
if ( \is_int( $value ) ) {
$data[ $item ] = $value;
}
}
}
return $data;
};
$serializer = \defined( 'Redis::SERIALIZER_IGBINARY' ) ? Redis::SERIALIZER_IGBINARY : Redis::SERIALIZER_PHP;
$compression = \defined( 'Redis::COMPRESSION_LZF' ) ? Redis::COMPRESSION_LZF : Redis::COMPRESSION_NONE;
/** @var array{host: string, port: int, timeout: int, persistent_id: ?string, prefix: string, retry_interval: int, read_timeout: int, serializer: int, compression: int, compression_level: ?int} $params */
$params = [
'host' => '127.0.0.1',
'port' => 6379,
'timeout' => 5,
'persistent_id' => DB_NAME,
'prefix' => implode( ':', [
DB_NAME,
$this->blog_prefix,
] ),
'retry_interval' => 5,
'read_timeout' => 5,
'serializer' => $serializer,
'compression' => $compression,
'compression_level' => Redis::COMPRESSION_LZF === $compression ? 3 : null,
];
/** @var array{string: int} $compressions */
$compressions = $get_valid_redis_list(
'COMPRESSION',
[
'none',
'lzf',
'zstd',
'lz4',
]
);
/** @var array{string: int} $serializers */
$serializers = $get_valid_redis_list(
'SERIALIZER',
[
'none',
'php',
'igbinary',
'msgpack',
'json',
]
);
foreach ( array_keys( $params ) as $setting ) {
$constant = sprintf( 'WP_REDIS_%s', strtoupper( $setting ) );
if ( \defined( $constant ) ) {
/** @var string|int $constant_val */
$constant_val = \constant( $constant );
switch ( $setting ) {
case 'serializer':
if ( ! empty( $serializers[ (string) $constant_val ] ) ) {
$constant_val = $serializers[ $constant_val ];
}
break;
case 'compression':
if ( ! empty( $compressions[ (string) $constant_val ] ) ) {
$constant_val = $compressions[ $constant_val ];
}
break;
case 'prefix':
$constant_val = rtrim( (string) $constant_val, ':' );
break;
}
$params[ $setting ] = $constant_val;
}
}
return $params;
}
private static function sanitize_key( int|string $key ): string {
return (string) preg_replace( '/[^a-z0-9_\-:]/', '', strtolower( (string) $key ) );
}
/**
* Checks if the given group is part the ignored group array.
*
* @param string $group Name of the group to check
*/
private function is_ignored_group( string $group ): bool {
return \in_array( $group, $this->ignored_groups, true );
}
/**
* Handle the redis failure gracefully or throw an exception.
*
* @param RedisException $redisException exception thrown
*
* @internal
*/
private function handle_exception( RedisException $redisException ): void {
$this->is_connected = false;
// When Redis is unavailable, fall back to the internal cache by forcing all groups to be "no redis" groups
$this->ignored_groups = array_unique( array_merge( $this->ignored_groups, $this->global_groups ) );
error_log( $redisException );
}
private function can_modify( string $group = 'default' ): bool {
if ( $this->is_ignored_group( $group ) ) {
return false;
}
return $this->redis_status();
}
}
}