5 * @link https://codex.wordpress.org/Function_Reference/WP_Cache
12 * Adds data to the cache, if the cache key doesn't already exist.
16 * @global WP_Object_Cache $wp_object_cache
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
24 function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
25 global $wp_object_cache;
27 return $wp_object_cache->add( $key, $data, $group, (int) $expire );
33 * 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.
40 * @return true Always returns True
42 function wp_cache_close() {
47 * Decrement numeric cache item's value
51 * @global WP_Object_Cache $wp_object_cache
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.
56 * @return false|int False on failure, the item's new value on success.
58 function wp_cache_decr( $key, $offset = 1, $group = '' ) {
59 global $wp_object_cache;
61 return $wp_object_cache->decr( $key, $offset, $group );
65 * Removes the cache contents matching key and group.
69 * @global WP_Object_Cache $wp_object_cache
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
75 function wp_cache_delete($key, $group = '') {
76 global $wp_object_cache;
78 return $wp_object_cache->delete($key, $group);
82 * Removes all cache items.
86 * @global WP_Object_Cache $wp_object_cache
88 * @return bool False on failure, true on success
90 function wp_cache_flush() {
91 global $wp_object_cache;
93 return $wp_object_cache->flush();
97 * Retrieves the cache contents from the cache by key and group.
101 * @global WP_Object_Cache $wp_object_cache
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.
107 * @return bool|mixed False on failure to retrieve contents or the cache
108 * contents on success
110 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
111 global $wp_object_cache;
113 return $wp_object_cache->get( $key, $group, $force, $found );
117 * Increment numeric cache item's value
121 * @global WP_Object_Cache $wp_object_cache
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.
126 * @return false|int False on failure, the item's new value on success.
128 function wp_cache_incr( $key, $offset = 1, $group = '' ) {
129 global $wp_object_cache;
131 return $wp_object_cache->incr( $key, $offset, $group );
135 * Sets up Object Cache Global and assigns it.
139 * @global WP_Object_Cache $wp_object_cache
141 function wp_cache_init() {
142 $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
146 * Replaces the contents of the cache with new data.
150 * @global WP_Object_Cache $wp_object_cache
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
158 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
159 global $wp_object_cache;
161 return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
165 * Saves the data to the cache.
169 * @global WP_Object_Cache $wp_object_cache
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
175 * @return bool False on failure, true on success
177 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
178 global $wp_object_cache;
180 return $wp_object_cache->set( $key, $data, $group, (int) $expire );
184 * Switch the interal blog id.
186 * This changes the blog id used to create keys in blog specific groups.
190 * @global WP_Object_Cache $wp_object_cache
192 * @param int $blog_id Blog ID
194 function wp_cache_switch_to_blog( $blog_id ) {
195 global $wp_object_cache;
197 $wp_object_cache->switch_to_blog( $blog_id );
201 * Adds a group or set of groups to the list of global groups.
205 * @global WP_Object_Cache $wp_object_cache
207 * @param string|array $groups A group or an array of groups to add
209 function wp_cache_add_global_groups( $groups ) {
210 global $wp_object_cache;
212 $wp_object_cache->add_global_groups( $groups );
216 * Adds a group or set of groups to the list of non-persistent groups.
220 * @param string|array $groups A group or an array of groups to add
222 function wp_cache_add_non_persistent_groups( $groups ) {
223 // 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.
232 * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
233 * function when preparing the cache for a blog switch. For clearing the cache
234 * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
235 * recommended outside of unit tests as the performance penality for using it is
241 * @global WP_Object_Cache $wp_object_cache
243 function wp_cache_reset() {
244 _deprecated_function( __FUNCTION__, '3.5' );
246 global $wp_object_cache;
248 $wp_object_cache->reset();
252 * WordPress Object Cache
254 * The WordPress Object Cache is used to save on trips to the database. The
255 * Object Cache stores all of the cache data to memory and makes the cache
256 * contents available by using a key, which is used to name and later retrieve
257 * the cache contents.
259 * The Object Cache can be replaced by other caching mechanisms by placing files
260 * in the wp-content folder which is looked at in wp-settings. If that file
261 * exists, then this file will not be included.
267 class WP_Object_Cache {
270 * Holds the cached objects
276 private $cache = array();
279 * The amount of times the cache data was already stored in the cache.
285 private $cache_hits = 0;
288 * Amount of times the cache did not have the request in cache
294 public $cache_misses = 0;
297 * List of global groups
303 protected $global_groups = array();
306 * The blog prefix to prepend to keys in non-global groups.
312 private $blog_prefix;
315 * Holds the value of `is_multisite()`
324 * Make private properties readable for backwards compatibility.
329 * @param string $name Property to get.
330 * @return mixed Property.
332 public function __get( $name ) {
337 * Make private properties settable for backwards compatibility.
342 * @param string $name Property to set.
343 * @param mixed $value Property value.
344 * @return mixed Newly-set property.
346 public function __set( $name, $value ) {
347 return $this->$name = $value;
351 * Make private properties checkable for backwards compatibility.
356 * @param string $name Property to check if set.
357 * @return bool Whether the property is set.
359 public function __isset( $name ) {
360 return isset( $this->$name );
364 * Make private properties un-settable for backwards compatibility.
369 * @param string $name Property to unset.
371 public function __unset( $name ) {
372 unset( $this->$name );
376 * 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
388 * @return bool False if cache key and group already exist, true on success
390 public function add( $key, $data, $group = 'default', $expire = 0 ) {
391 if ( wp_suspend_cache_addition() )
394 if ( empty( $group ) )
398 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
399 $id = $this->blog_prefix . $key;
401 if ( $this->_exists( $id, $group ) )
404 return $this->set( $key, $data, $group, (int) $expire );
408 * Sets the list of global groups.
412 * @param array $groups List of groups that are global.
414 public function add_global_groups( $groups ) {
415 $groups = (array) $groups;
417 $groups = array_fill_keys( $groups, true );
418 $this->global_groups = array_merge( $this->global_groups, $groups );
422 * Decrement 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.
429 * @return false|int False on failure, the item's new value on success.
431 public function decr( $key, $offset = 1, $group = 'default' ) {
432 if ( empty( $group ) )
435 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
436 $key = $this->blog_prefix . $key;
438 if ( ! $this->_exists( $key, $group ) )
441 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
442 $this->cache[ $group ][ $key ] = 0;
444 $offset = (int) $offset;
446 $this->cache[ $group ][ $key ] -= $offset;
448 if ( $this->cache[ $group ][ $key ] < 0 )
449 $this->cache[ $group ][ $key ] = 0;
451 return $this->cache[ $group ][ $key ];
455 * Remove the contents of the cache key in the group
457 * 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
467 public function delete( $key, $group = 'default', $deprecated = false ) {
468 if ( empty( $group ) )
471 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
472 $key = $this->blog_prefix . $key;
474 if ( ! $this->_exists( $key, $group ) )
477 unset( $this->cache[$group][$key] );
482 * Clears the object cache of all data
486 * @return true Always returns true
488 public function flush() {
489 $this->cache = array();
495 * Retrieves the cache contents, if it exists
497 * The contents will be first attempted to be retrieved by searching by the
498 * key in the cache group. If the cache is hit (success) then the contents
501 * 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
511 public function get( $key, $group = 'default', $force = false, &$found = null ) {
512 if ( empty( $group ) )
515 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
516 $key = $this->blog_prefix . $key;
518 if ( $this->_exists( $key, $group ) ) {
520 $this->cache_hits += 1;
521 if ( is_object($this->cache[$group][$key]) )
522 return clone $this->cache[$group][$key];
524 return $this->cache[$group][$key];
528 $this->cache_misses += 1;
533 * Increment 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.
540 * @return false|int False on failure, the item's new value on success.
542 public function incr( $key, $offset = 1, $group = 'default' ) {
543 if ( empty( $group ) )
546 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
547 $key = $this->blog_prefix . $key;
549 if ( ! $this->_exists( $key, $group ) )
552 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
553 $this->cache[ $group ][ $key ] = 0;
555 $offset = (int) $offset;
557 $this->cache[ $group ][ $key ] += $offset;
559 if ( $this->cache[ $group ][ $key ] < 0 )
560 $this->cache[ $group ][ $key ] = 0;
562 return $this->cache[ $group ][ $key ];
566 * Replace the contents in the cache, if contents already exist
569 * @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
577 public function replace( $key, $data, $group = 'default', $expire = 0 ) {
578 if ( empty( $group ) )
582 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
583 $id = $this->blog_prefix . $key;
585 if ( ! $this->_exists( $id, $group ) )
588 return $this->set( $key, $data, $group, (int) $expire );
597 public function reset() {
598 _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' );
600 // Clear out non-global caches since the blog ID has changed.
601 foreach ( array_keys( $this->cache ) as $group ) {
602 if ( ! isset( $this->global_groups[ $group ] ) )
603 unset( $this->cache[ $group ] );
608 * Sets the data contents into the cache
610 * The cache contents is grouped by the $group parameter followed by the
611 * $key. This allows for duplicate ids in unique groups. Therefore, naming of
612 * the group should be used with care and should follow normal function
613 * naming guidelines outside of core WordPress usage.
615 * The $expire parameter is not used, because the cache will automatically
616 * expire for each time a page is accessed and PHP finishes. The method is
617 * 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
627 public function set( $key, $data, $group = 'default', $expire = 0 ) {
628 if ( empty( $group ) )
631 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
632 $key = $this->blog_prefix . $key;
634 if ( is_object( $data ) )
637 $this->cache[$group][$key] = $data;
642 * Echoes the stats of the caching.
644 * Gives the cache hits, and cache misses. Also prints every cached group,
649 public function stats() {
651 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
652 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
655 foreach ($this->cache as $group => $cache) {
656 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
662 * Switch the interal blog id.
664 * This changes the blog id used to create keys in blog specific groups.
668 * @param int $blog_id Blog ID
670 public function switch_to_blog( $blog_id ) {
671 $blog_id = (int) $blog_id;
672 $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
676 * Utility function to determine whether a key exists in the cache.
682 * @param string $group
685 protected function _exists( $key, $group ) {
686 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
694 * @global int $blog_id
696 public function __construct() {
699 $this->multisite = is_multisite();
700 $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
704 * @todo This should be moved to the PHP4 style constructor, PHP5
705 * already calls __destruct()
707 register_shutdown_function( array( $this, '__destruct' ) );
711 * Will save the object cache before object is completely destroyed.
713 * Called upon object destruction, which should be when PHP ends.
717 * @return true True value. Won't be used by PHP
719 public function __destruct() {