16
* @global WP_Object_Cache $wp_object_cache
16
* @see WP_Object_Cache::add()
17
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
18
* @param int|string $key The cache key to use for retrieval later
19
* @param mixed $data The data to add to the cache store
20
* @param string $group The group to add the cache to
21
* @param int $expire When the cache data should be expired
22
* @return bool False if cache key and group already exist, true on success
19
* @param int|string $key The cache key to use for retrieval later.
20
* @param mixed $data The data to add to the cache.
21
* @param string $group Optional. The group to add the cache to. Enables the same key
22
* to be used across groups. Default empty.
23
* @param int $expire Optional. When the cache data should expire, in seconds.
24
* Default 0 (no expiration).
25
* @return bool False if cache key and group already exist, true on success.
24
27
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
25
28
global $wp_object_cache;
31
34
* Closes the cache.
33
36
* This function has ceased to do anything since WordPress 2.5. The
34
* functionality was removed along with the rest of the persistent cache. This
35
* does not mean that plugins can't implement this function when they need to
36
* make sure that the cache is cleaned up after WordPress no longer needs it.
37
* functionality was removed along with the rest of the persistent cache.
39
* This does not mean that plugins can't implement this function when they need
40
* to make sure that the cache is cleaned up after WordPress no longer needs it.
40
* @return true Always returns True
44
* @return true Always returns true.
42
46
function wp_cache_close() {
47
* Decrement numeric cache item's value
51
* Decrements numeric cache item's value.
51
* @global WP_Object_Cache $wp_object_cache
55
* @see WP_Object_Cache::decr()
56
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
53
* @param int|string $key The cache key to increment
54
* @param int $offset The amount by which to decrement the item's value. Default is 1.
55
* @param string $group The group the key is in.
58
* @param int|string $key The cache key to decrement.
59
* @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
60
* @param string $group Optional. The group the key is in. Default empty.
56
61
* @return false|int False on failure, the item's new value on success.
58
63
function wp_cache_decr( $key, $offset = 1, $group = '' ) {
69
* @global WP_Object_Cache $wp_object_cache
74
* @see WP_Object_Cache::delete()
75
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
71
* @param int|string $key What the contents in the cache are called
72
* @param string $group Where the cache contents are grouped
73
* @return bool True on successful removal, false on failure
77
* @param int|string $key What the contents in the cache are called.
78
* @param string $group Optional. Where the cache contents are grouped. Default empty.
79
* @return bool True on successful removal, false on failure.
75
function wp_cache_delete($key, $group = '') {
81
function wp_cache_delete( $key, $group = '' ) {
76
82
global $wp_object_cache;
78
84
return $wp_object_cache->delete($key, $group);
86
* @global WP_Object_Cache $wp_object_cache
92
* @see WP_Object_Cache::flush()
93
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
88
95
* @return bool False on failure, true on success
101
* @global WP_Object_Cache $wp_object_cache
108
* @see WP_Object_Cache::get()
109
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
103
* @param int|string $key What the contents in the cache are called
104
* @param string $group Where the cache contents are grouped
105
* @param bool $force Whether to force an update of the local cache from the persistent cache (default is false)
106
* @param bool &$found Whether key was found in the cache. Disambiguates a return of false, a storable value.
111
* @param int|string $key The key under which the cache contents are stored.
112
* @param string $group Optional. Where the cache contents are grouped. Default empty.
113
* @param bool $force Optional. Whether to force an update of the local cache from the persistent
114
* cache. Default false.
115
* @param bool &$found Optional. Whether the key was found in the cache. Disambiguates a return of false,
116
* a storable value. Passed by reference. Default null.
107
117
* @return bool|mixed False on failure to retrieve contents or the cache
108
118
* contents on success
121
* @global WP_Object_Cache $wp_object_cache
131
* @see WP_Object_Cache::incr()
132
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
123
* @param int|string $key The cache key to increment
124
* @param int $offset The amount by which to increment the item's value. Default is 1.
125
* @param string $group The group the key is in.
134
* @param int|string $key The key for the cache contents that should be incremented.
135
* @param int $offset Optional. The amount by which to increment the item's value. Default 1.
136
* @param string $group Optional. The group the key is in. Default empty.
126
137
* @return false|int False on failure, the item's new value on success.
128
139
function wp_cache_incr( $key, $offset = 1, $group = '' ) {
150
* @global WP_Object_Cache $wp_object_cache
161
* @see WP_Object_Cache::replace()
162
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
152
* @param int|string $key What to call the contents in the cache
153
* @param mixed $data The contents to store in the cache
154
* @param string $group Where to group the cache contents
155
* @param int $expire When to expire the cache contents
156
* @return bool False if not exists, true if contents were replaced
164
* @param int|string $key The key for the cache data that should be replaced.
165
* @param mixed $data The new data to store in the cache.
166
* @param string $group Optional. The group for the cache data that should be replaced.
168
* @param int $expire Optional. When to expire the cache contents, in seconds.
169
* Default 0 (no expiration).
170
* @return bool False if original value does not exist, true if contents were replaced
158
172
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
159
173
global $wp_object_cache;
165
179
* Saves the data to the cache.
181
* Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
169
* @global WP_Object_Cache $wp_object_cache
185
* @see WP_Object_Cache::set()
186
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
171
* @param int|string $key What to call the contents in the cache
172
* @param mixed $data The contents to store in the cache
173
* @param string $group Where to group the cache contents
174
* @param int $expire When to expire the cache contents
188
* @param int|string $key The cache key to use for retrieval later.
189
* @param mixed $data The contents to store in the cache.
190
* @param string $group Optional. Where to group the cache contents. Enables the same key
191
* to be used across groups. Default empty.
192
* @param int $expire Optional. When to expire the cache contents, in seconds.
193
* Default 0 (no expiration).
175
194
* @return bool False on failure, true on success
177
196
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
184
* Switch the interal blog id.
203
* Switches the interal blog ID.
186
205
* This changes the blog id used to create keys in blog specific groups.
190
* @global WP_Object_Cache $wp_object_cache
209
* @see WP_Object_Cache::switch_to_blog()
210
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
192
* @param int $blog_id Blog ID
212
* @param int $blog_id Blog ID.
194
214
function wp_cache_switch_to_blog( $blog_id ) {
195
215
global $wp_object_cache;
205
* @global WP_Object_Cache $wp_object_cache
225
* @see WP_Object_Cache::add_global_groups()
226
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
207
* @param string|array $groups A group or an array of groups to add
228
* @param string|array $groups A group or an array of groups to add.
209
230
function wp_cache_add_global_groups( $groups ) {
210
231
global $wp_object_cache;
220
* @param string|array $groups A group or an array of groups to add
241
* @param string|array $groups A group or an array of groups to add.
222
243
function wp_cache_add_non_persistent_groups( $groups ) {
223
244
// Default cache doesn't persist so nothing to do here.
227
* Reset internal cache keys and structures. If the cache backend uses global
228
* blog or site IDs as part of its cache keys, this function instructs the
229
* backend to reset those keys and perform any cleanup since blog or site IDs
230
* have changed since cache init.
248
* Reset internal cache keys and structures.
250
* If the cache backend uses global blog or site IDs as part of its cache keys,
251
* this function instructs the backend to reset those keys and perform any cleanup
252
* since blog or site IDs have changed since cache init.
232
254
* This function is deprecated. Use wp_cache_switch_to_blog() instead of this
233
255
* function when preparing the cache for a blog switch. For clearing the cache
261
* @deprecated 3.5.0 WP_Object_Cache::reset()
262
* @see WP_Object_Cache::reset()
241
* @global WP_Object_Cache $wp_object_cache
264
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
243
266
function wp_cache_reset() {
244
267
_deprecated_function( __FUNCTION__, '3.5' );
285
308
private $cache_hits = 0;
288
* Amount of times the cache did not have the request in cache
311
* Amount of times the cache did not have the request in cache.
294
317
public $cache_misses = 0;
297
* List of global groups
320
* List of global cache groups.
303
326
protected $global_groups = array();
306
329
* The blog prefix to prepend to keys in non-global groups.
312
335
private $blog_prefix;
315
* Holds the value of `is_multisite()`
338
* Holds the value of is_multisite().
321
344
private $multisite;
324
* Make private properties readable for backwards compatibility.
347
* Makes private properties readable for backwards compatibility.
376
399
* Adds data to the cache if it doesn't already exist.
378
* @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
379
* @uses WP_Object_Cache::set Sets the data after the checking the cache
380
* contents existence.
384
* @param int|string $key What to call the contents in the cache
385
* @param mixed $data The contents to store in the cache
386
* @param string $group Where to group the cache contents
387
* @param int $expire When to expire the cache contents
404
* @uses WP_Object_Cache::_exists() Checks to see if the cache already has data.
405
* @uses WP_Object_Cache::set() Sets the data after the checking the cache
406
* contents existence.
408
* @param int|string $key What to call the contents in the cache.
409
* @param mixed $data The contents to store in the cache.
410
* @param string $group Optional. Where to group the cache contents. Default 'default'.
411
* @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
388
412
* @return bool False if cache key and group already exist, true on success
390
414
public function add( $key, $data, $group = 'default', $expire = 0 ) {
422
* Decrement numeric cache item's value
447
* Decrements numeric cache item's value.
426
* @param int|string $key The cache key to increment
427
* @param int $offset The amount by which to decrement the item's value. Default is 1.
428
* @param string $group The group the key is in.
452
* @param int|string $key The cache key to decrement.
453
* @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
454
* @param string $group Optional. The group the key is in. Default 'default'.
429
455
* @return false|int False on failure, the item's new value on success.
431
457
public function decr( $key, $offset = 1, $group = 'default' ) {
455
* Remove the contents of the cache key in the group
481
* Removes the contents of the cache key in the group.
457
483
* If the cache key does not exist in the group, then nothing will happen.
461
* @param int|string $key What the contents in the cache are called
462
* @param string $group Where the cache contents are grouped
463
* @param bool $deprecated Deprecated.
465
* @return bool False if the contents weren't deleted and true on success
488
* @param int|string $key What the contents in the cache are called.
489
* @param string $group Optional. Where the cache contents are grouped. Default 'default'.
490
* @param bool $deprecated Optional. Unused. Default false.
491
* @return bool False if the contents weren't deleted and true on success.
467
493
public function delete( $key, $group = 'default', $deprecated = false ) {
468
494
if ( empty( $group ) )
482
* Clears the object cache of all data
508
* Clears the object cache of all data.
486
* @return true Always returns true
513
* @return true Always returns true.
488
515
public function flush() {
489
516
$this->cache = array();
495
* Retrieves the cache contents, if it exists
522
* Retrieves the cache contents, if it exists.
497
524
* The contents will be first attempted to be retrieved by searching by the
498
525
* key in the cache group. If the cache is hit (success) then the contents
501
528
* On failure, the number of cache misses will be incremented.
505
* @param int|string $key What the contents in the cache are called
506
* @param string $group Where the cache contents are grouped
507
* @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
508
* @return false|mixed False on failure to retrieve contents or the cache
509
* contents on success
533
* @param int|string $key What the contents in the cache are called.
534
* @param string $group Optional. Where the cache contents are grouped. Default 'default'.
535
* @param string $force Optional. Unused. Whether to force a refetch rather than relying on the local
536
* cache. Default false.
537
* @param bool &$found Optional. Whether the key was found in the cache. Disambiguates a return of
538
* false, a storable value. Passed by reference. Default null.
539
* @return false|mixed False on failure to retrieve contents or the cache contents on success.
511
541
public function get( $key, $group = 'default', $force = false, &$found = null ) {
512
542
if ( empty( $group ) )
533
* Increment numeric cache item's value
563
* Increments numeric cache item's value.
537
* @param int|string $key The cache key to increment
538
* @param int $offset The amount by which to increment the item's value. Default is 1.
539
* @param string $group The group the key is in.
568
* @param int|string $key The cache key to increment
569
* @param int $offset Optional. The amount by which to increment the item's value. Default 1.
570
* @param string $group Optional. The group the key is in. Default 'default'.
540
571
* @return false|int False on failure, the item's new value on success.
542
573
public function incr( $key, $offset = 1, $group = 'default' ) {
566
* Replace the contents in the cache, if contents already exist
597
* Replaces the contents in the cache, if contents already exist.
569
602
* @see WP_Object_Cache::set()
571
* @param int|string $key What to call the contents in the cache
572
* @param mixed $data The contents to store in the cache
573
* @param string $group Where to group the cache contents
574
* @param int $expire When to expire the cache contents
575
* @return bool False if not exists, true if contents were replaced
604
* @param int|string $key What to call the contents in the cache.
605
* @param mixed $data The contents to store in the cache.
606
* @param string $group Optional. Where to group the cache contents. Default 'default'.
607
* @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
608
* @return bool False if not exists, true if contents were replaced.
577
610
public function replace( $key, $data, $group = 'default', $expire = 0 ) {
578
611
if ( empty( $group ) )
608
* Sets the data contents into the cache
644
* Sets the data contents into the cache.
610
646
* The cache contents is grouped by the $group parameter followed by the
611
647
* $key. This allows for duplicate ids in unique groups. Therefore, naming of
617
653
* more for cache plugins which use files.
621
* @param int|string $key What to call the contents in the cache
622
* @param mixed $data The contents to store in the cache
623
* @param string $group Where to group the cache contents
624
* @param int $expire Not Used
625
* @return true Always returns true
658
* @param int|string $key What to call the contents in the cache.
659
* @param mixed $data The contents to store in the cache.
660
* @param string $group Optional. Where to group the cache contents. Default 'default'.
661
* @param int $expire Not Used.
662
* @return true Always returns true.
627
664
public function set( $key, $data, $group = 'default', $expire = 0 ) {
628
665
if ( empty( $group ) )
655
693
foreach ($this->cache as $group => $cache) {
656
echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
694
echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
662
* Switch the interal blog id.
700
* Switches the interal blog ID.
664
* This changes the blog id used to create keys in blog specific groups.
702
* This changes the blog ID used to create keys in blog specific groups.
668
* @param int $blog_id Blog ID
707
* @param int $blog_id Blog ID.
670
709
public function switch_to_blog( $blog_id ) {
671
710
$blog_id = (int) $blog_id;
676
* Utility function to determine whether a key exists in the cache.
715
* Serves as a utility function to determine whether a key exists in the cache.
682
* @param string $group
720
* @param int|string $key Cache key to check for existence.
721
* @param string $group Cache group for the key existence check.
722
* @return bool Whether the key exists in the cache for the given group.
685
724
protected function _exists( $key, $group ) {
686
725
return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
690
* Sets up object properties; PHP 5 style constructor
729
* Sets up object properties; PHP 5 style constructor.
694
* @global int $blog_id
733
* @global int $blog_id Global blog ID.
696
735
public function __construct() {
711
* Will save the object cache before object is completely destroyed.
750
* Saves the object cache before object is completely destroyed.
713
752
* Called upon object destruction, which should be when PHP ends.
717
* @return true True value. Won't be used by PHP
756
* @return true Always returns true.
719
758
public function __destruct() {