-
Notifications
You must be signed in to change notification settings - Fork 16
/
object-cache.php
executable file
·1401 lines (1211 loc) · 38.4 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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: WordPress APCu Object Cache Backend
Plugin URI: https://github.com/l3rady/object-cache-apcu
Description: APCu backend for WordPress' Object Cache
Version: 1.2
Author: Scott Cariss
Author URI: https://cariss.dev
*/
/* Copyright 2024 Scott Cariss (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Stop direct access
defined('ABSPATH') or exit;
/**
* Adds data to the cache, if the cache key does not already exist.
*
* @param int|string $key The cache key to use for retrieval later
* @param mixed $data The data to add to the cache store
* @param string $group The group to add the cache to
* @param int $expire When the cache data should be expired
*
* @return bool False if cache key and group already exist, true on success
*/
function wp_cache_add($key, $data, $group = 'default', $expire = 0)
{
return WP_Object_Cache::instance()->add($key, $data, $group, $expire);
}
/**
* Adds multiple values to the cache in one call.
*
* @param array $data Array of keys and values to be set.
* @param string $group Optional. Where the cache contents are grouped. Default default.
* @param int $expire Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if cache key and group already exist.
*/
function wp_cache_add_multiple(array $data, $group = 'default', $expire = 0)
{
return WP_Object_Cache::instance()->add_multiple($data, $group, $expire);
}
/**
* 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 numeric cache item's value
*
* @param int|string $key The cache key to increment
* @param int $offset The amount by which to decrement the item's value. Default is 1.
* @param string $group The group the key is in.
*
* @return false|int False on failure, the item's new value on success.
*/
function wp_cache_decr($key, $offset = 1, $group = 'default')
{
return WP_Object_Cache::instance()->decr($key, $offset, $group);
}
/**
* Removes the cache contents matching key and group.
*
* @param int|string $key What the contents in the cache are called
* @param string $group Where the cache contents are grouped
*
* @return bool True on successful removal, false on failure
*/
function wp_cache_delete($key, $group = 'default')
{
return WP_Object_Cache::instance()->delete($key, $group);
}
/**
* Deletes multiple values from the cache in one call.
*
* @param array $keys Array of keys under which the cache to deleted.
* @param string $group Optional. Where the cache contents are grouped. Default default.
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if the contents were not deleted.
*/
function wp_cache_delete_multiple(array $keys, $group = 'default')
{
return WP_Object_Cache::instance()->delete_multiple($keys, $group);
}
/**
* Removes all cache items.
*
* @return bool False on failure, true on success
*/
function wp_cache_flush()
{
return WP_Object_Cache::instance()->flush();
}
/**
* Removes all cache items from the in-memory runtime cache.
*
* @return bool True on success, false on failure.
*/
function wp_cache_flush_runtime()
{
return WP_Object_Cache::instance()->flush_runtime();
}
/**
* Retrieves the cache contents from the cache by key and group.
*
* @param int|string $key What the contents in the cache are called
* @param string $group Where the cache contents are grouped
* @param bool $force Does nothing with APCu object cache
* @param bool &$found Whether key was found in the cache. Disambiguates a return of false, a storable value.
*
* @return bool|mixed False on failure to retrieve contents or the cache contents on success
*/
function wp_cache_get($key, $group = 'default', $force = false, &$found = null)
{
return WP_Object_Cache::instance()->get($key, $group, $force, $found);
}
/**
* Retrieves multiple values from the cache in one call.
*
* @param array $keys Array of keys under which the cache contents are stored.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param bool $force Optional. Whether to force an update of the local cache
* from the persistent cache. Default false.
* @return array Array of return values, grouped by key. Each value is either
* the cache contents on success, or false on failure.
*/
function wp_cache_get_multiple($keys, $group = 'default', $force = false)
{
return WP_Object_Cache::instance()->get_multiple($keys, $group, $force);
}
/**
* Increment numeric cache item's value
*
* @param int|string $key The cache key to increment
* @param int $offset The amount by which to increment the item's value. Default is 1.
* @param string $group The group the key is in.
*
* @return false|int False on failure, the item's new value on success.
*/
function wp_cache_incr($key, $offset = 1, $group = 'default')
{
return WP_Object_Cache::instance()->incr($key, $offset, $group);
}
/**
* Sets up Object Cache Global and assigns it.
*
* @global WP_Object_Cache $wp_object_cache WordPress Object Cache
*/
function wp_cache_init()
{
$GLOBALS['wp_object_cache'] = WP_Object_Cache::instance();
}
/**
* Replaces the contents of the cache with new data.
*
* @param int|string $key What to call the contents in the cache
* @param mixed $data The contents to store in the cache
* @param string $group Where to group the cache contents
* @param int $expire When to expire the cache contents
*
* @return bool False if not exists, true if contents were replaced
*/
function wp_cache_replace($key, $data, $group = 'default', $expire = 0)
{
return WP_Object_Cache::instance()->replace($key, $data, $group, $expire);
}
/**
* Saves the data to the cache.
*
* @param int|string $key What to call the contents in the cache
* @param mixed $data The contents to store in the cache
* @param string $group Where to group the cache contents
* @param int $expire When to expire the cache contents
*
* @return bool False on failure, true on success
*/
function wp_cache_set($key, $data, $group = 'default', $expire = 0)
{
return WP_Object_Cache::instance()->set($key, $data, $group, $expire);
}
/**
* Sets multiple values to the cache in one call.
*
* @param array $data Array of keys and values to be set.
* @param string $group Optional. Where the cache contents are grouped. Default default.
* @param int $expire Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false on failure.
*/
function wp_cache_set_multiple(array $data, $group = 'default', $expire = 0)
{
return WP_Object_Cache::instance()->set_multiple($data, $group, $expire);
}
/**
* Switch the internal blog id.
*
* This changes the blog id used to create keys in blog specific groups.
*
* @param int $blog_id Blog ID
*/
function wp_cache_switch_to_blog($blog_id)
{
WP_Object_Cache::instance()->switch_to_blog($blog_id);
}
/**
* Adds a group or set of groups to the list of global groups.
*
* @param string|array $groups A group or an array of groups to add
*/
function wp_cache_add_global_groups($groups)
{
WP_Object_Cache::instance()->add_global_groups($groups);
}
/**
* Adds a group or set of groups to the list of non-persistent groups.
*
* @param string|array $groups A group or an array of groups to add
*/
function wp_cache_add_non_persistent_groups($groups)
{
WP_Object_Cache::instance()->add_non_persistent_groups($groups);
}
/**
* Function was depreciated and now does nothing
*
* @return bool Always returns false
*/
function wp_cache_reset()
{
_deprecated_function(__FUNCTION__, '3.5', 'wp_cache_switch_to_blog()');
return false;
}
/**
* Invalidate a site's object cache
*
* @param mixed $sites Sites ID's that want flushing.
* Don't pass a site to flush current site
*
* @return bool
*/
function wp_cache_flush_site($sites = null)
{
return WP_Object_Cache::instance()->flush_sites($sites);
}
/**
* Invalidate a groups object cache
*
* @param mixed $groups A group or an array of groups to invalidate
*
* @return bool
*/
function wp_cache_flush_group($groups = 'default')
{
return WP_Object_Cache::instance()->flush_groups($groups);
}
/**
* Determines whether the object cache implementation supports a particular feature.
*
* @since 6.1.0
*
* @param string $feature Name of the feature to check for. Possible values include:
* 'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple',
* 'flush_runtime', 'flush_group'.
* @return bool True if the feature is supported, false otherwise.
*/
function wp_cache_supports( $feature ) {
switch ( $feature ) {
case 'add_multiple':
case 'set_multiple':
case 'get_multiple':
case 'delete_multiple':
case 'flush_runtime':
case 'flush_group':
return true;
default:
return false;
}
}
/**
* WordPress APCu Object Cache Backend
*
* The WordPress Object Cache is used to save on trips to the database. The
* APCu Object Cache stores all of the cache data to APCu and makes the cache
* contents available by using a key, which is used to name and later retrieve
* the cache contents.
*/
class WP_Object_Cache
{
/**
* @var string MD5 hash of the current installation ABSPATH
*/
private $_absPath;
/**
* @var bool Stores if APCu is available.
*/
private $_apcuAvailable;
/**
* @var int The sites current blog ID. This only
* differs if running a multi-site installations
*/
private $_blogPrefix;
/**
* @var int Keeps count of how many times the
* cache was successfully received from APCu
*/
public $cache_hits = 0;
/**
* @var int Keeps count of how many times the
* cache was not successfully received from APCu
*/
public $cache_misses = 0;
/**
* @var array Holds a list of cache groups that are
* shared across all sites in a multi-site installation
*/
private $_globalGroups = [];
/**
* @var array Holds an array of versions of the retrieved groups
*/
private $_groupVersions = [];
/**
* @var bool True if the current installation is a multi-site
*/
private $_multiSite;
/**
* @var array Holds cache that is to be non persistent
*/
private $_nonPersistentCache = [];
/**
* @var array Holds a list of cache groups that are not to be saved to APCu
*/
private $_nonPersistentGroups = [];
/**
* @var array
*/
private $_localCache = [];
/**
* @var array Holds an array of versions of the retrieved sites
*/
private $_siteVersions = [];
private static $_instance;
/**
* Singleton. Return instance of WP_Object_Cache
*
* @return WP_Object_Cache
*/
public static function instance()
{
if (self::$_instance === null) {
self::$_instance = new WP_Object_Cache();
}
return self::$_instance;
}
/**
* __clone not allowed
*/
private function __clone()
{
}
/**
* Direct access to __construct not allowed.
*/
private function __construct()
{
global $blog_id;
if (!defined('WP_APCU_KEY_SALT')) {
/**
* Set in config if you are using some sort of shared
* config where ABSPATH is the same on all sites
*/
define('WP_APCU_KEY_SALT', 'wp');
}
/**
* define('WP_APCU_LOCAL_CACHE', false) to disable local
* array cache and force all cache to be returned from APCu
*/
if (!defined('WP_APCU_LOCAL_CACHE')) {
define('WP_APCU_LOCAL_CACHE', true);
}
$this->_absPath = md5(ABSPATH);
$this->_apcuAvailable = (extension_loaded('apcu') && ini_get('apc.enabled'));
$this->_multiSite = is_multisite();
$this->_blogPrefix = $this->_multiSite ? $blog_id : 1;
}
public function stats()
{
echo '<p>';
echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
echo '</p>';
echo '<ul>';
foreach ( $this->_localCache as $group => $cache ) {
echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
}
echo '</ul>';
}
/**
* Adds data to the cache, if the cache key does not already exist.
*
* @param int|string $key The cache key to use for retrieval later
* @param mixed $var The data to add to the cache store
* @param string $group The group to add the cache to
* @param int $ttl When the cache data should be expired
*
* @return bool False if cache key and group already exist, true on success
*/
public function add($key, $var, $group = 'default', $ttl = 0)
{
if (function_exists('wp_suspend_cache_addition') && wp_suspend_cache_addition()) {
return false;
}
$key = $this->_key($key, $group);
if (!$this->_apcuAvailable || $this->_is_non_persistent_group($group)) {
return $this->_add_np($key, $var);
}
return $this->_add($key, $var, $ttl);
}
/**
* Adds multiple values to the cache in one call.
*
* @param array $data Array of keys and values to be added.
* @param string $group Optional. Where the cache contents are grouped. Default default.
* @param int $ttl Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if cache key and group already exist.
*/
public function add_multiple(array $data, $group = 'default', $ttl = 0)
{
$values = [];
foreach ($data as $key => $value) {
$values[$key] = $this->add($key, $value, $group, $ttl);
}
return $values;
}
/**
* Adds data to APCu cache, if the cache key does not already exist.
*
* @param string $key The cache key to use for retrieval later
* @param mixed $var The data to add to the cache store
* @param int $ttl When the cache data should be expired
*
* @return bool False if cache key and group already exist, true on success
*/
private function _add($key, $var, $ttl)
{
if (apcu_add($key, $var, max((int)$ttl, 0))) {
if (WP_APCU_LOCAL_CACHE) {
$this->_localCache[$key] = is_object($var) ? clone $var : $var;
}
return true;
}
return false;
}
/**
* Adds data to non persistent cache, if the cache key does not already exist.
*
* @param string $key The cache key to use for retrieval later
* @param mixed $var The data to add to the cache store
*
* @return bool False if cache key and group already exist, true on success
*/
private function _add_np($key, $var)
{
if ($this->_exists_np($key)) {
return false;
}
return $this->_set_np($key, $var);
}
/**
* Sets the list of global groups.
*
* @param string|array $groups List of groups that are global.
*/
public function add_global_groups($groups)
{
foreach ((array)$groups as $group) {
$this->_globalGroups[$group] = true;
}
}
/**
* Sets the list of non persistent groups.
*
* @param string|array $groups List of groups that are non persistent.
*/
public function add_non_persistent_groups($groups)
{
foreach ((array)$groups as $group) {
$this->_nonPersistentGroups[$group] = true;
}
}
/**
* Decrement numeric cache item's value
*
* @param int|string $key The cache key to increment
* @param int $offset The amount by which to decrement the item's value. Default is 1.
* @param string $group The group the key is in.
*
* @return false|int False on failure, the item's new value on success.
*/
public function decr($key, $offset = 1, $group = 'default')
{
$key = $this->_key($key, $group);
if (!$this->_apcuAvailable || $this->_is_non_persistent_group($group)) {
return $this->_decr_np($key, $offset);
}
return $this->_decr($key, $offset);
}
/**
* Decrement numeric APCu cache item's value
*
* @param string $key The cache key to increment
* @param int $offset The amount by which to decrement the item's value. Default is 1.
*
* @return false|int False on failure, the item's new value on success.
*/
private function _decr($key, $offset)
{
$this->_get($key, $success);
if (!$success) {
return false;
}
$value = apcu_dec($key, max((int)$offset, 0));
if ($value !== false && WP_APCU_LOCAL_CACHE) {
$this->_localCache[$key] = $value;
}
return $value;
}
/**
* Decrement numeric non persistent cache item's value
*
* @param string $key The cache key to increment
* @param int $offset The amount by which to decrement the item's value. Default is 1.
*
* @return false|int False on failure, the item's new value on success.
*/
private function _decr_np($key, $offset)
{
if (!$this->_exists_np($key)) {
return false;
}
$offset = max((int)$offset, 0);
$var = $this->_get_np($key);
$var = is_numeric($var) ? $var : 0;
$var -= $offset;
return $this->_set_np($key, $var);
}
/**
* Remove the contents of the cache key in the group
*
* If the cache key does not exist in the group, then nothing will happen.
*
* @param int|string $key What the contents in the cache are called
* @param string $group Where the cache contents are grouped
* @param bool $deprecated Deprecated.
*
* @return bool False if the contents weren't deleted and true on success
*/
public function delete($key, $group = 'default', $deprecated = false)
{
$key = $this->_key($key, $group);
if (!$this->_apcuAvailable || $this->_is_non_persistent_group($group)) {
return $this->_delete_np($key);
}
return $this->_delete($key);
}
/**
* Deletes multiple values from the cache in one call.
*
* @param array $keys Array of keys to be deleted.
* @param string $group Optional. Where the cache contents are grouped. Default default.
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if the contents were not deleted.
*/
public function delete_multiple(array $keys, $group = 'default')
{
$values = [];
foreach ($keys as $key) {
$values[$key] = $this->delete($key, $group);
}
return $values;
}
/**
* Remove the contents of the APCu cache key in the group
*
* If the cache key does not exist in the group, then nothing will happen.
*
* @param string $key What the contents in the cache are called
*
* @return bool False if the contents weren't deleted and true on success
*/
private function _delete($key)
{
unset($this->_localCache[$key]);
return apcu_delete($key);
}
/**
* Remove the contents of the non persistent cache key in the group
*
* If the cache key does not exist in the group, then nothing will happen.
*
* @param string $key What the contents in the cache are called
*
* @return bool False if the contents weren't deleted and true on success
*/
private function _delete_np($key)
{
if (array_key_exists($key, $this->_nonPersistentCache)) {
unset($this->_nonPersistentCache[$key]);
return true;
}
return false;
}
/**
* Checks if the cached non persistent key exists
*
* @param string $key What the contents in the cache are called
*
* @return bool True if cache key exists else false
*/
private function _exists_np($key)
{
return array_key_exists($key, $this->_nonPersistentCache);
}
/**
* Clears the object cache of all data
*
* @return bool Always returns true
*/
public function flush()
{
$this->_nonPersistentCache = [];
if (WP_APCU_LOCAL_CACHE) {
$this->_localCache = [];
}
if ($this->_apcuAvailable) {
apcu_clear_cache();
}
return true;
}
/**
* if using local cache clear that else flush all
*
* @return bool Always returns true
*/
public function flush_runtime()
{
$this->_nonPersistentCache = [];
if (WP_APCU_LOCAL_CACHE) {
$this->_localCache = [];
} elseif ($this->_apcuAvailable) {
apcu_clear_cache();
}
return true;
}
/**
* Invalidate a groups object cache
*
* @param mixed $groups A group or an array of groups to invalidate
*
* @return bool
*/
public function flush_groups($groups)
{
$groups = (array)$groups;
if (empty($groups)) {
return false;
}
foreach ($groups as $group) {
$version = $this->_get_group_cache_version($group);
$this->_set_group_cache_version($group, $version + 1);
}
return true;
}
/**
* Invalidate a site's object cache
*
* @param mixed $sites Sites ID's that want flushing.
* Don't pass a site to flush current site
*
* @return bool
*/
public function flush_sites($sites)
{
$sites = (array)$sites;
if (empty($sites)) {
$sites = [$this->_blogPrefix];
}
// Add global groups (site 0) to be flushed.
if (!in_array(0, $sites, false)) {
$sites[] = 0;
}
foreach ($sites as $site) {
$version = $this->_get_site_cache_version($site);
$this->_set_site_cache_version($site, $version + 1);
}
return true;
}
/**
* Retrieves the cache contents, if it exists
*
* The contents will be first attempted to be retrieved by searching by the
* key in the cache key. If the cache is hit (success) then the contents
* are returned.
*
* On failure, the number of cache misses will be incremented.
*
* @param int|string $key What the contents in the cache are called
* @param string $group Where the cache contents are grouped
* @param bool $force Not used.
* @param bool &$success
*
* @return bool|mixed False on failure to retrieve contents or the cache contents on success
*/
public function get($key, $group = 'default', $force = false, &$success = null)
{
$key = $this->_key($key, $group);
if (!$this->_apcuAvailable || $this->_is_non_persistent_group($group)) {
$var = $this->_get_np($key, $success);
} else {
$var = $this->_get($key, $success);
}
if ($success) {
$this->cache_hits++;
} else {
$this->cache_misses++;
}
return $var;
}
/**
* Retrieves the APCu cache contents, if it exists
*
* @param string $key What the contents in the cache are called
* @param bool &$success
*
* @return bool|mixed False on failure to retrieve contents or the cache contents on success
*/
private function _get($key, &$success = null)
{
if (WP_APCU_LOCAL_CACHE && array_key_exists($key, $this->_localCache)
) {
$success = true;
$var = $this->_localCache[$key];
} else {
$var = apcu_fetch($key, $success);
if ($success && WP_APCU_LOCAL_CACHE) {
$this->_localCache[$key] = $var;
}
}
if (is_object($var)) {
$var = clone $var;
}
return $var;
}
/**
* Retrieves the non persistent cache contents, if it exists
*
* @param string $key What the contents in the cache are called
* @param bool &$success
*
* @return bool|mixed False on failure to retrieve contents or the cache contents on success
*/
private function _get_np($key, &$success = null)
{
if (array_key_exists($key, $this->_nonPersistentCache)) {
$success = true;
return $this->_nonPersistentCache[$key];
}
$success = false;
return false;
}
/**
* Get the cache version of a given key
*
* @param string $key
*
* @return int cache version
*/
private function _get_cache_version($key)
{
if ($this->_apcuAvailable) {
$version = (int)apcu_fetch($key);
} elseif (array_key_exists($key, $this->_nonPersistentCache)) {
$version = (int)$this->_nonPersistentCache[$key];
} else {
$version = 0;
}
return $version;
}
/**
* Build cache version key
*
* @param string $type Type of key, for site or group
* @param mixed $value the group or site id
*
* @return string The key
*/
private function _get_cache_version_key($type, $value)
{
return WP_APCU_KEY_SALT . ':' . $this->_absPath . ':' . $type . ':' . $value;
}
/**
* Get the groups cache version
*
* @param string $group The group to get version for
*
* @return int The group cache version
*/
private function _get_group_cache_version($group)
{
if (!isset($this->_groupVersions[$group])) {
$this->_groupVersions[$group] = $this->_get_cache_version(
$this->_get_cache_version_key(
'GroupVersion',
$group
)
);
}
return $this->_groupVersions[$group];
}
/**
* Retrieves multiple values from the cache in one call.
*
* @param array $keys Array of keys under which the cache contents are stored.
* @param string $group Optional. Where the cache contents are grouped. Default 'default'.
* @param bool $force Optional. Whether to force an update of the local cache
* from the persistent cache. Default false.
* @return array Array of return values, grouped by key. Each value is either
* the cache contents on success, or false on failure.
*/
public function get_multiple($keys, $group = 'default', $force = false)
{
$values = [];
foreach ($keys as $key) {
$values[$key] = $this->get($key, $group, $force);
}
return $values;
}
/**