5 * @link https://codex.wordpress.org/Function_Reference/WP_Cache
12 * Adds data to the cache, if the cache key doesn't already exist.
15 * @uses $wp_object_cache Object Cache Class
16 * @see WP_Object_Cache::add()
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 bool Always returns True
42 function wp_cache_close() {
47 * Decrement numeric cache item's value
50 * @uses $wp_object_cache Object Cache Class
51 * @see WP_Object_Cache::decr()
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.
68 * @uses $wp_object_cache Object Cache Class
69 * @see WP_Object_Cache::delete()
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.
85 * @uses $wp_object_cache Object Cache Class
86 * @see WP_Object_Cache::flush()
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.
100 * @uses $wp_object_cache Object Cache Class
101 * @see WP_Object_Cache::get()
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
120 * @uses $wp_object_cache Object Cache Class
121 * @see WP_Object_Cache::incr()
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.
138 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
140 function wp_cache_init() {
141 $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
145 * Replaces the contents of the cache with new data.
148 * @uses $wp_object_cache Object Cache Class
149 * @see WP_Object_Cache::replace()
151 * @param int|string $key What to call the contents in the cache
152 * @param mixed $data The contents to store in the cache
153 * @param string $group Where to group the cache contents
154 * @param int $expire When to expire the cache contents
155 * @return bool False if not exists, true if contents were replaced
157 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
158 global $wp_object_cache;
160 return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
164 * Saves the data to the cache.
168 * @uses $wp_object_cache Object Cache Class
169 * @see WP_Object_Cache::set()
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 * @param int $blog_id Blog ID
192 function wp_cache_switch_to_blog( $blog_id ) {
193 global $wp_object_cache;
195 return $wp_object_cache->switch_to_blog( $blog_id );
199 * Adds a group or set of groups to the list of global groups.
203 * @param string|array $groups A group or an array of groups to add
205 function wp_cache_add_global_groups( $groups ) {
206 global $wp_object_cache;
208 return $wp_object_cache->add_global_groups( $groups );
212 * Adds a group or set of groups to the list of non-persistent groups.
216 * @param string|array $groups A group or an array of groups to add
218 function wp_cache_add_non_persistent_groups( $groups ) {
219 // Default cache doesn't persist so nothing to do here.
223 * Reset internal cache keys and structures. If the cache backend uses global
224 * blog or site IDs as part of its cache keys, this function instructs the
225 * backend to reset those keys and perform any cleanup since blog or site IDs
226 * have changed since cache init.
228 * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
229 * function when preparing the cache for a blog switch. For clearing the cache
230 * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
231 * recommended outside of unit tests as the performance penality for using it is
237 function wp_cache_reset() {
238 _deprecated_function( __FUNCTION__, '3.5' );
240 global $wp_object_cache;
242 return $wp_object_cache->reset();
246 * WordPress Object Cache
248 * The WordPress Object Cache is used to save on trips to the database. The
249 * Object Cache stores all of the cache data to memory and makes the cache
250 * contents available by using a key, which is used to name and later retrieve
251 * the cache contents.
253 * The Object Cache can be replaced by other caching mechanisms by placing files
254 * in the wp-content folder which is looked at in wp-settings. If that file
255 * exists, then this file will not be included.
261 class WP_Object_Cache {
264 * Holds the cached objects
270 private $cache = array();
273 * The amount of times the cache data was already stored in the cache.
279 private $cache_hits = 0;
282 * Amount of times the cache did not have the request in cache
288 public $cache_misses = 0;
291 * List of global groups
297 protected $global_groups = array();
300 * The blog prefix to prepend to keys in non-global groups.
306 private $blog_prefix;
309 * Holds the value of `is_multisite()`
318 * Make private properties readable for backwards compatibility.
323 * @param string $name Property to get.
324 * @return mixed Property.
326 public function __get( $name ) {
331 * Make private properties settable for backwards compatibility.
336 * @param string $name Property to set.
337 * @param mixed $value Property value.
338 * @return mixed Newly-set property.
340 public function __set( $name, $value ) {
341 return $this->$name = $value;
345 * Make private properties checkable for backwards compatibility.
350 * @param string $name Property to check if set.
351 * @return bool Whether the property is set.
353 public function __isset( $name ) {
354 return isset( $this->$name );
358 * Make private properties un-settable for backwards compatibility.
363 * @param string $name Property to unset.
365 public function __unset( $name ) {
366 unset( $this->$name );
370 * Adds data to the cache if it doesn't already exist.
372 * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
373 * @uses WP_Object_Cache::set Sets the data after the checking the cache
374 * contents existence.
378 * @param int|string $key What to call the contents in the cache
379 * @param mixed $data The contents to store in the cache
380 * @param string $group Where to group the cache contents
381 * @param int $expire When to expire the cache contents
382 * @return bool False if cache key and group already exist, true on success
384 public function add( $key, $data, $group = 'default', $expire = 0 ) {
385 if ( wp_suspend_cache_addition() )
388 if ( empty( $group ) )
392 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
393 $id = $this->blog_prefix . $key;
395 if ( $this->_exists( $id, $group ) )
398 return $this->set( $key, $data, $group, (int) $expire );
402 * Sets the list of global groups.
406 * @param array $groups List of groups that are global.
408 public function add_global_groups( $groups ) {
409 $groups = (array) $groups;
411 $groups = array_fill_keys( $groups, true );
412 $this->global_groups = array_merge( $this->global_groups, $groups );
416 * Decrement numeric cache item's value
420 * @param int|string $key The cache key to increment
421 * @param int $offset The amount by which to decrement the item's value. Default is 1.
422 * @param string $group The group the key is in.
423 * @return false|int False on failure, the item's new value on success.
425 public function decr( $key, $offset = 1, $group = 'default' ) {
426 if ( empty( $group ) )
429 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
430 $key = $this->blog_prefix . $key;
432 if ( ! $this->_exists( $key, $group ) )
435 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
436 $this->cache[ $group ][ $key ] = 0;
438 $offset = (int) $offset;
440 $this->cache[ $group ][ $key ] -= $offset;
442 if ( $this->cache[ $group ][ $key ] < 0 )
443 $this->cache[ $group ][ $key ] = 0;
445 return $this->cache[ $group ][ $key ];
449 * Remove the contents of the cache key in the group
451 * If the cache key does not exist in the group, then nothing will happen.
455 * @param int|string $key What the contents in the cache are called
456 * @param string $group Where the cache contents are grouped
457 * @param bool $deprecated Deprecated.
459 * @return bool False if the contents weren't deleted and true on success
461 public function delete( $key, $group = 'default', $deprecated = false ) {
462 if ( empty( $group ) )
465 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
466 $key = $this->blog_prefix . $key;
468 if ( ! $this->_exists( $key, $group ) )
471 unset( $this->cache[$group][$key] );
476 * Clears the object cache of all data
480 * @return bool Always returns true
482 public function flush() {
483 $this->cache = array ();
489 * Retrieves the cache contents, if it exists
491 * The contents will be first attempted to be retrieved by searching by the
492 * key in the cache group. If the cache is hit (success) then the contents
495 * On failure, the number of cache misses will be incremented.
499 * @param int|string $key What the contents in the cache are called
500 * @param string $group Where the cache contents are grouped
501 * @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
502 * @return bool|mixed False on failure to retrieve contents or the cache
503 * contents on success
505 public function get( $key, $group = 'default', $force = false, &$found = null ) {
506 if ( empty( $group ) )
509 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
510 $key = $this->blog_prefix . $key;
512 if ( $this->_exists( $key, $group ) ) {
514 $this->cache_hits += 1;
515 if ( is_object($this->cache[$group][$key]) )
516 return clone $this->cache[$group][$key];
518 return $this->cache[$group][$key];
522 $this->cache_misses += 1;
527 * Increment numeric cache item's value
531 * @param int|string $key The cache key to increment
532 * @param int $offset The amount by which to increment the item's value. Default is 1.
533 * @param string $group The group the key is in.
534 * @return false|int False on failure, the item's new value on success.
536 public function incr( $key, $offset = 1, $group = 'default' ) {
537 if ( empty( $group ) )
540 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
541 $key = $this->blog_prefix . $key;
543 if ( ! $this->_exists( $key, $group ) )
546 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
547 $this->cache[ $group ][ $key ] = 0;
549 $offset = (int) $offset;
551 $this->cache[ $group ][ $key ] += $offset;
553 if ( $this->cache[ $group ][ $key ] < 0 )
554 $this->cache[ $group ][ $key ] = 0;
556 return $this->cache[ $group ][ $key ];
560 * Replace the contents in the cache, if contents already exist
563 * @see WP_Object_Cache::set()
565 * @param int|string $key What to call the contents in the cache
566 * @param mixed $data The contents to store in the cache
567 * @param string $group Where to group the cache contents
568 * @param int $expire When to expire the cache contents
569 * @return bool False if not exists, true if contents were replaced
571 public function replace( $key, $data, $group = 'default', $expire = 0 ) {
572 if ( empty( $group ) )
576 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
577 $id = $this->blog_prefix . $key;
579 if ( ! $this->_exists( $id, $group ) )
582 return $this->set( $key, $data, $group, (int) $expire );
591 public function reset() {
592 _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' );
594 // Clear out non-global caches since the blog ID has changed.
595 foreach ( array_keys( $this->cache ) as $group ) {
596 if ( ! isset( $this->global_groups[ $group ] ) )
597 unset( $this->cache[ $group ] );
602 * Sets the data contents into the cache
604 * The cache contents is grouped by the $group parameter followed by the
605 * $key. This allows for duplicate ids in unique groups. Therefore, naming of
606 * the group should be used with care and should follow normal function
607 * naming guidelines outside of core WordPress usage.
609 * The $expire parameter is not used, because the cache will automatically
610 * expire for each time a page is accessed and PHP finishes. The method is
611 * more for cache plugins which use files.
615 * @param int|string $key What to call the contents in the cache
616 * @param mixed $data The contents to store in the cache
617 * @param string $group Where to group the cache contents
618 * @param int $expire Not Used
619 * @return bool Always returns true
621 public function set( $key, $data, $group = 'default', $expire = 0 ) {
622 if ( empty( $group ) )
625 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
626 $key = $this->blog_prefix . $key;
628 if ( is_object( $data ) )
631 $this->cache[$group][$key] = $data;
636 * Echoes the stats of the caching.
638 * Gives the cache hits, and cache misses. Also prints every cached group,
643 public function stats() {
645 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
646 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
649 foreach ($this->cache as $group => $cache) {
650 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
656 * Switch the interal blog id.
658 * This changes the blog id used to create keys in blog specific groups.
662 * @param int $blog_id Blog ID
664 public function switch_to_blog( $blog_id ) {
665 $blog_id = (int) $blog_id;
666 $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
670 * Utility function to determine whether a key exists in the cache.
676 * @param string $group
679 protected function _exists( $key, $group ) {
680 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
684 * Sets up object properties; PHP 5 style constructor
688 public function __construct() {
691 $this->multisite = is_multisite();
692 $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
696 * @todo This should be moved to the PHP4 style constructor, PHP5
697 * already calls __destruct()
699 register_shutdown_function( array( $this, '__destruct' ) );
703 * Will save the object cache before object is completely destroyed.
705 * Called upon object destruction, which should be when PHP ends.
709 * @return bool True value. Won't be used by PHP
711 public function __destruct() {