-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-cache.php
837 lines (722 loc) · 22.5 KB
/
object-cache.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
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
<?php
/*
Plugin Name: Object Cache
Plugin URI: https://www.littlebizzy.com/plugins/object-cache
Description: Drop-in persistent object cache for WordPress based on Redis in-memory storage that supports Predis, clusters, and WP-CLI (forked from PressJitsu).
Version: 1.2.3
Author: LittleBizzy
Author URI: https://www.littlebizzy.com
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Upstream Plugin Name: Pressjitsu Redis Object Cache
Upstream Plugin URI: https://github.com/pressjitsu/pj-object-cache-red
Upstream Author: Eric Mann + Erick Hitter + Pressjitsu, Inc.
Upstream Author URI: https://pressjitsu.com
PBP Version: N/A
WC requires at least: 3.3
WC tested up to: 3.8
Prefix: OBJCHE
*/
declare(strict_types=1);
// check if OBJECT_CACHE is disabled in wp-config.php
if ( defined( 'OBJECT_CACHE' ) && !OBJECT_CACHE ) {
return;
}
// check if 'Redis' class exists
if( class_exists( 'Redis' ) ) :
/**
* Adds a value to cache.
*
* If the specified key wp_cache_addalready exists, the value is not stored and the function
* returns false.
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_add( $key, $value, $group = 'default', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->add( $key, $value, $group, $expiration );
}
/**
* Closes the cache.
*
* This function has ceased to do anything since WordPress 2.5. The
* functionality was removed along with the rest of the persistent cache. This
* does not mean that plugins can't implement this function when they need to
* make sure that the cache is cleaned up after WordPress no longer needs it.
*
* @return bool Always returns True
*/
function wp_cache_close() {
return true;
}
/**
* Decrement a numeric item's value.
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to decrement the item's value.
* @param string $group The group value appended to the $key.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return int|bool Returns item's new value on success or FALSE on failure.
*/
function wp_cache_decr( $key, $offset = 1, $group = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->decr( $key, $offset, $group );
}
/**
* Remove the item from the cache.
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param int $time The amount of time the server will wait to delete the item in seconds.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_delete( $key, $group = 'default', $time = 0 ) {
global $wp_object_cache;
return $wp_object_cache->delete( $key, $group, $time );
}
/**
* Invalidate all items in the cache.
*
* @param int $delay Number of seconds to wait before invalidating the items.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_flush( $delay = 0 ) {
global $wp_object_cache;
return $wp_object_cache->flush( $delay );
}
/**
* Retrieve object from cache.
*
* Gets an object from cache based on $key and $group.
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool|mixed Cached object value.
*/
function wp_cache_get( $key, $group = 'default', $force = false ) {
global $wp_object_cache;
return $wp_object_cache->get( $key, $group, $force );
}
/**
* Retrieve multiple values from cache.
*
* Gets multiple values from cache, including across multiple groups
*
* Usage: array( 'group0' => array( 'key0', 'key1', 'key2', ), 'group1' => array( 'key0' ) )
*
* Mirrors the Memcached Object Cache plugin's argument and return-value formats
*
* @param array $groups Array of groups and keys to retrieve
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool|mixed Array of cached values, keys in the format $group:$key. Non-existent keys false
*/
function wp_cache_get_multi( $groups, $unserialize = true ) {
global $wp_object_cache;
return $wp_object_cache->get_multi( $groups, $unserialize );
}
/**
* Increment a numeric item's value.
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to increment the item's value.
* @param string $group The group value appended to the $key.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return int|bool Returns item's new value on success or FALSE on failure.
*/
function wp_cache_incr( $key, $offset = 1, $group = 'default' ) {
global $wp_object_cache;
return $wp_object_cache->incr( $key, $offset, $group );
}
/**
* Sets up Object Cache Global and assigns it.
*
* @global WP_Object_Cache $wp_object_cache WordPress Object Cache
*
* @return void
*/
function wp_cache_init() {
global $wp_object_cache;
$wp_object_cache = new WP_Object_Cache();
}
/**
* Replaces a value in cache.
*
* This method is similar to "add"; however, is does not successfully set a value if
* the object's key is not already set in cache.
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_replace( $key, $value, $group = 'default', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->replace( $key, $value, $group, $expiration );
}
/**
* Sets a value in cache.
*
* The value is set whether or not this key already exists in Redis.
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
function wp_cache_set( $key, $value, $group = 'default', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->set( $key, $value, $group, $expiration );
}
/**
* Switch the interal blog id.
*
* This changes the blog id used to create keys in blog specific groups.
*
* @param int $_blog_id Blog ID
*
* @global WP_Object_Cache $wp_object_cache
*
* @return bool
*/
function wp_cache_switch_to_blog( $_blog_id ) {
global $wp_object_cache;
return $wp_object_cache->switch_to_blog( $_blog_id );
}
/**
* Adds a group or set of groups to the list of Redis groups.
*
* @param string|array $groups A group or an array of groups to add.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return void
*/
function wp_cache_add_global_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_global_groups( $groups );
}
/**
* Adds a group or set of groups to the list of non-Redis groups.
*
* @param string|array $groups A group or an array of groups to add.
*
* @global WP_Object_Cache $wp_object_cache
*
* @return void
*/
function wp_cache_add_non_persistent_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_non_persistent_groups( $groups );
}
class WP_Object_Cache {
/**
* Holds the Redis client.
*
* @var Redis
*/
private $redis;
/**
* Track if Redis is available
*
* @var bool
*/
private $redis_connected = false;
/**
* Local cache
*
* @var array
*/
public $cache = array();
private $to_unserialize = array();
public $to_preload = array();
/**
* List of global groups.
*
* @var array
*/
public $global_groups = array(
'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',
);
private $_global_groups;
/**
* List of groups not saved to Redis.
*
* @var array
*/
public $no_redis_groups = array( 'comment', 'counts' );
/**
* Prefix used for global groups.
*
* @var string
*/
public $global_prefix = '';
/**
* Prefix used for non-global groups.
*
* @var string
*/
public $blog_prefix = '';
/**
* Track how many requests were found in cache
*
* @var int
*/
public $cache_hits = 0;
/**
* Track how may requests were not cached
*
* @var int
*/
public $cache_misses = 0;
private $multisite;
public $stats = array();
/**
* Instantiate the Redis class.
*
* Instantiates the Redis class.
*
* @param null $persistent_id To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance.
*/
public function __construct( $redis_instance = null ) {
// General Redis settings
$redis = array(
'host' => '127.0.0.1',
'port' => 6379,
);
if ( defined( 'WP_REDIS_BACKEND_HOST' ) && WP_REDIS_BACKEND_HOST ) {
$redis['host'] = WP_REDIS_BACKEND_HOST;
}
if ( defined( 'WP_REDIS_BACKEND_PORT' ) && WP_REDIS_BACKEND_PORT ) {
$redis['port'] = WP_REDIS_BACKEND_PORT;
}
if ( defined( 'WP_REDIS_BACKEND_AUTH' ) && WP_REDIS_BACKEND_AUTH ) {
$redis['auth'] = WP_REDIS_BACKEND_AUTH;
}
if ( defined( 'WP_REDIS_BACKEND_DB' ) && WP_REDIS_BACKEND_DB ) {
$redis['database'] = WP_REDIS_BACKEND_DB;
}
// Use Redis PECL library.
try {
if ( is_null( $redis_instance ) ) {
$redis_instance = new Redis();
}
$this->redis = $redis_instance;
$this->redis->connect( $redis['host'], $redis['port'] );
$this->redis->setOption( Redis::OPT_SERIALIZER, (string) Redis::SERIALIZER_PHP );
if ( isset( $redis['auth'] ) ) {
$this->redis->auth( $redis['auth'] );
}
if ( isset( $redis['database'] ) ) {
$this->redis->select( $redis['database'] );
}
$this->redis_connected = true;
} catch ( RedisException $e ) {
// When Redis is unavailable, fall back to the internal back by forcing all groups to be "no redis" groups
$this->no_redis_groups = array_unique( array_merge( $this->no_redis_groups, $this->global_groups ) );
$this->redis_connected = false;
}
/**
* This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for
* multi single WP installs on the same server.
*/
if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {
define( 'WP_CACHE_KEY_SALT', '' );
}
$this->multisite = is_multisite();
$this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '';
$this->_global_groups = array_flip( $this->global_groups );
$this->maybe_preload();
}
public function maybe_preload() {
if ( ! $this->can_redis() || empty( $_SERVER['REQUEST_URI'] ) ) {
return;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
$request_hash = md5( json_encode( array(
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI'],
) ) );
$this->preload( $request_hash );
if ( defined( 'DOING_TESTS' ) && DOING_TESTS ) {
return $request_hash;
}
register_shutdown_function( array( $this, 'save_preloads' ), $request_hash );
}
public function preload( $hash ) {
$keys = $this->get( $hash, 'pj-preload' );
if ( is_array( $keys ) ) {
$this->get_multi( $keys, false );
}
}
public function save_preloads( $hash ) {
$keys = array();
foreach ( $this->to_preload as $group => $_keys ) {
if ( $group === 'pj-preload' ) {
continue;
}
if ( in_array( $group, $this->no_redis_groups ) ) {
continue;
}
$_keys = array_keys( $_keys );
$keys[ $group ] = $_keys;
}
$this->set( $hash, $keys, 'pj-preload' );
}
/**
* Is Redis available?
*
* @return bool
*/
protected function can_redis() {
return $this->redis_connected;
}
/**
* Adds a value to cache.
*
* If the specified key already exists, the value is not stored and the function
* returns false.
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function add( $_key, $value, $group, $expiration = 0 ) {
if ( wp_suspend_cache_addition() ) {
return false;
}
list( $key, $redis_key ) = $this->build_key( $_key, $group );
if ( isset( $this->cache[ $group ], $this->cache[ $group ][ $key ] ) && false !== $this->cache[ $group ][ $key ] ) {
return false;
}
return $this->set( $_key, $value, $group, $expiration );
}
/**
* Replace a value in the cache.
*
* If the specified key doesn't exist, the value is not stored and the function
* returns false.
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function replace( $_key, $value, $group, $expiration = 0 ) {
list( $key, $redis_key ) = $this->build_key( $_key, $group );
// If group is a non-Redis group, save to internal cache, not Redis
if ( in_array( $group, $this->no_redis_groups ) || ! $this->can_redis() ) {
if ( ! isset( $this->cache[ $group ], $this->cache[ $group ][ $key ] ) ) {
return false;
}
} else {
if ( ! $this->redis->exists( $redis_key ) ) {
return false;
}
}
return $this->set( $_key, $value, $group, $expiration );
}
/**
* Remove the item from the cache.
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function delete( $_key, $group ) {
list( $key, $redis_key ) = $this->build_key( $_key, $group );
if ( in_array( $group, $this->no_redis_groups ) || ! $this->can_redis() ) {
if ( ! isset( $this->cache[ $group ], $this->cache[ $group ][ $key ] ) ) {
return false;
}
unset( $this->cache[ $group ][ $key ] );
unset( $this->to_preload[ $group ][ $key ] );
unset( $this->to_unserialize[ $redis_key ] );
return true;
}
unset( $this->cache[ $group ][ $key ] );
unset( $this->to_preload[ $group ][ $key ] );
unset( $this->to_unserialize[ $redis_key ] );
return (bool) $this->redis->del( $redis_key );
}
/**
* Invalidate all items in the cache.
*
* @return bool
*/
public function flush() {
$this->cache = array();
$this->to_preload = array();
$this->to_unserialize = array();
if ( $this->can_redis() ) {
$this->redis->flushDb();
}
return true;
}
/**
* Retrieve object from cache.
*
* Gets an object from cache based on $key and $group.
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @return bool|mixed Cached object value.
*/
public function get( $_key, $group = 'default', $force = false ) {
list( $key, $redis_key ) = $this->build_key( $_key, $group );
$this->to_preload[ $group ][ $_key ] = true;
if ( ! $force && isset( $this->cache[ $group ][ $key ] ) ) {
$value = $this->cache[ $group ][ $key ];
if ( isset( $this->to_unserialize[ $redis_key ] ) ) {
unset( $this->to_unserialize[ $redis_key ] );
if ( is_string( $value ) ) {
$value = unserialize( $value );
}
$this->cache[ $group ][ $key ] = $value;
}
$this->cache_hits += 1;
return is_object( $value ) ? clone $value : $value;
}
if ( in_array( $group, $this->no_redis_groups ) || ! $this->can_redis() ) {
$this->cache_misses += 1;
return false;
}
// Fetch from Redis
$value = $this->redis->get( $redis_key );
if ( ! is_string( $value ) ) {
$this->cache[ $group ][ $key ] = false;
$this->cache_misses += 1;
return false;
}
$value = is_numeric( $value ) ? $value : unserialize( $value );
$this->cache[ $group ][ $key ] = $value;
$this->cache_hits += 1;
return $value;
}
/**
* Retrieve multiple values from cache.
*
* Gets multiple values from cache, including across multiple groups
*
* Usage: array( 'group0' => array( 'key0', 'key1', 'key2', ), 'group1' => array( 'key0' ) )
*
* @param array $groups Array of groups and keys to retrieve
* @return bool|mixed Array of cached values, keys in the format $group:$key. Non-existent keys null.
*/
public function get_multi( $groups, $unserialize = true ) {
if ( empty( $groups ) || ! is_array( $groups ) ) {
return false;
}
// Retrieve requested caches and reformat results to mimic Memcached Object Cache's output
$cache = array();
$fetch_keys = array();
$map = array();
foreach ( $groups as $group => $keys ) {
if ( in_array( $group, $this->no_redis_groups ) || ! $this->can_redis() ) {
foreach ( $keys as $_key ) {
list( $key, $redis_key ) = $this->build_key( $_key, $group );
$cache[ $group ][ $key ] = $this->get( $_key, $group );
}
continue;
}
if ( empty( $cache[ $group ] ) ) {
$cache[ $group ] = array();
}
foreach ( $keys as $_key ) {
list( $key, $redis_key ) = $this->build_key( $_key, $group );
if ( isset( $this->cache[ $group ][ $key ] ) ) {
$cache[ $group ][ $key ] = $this->cache[ $group ][ $key ];
continue;
}
// Fetch these from Redis
$map[ $redis_key ] = array( $group, $key );
$fetch_keys[] = $redis_key;
}
}
// Nothing else to fetch
if ( empty( $fetch_keys ) ) {
return $cache;
}
$results = $this->redis->mget( $fetch_keys );
foreach( array_combine( $fetch_keys, $results ) as $redis_key => $value ) {
list( $group, $key ) = $map[ $redis_key ];
if ( is_string( $value ) ) {
if ( ! $unserialize && ! is_numeric( $value ) ) {
$this->to_unserialize[ $redis_key ] = true;
} elseif ( $unserialize ) {
$this->to_preload[ $group ][ $key ] = true;
$value = is_numeric( $value ) ? $value : unserialize( $value );
}
} else {
$value = false;
}
$this->cache[ $group ][ $key ] = $cache[ $group ][ $key ] = $value;
}
return $cache;
}
/**
* Sets a value in cache.
*
* The value is set whether or not this key already exists in Redis.
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function set( $_key, $value, $group = 'default', $expiration = 0 ) {
list( $key, $redis_key ) = $this->build_key( $_key, $group );
if ( is_object( $value ) ) {
$value = clone $value;
}
$this->cache[ $group ][ $key ] = $value;
if ( in_array( $group, $this->no_redis_groups ) || ! $this->can_redis() ) {
return true;
}
$value = is_numeric( $value ) ? $value : serialize( $value );
// Save to Redis
if ( $expiration ) {
$this->redis->setex( $redis_key, $expiration, $value );
} else {
$this->redis->set( $redis_key, $value );
}
return true;
}
/**
* Increment a Redis counter by the amount specified
*
* @param string $key
* @param int $offset
* @param string $group
* @return bool
*/
public function incr( $_key, $offset = 1, $group ) {
list( $key, $redis_key ) = $this->build_key( $_key, $group );
if ( in_array( $group, $this->no_redis_groups ) || ! $this->can_redis() ) {
// Consistent with the Redis behavior (start from 0 if not exists)
if ( ! isset( $this->cache[ $group ][ $key ] ) ) {
$this->cache[ $group ][ $key ] = 0;
}
$this->cache[ $group ][ $key ] += $offset;
return true;
}
// Save to Redis
$value = $this->redis->incrBy( $redis_key, $offset );
$this->cache[ $group ][ $key ] = $value;
return $value;
}
/**
* Decrement a Redis counter by the amount specified
*
* @param string $key
* @param int $offset
* @param string $group
* @return bool
*/
public function decr( $key, $offset = 1, $group = 'default' ) {
return $this->incr( $key, $offset * -1, $group );
}
/**
* Builds a key for the cached object using the blog_id, key, and group values.
*
* @author Ryan Boren This function is inspired by the original WP Memcached Object cache.
* @link http://wordpress.org/extend/plugins/memcached/
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
*
* @return array
*/
public function build_key( $key, $group = 'default' ) {
$prefix = '';
if ( ! isset( $this->_global_groups[ $group ] ) ) {
$prefix = $this->blog_prefix;
}
$local_key = $prefix . $key;
return array( $local_key, WP_CACHE_KEY_SALT . "$prefix$group:$key" );
}
/**
* In multisite, switch blog prefix when switching blogs
*
* @param int $_blog_id
* @return bool
*/
public function switch_to_blog( $blog_id ) {
$this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
}
/**
* Sets the list of global groups.
*
* @param array $groups List of groups that are global.
*/
public function add_global_groups( $groups ) {
$groups = (array) $groups;
if ( $this->can_redis() ) {
$this->global_groups = array_unique( array_merge( $this->global_groups, $groups ) );
} else {
$this->no_redis_groups = array_unique( array_merge( $this->no_redis_groups, $groups ) );
}
$this->_global_groups = array_flip( $this->global_groups );
}
/**
* Sets the list of groups not to be cached by Redis.
*
* @param array $groups List of groups that are to be ignored.
*/
public function add_non_persistent_groups( $groups ) {
$groups = (array) $groups;
$this->no_redis_groups = array_unique( array_merge( $this->no_redis_groups, $groups ) );
}
}
endif; // if 'Redis' class exists