2005-11-07 22:56:03 +01:00
|
|
|
<?php
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Object Cache API
|
|
|
|
*
|
2016-05-19 00:07:28 +02:00
|
|
|
* @link https://codex.wordpress.org/Class_Reference/WP_Object_Cache
|
2008-05-25 17:45:05 +02:00
|
|
|
*
|
2008-01-03 05:35:47 +01:00
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Cache
|
|
|
|
*/
|
|
|
|
|
2020-02-06 06:53:06 +01:00
|
|
|
/** WP_Object_Cache class */
|
|
|
|
require_once ABSPATH . WPINC . '/class-wp-object-cache.php';
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2011-09-03 18:02:41 +02:00
|
|
|
* Adds data to the cache, if the cache key doesn't already exist.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::add()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
|
|
|
*
|
|
|
|
* @param int|string $key The cache key to use for retrieval later.
|
|
|
|
* @param mixed $data The data to add to the cache.
|
|
|
|
* @param string $group Optional. The group to add the cache to. Enables the same key
|
|
|
|
* to be used across groups. Default empty.
|
|
|
|
* @param int $expire Optional. When the cache data should expire, in seconds.
|
|
|
|
* Default 0 (no expiration).
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return bool True on success, false if cache key and group already exist.
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2013-09-16 19:43:09 +02:00
|
|
|
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2013-09-16 19:43:09 +02:00
|
|
|
return $wp_object_cache->add( $key, $data, $group, (int) $expire );
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2008-05-25 17:45:05 +02:00
|
|
|
* Closes the cache.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* This function has ceased to do anything since WordPress 2.5. The
|
2015-09-16 11:40:25 +02:00
|
|
|
* 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.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @return true Always returns true.
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2005-11-07 22:56:03 +01:00
|
|
|
function wp_cache_close() {
|
2008-01-03 00:03:25 +01:00
|
|
|
return true;
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2011-08-22 19:39:44 +02:00
|
|
|
/**
|
2015-09-16 11:40:25 +02:00
|
|
|
* Decrements numeric cache item's value.
|
2011-08-22 19:39:44 +02:00
|
|
|
*
|
|
|
|
* @since 3.3.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::decr()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2011-08-22 19:39:44 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param int|string $key The cache key to decrement.
|
|
|
|
* @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
|
|
|
|
* @param string $group Optional. The group the key is in. Default empty.
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return int|false The item's new value on success, false on failure.
|
2011-08-22 19:39:44 +02:00
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function wp_cache_decr( $key, $offset = 1, $group = '' ) {
|
2011-08-22 19:39:44 +02:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $wp_object_cache->decr( $key, $offset, $group );
|
2011-08-22 19:39:44 +02:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2011-09-08 19:19:09 +02:00
|
|
|
* Removes the cache contents matching key and group.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::delete()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param int|string $key What the contents in the cache are called.
|
|
|
|
* @param string $group Optional. Where the cache contents are grouped. Default empty.
|
2015-09-10 20:24:24 +02:00
|
|
|
* @return bool True on successful removal, false on failure.
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2015-09-16 11:40:25 +02:00
|
|
|
function wp_cache_delete( $key, $group = '' ) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
return $wp_object_cache->delete( $key, $group );
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2008-05-25 17:45:05 +02:00
|
|
|
* Removes all cache items.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::flush()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return bool True on success, false on failure.
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2005-11-07 22:56:03 +01:00
|
|
|
function wp_cache_flush() {
|
|
|
|
global $wp_object_cache;
|
2005-11-14 23:32:03 +01:00
|
|
|
|
2005-11-14 23:10:28 +01:00
|
|
|
return $wp_object_cache->flush();
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2011-09-08 19:19:09 +02:00
|
|
|
* Retrieves the cache contents from the cache by key and group.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::get()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param int|string $key The key 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.
|
2017-10-03 00:14:46 +02:00
|
|
|
* @param bool $found Optional. Whether the key was found in the cache (passed by reference).
|
|
|
|
* Disambiguates a return of false, a storable value. Default null.
|
2008-05-25 17:45:05 +02:00
|
|
|
* @return bool|mixed False on failure to retrieve contents or the cache
|
2017-12-01 00:11:00 +01:00
|
|
|
* contents on success
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2012-03-02 22:10:37 +01:00
|
|
|
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
return $wp_object_cache->get( $key, $group, $force, $found );
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2011-08-22 19:39:44 +02:00
|
|
|
/**
|
|
|
|
* Increment numeric cache item's value
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::incr()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2011-08-22 19:39:44 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param int|string $key The key for the cache contents that should be incremented.
|
|
|
|
* @param int $offset Optional. The amount by which to increment the item's value. Default 1.
|
|
|
|
* @param string $group Optional. The group the key is in. Default empty.
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return int|false The item's new value on success, false on failure.
|
2011-08-22 19:39:44 +02:00
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function wp_cache_incr( $key, $offset = 1, $group = '' ) {
|
2011-08-22 19:39:44 +02:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $wp_object_cache->incr( $key, $offset, $group );
|
2011-08-22 19:39:44 +02:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2008-05-25 17:45:05 +02:00
|
|
|
* Sets up Object Cache Global and assigns it.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
|
|
|
* @global WP_Object_Cache $wp_object_cache
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2005-11-07 22:56:03 +01:00
|
|
|
function wp_cache_init() {
|
2011-10-18 22:20:59 +02:00
|
|
|
$GLOBALS['wp_object_cache'] = new WP_Object_Cache();
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2008-05-25 17:45:05 +02:00
|
|
|
* Replaces the contents of the cache with new data.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::replace()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param int|string $key The key for the cache data that should be replaced.
|
|
|
|
* @param mixed $data The new data to store in the cache.
|
|
|
|
* @param string $group Optional. The group for the cache data that should be replaced.
|
|
|
|
* Default empty.
|
|
|
|
* @param int $expire Optional. When to expire the cache contents, in seconds.
|
|
|
|
* Default 0 (no expiration).
|
2015-09-10 20:24:24 +02:00
|
|
|
* @return bool False if original value does not exist, true if contents were replaced
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2013-09-16 19:43:09 +02:00
|
|
|
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2013-09-16 19:43:09 +02:00
|
|
|
return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2008-05-25 17:45:05 +02:00
|
|
|
* Saves the data to the cache.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2015-09-10 20:24:24 +02:00
|
|
|
* Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
|
|
|
|
*
|
2013-12-24 19:57:12 +01:00
|
|
|
* @since 2.0.0
|
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::set()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param int|string $key The cache key to use for retrieval later.
|
|
|
|
* @param mixed $data The contents to store in the cache.
|
|
|
|
* @param string $group Optional. Where to group the cache contents. Enables the same key
|
|
|
|
* to be used across groups. Default empty.
|
|
|
|
* @param int $expire Optional. When to expire the cache contents, in seconds.
|
|
|
|
* Default 0 (no expiration).
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return bool True on success, false on failure.
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2013-09-16 19:43:09 +02:00
|
|
|
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2013-09-16 19:43:09 +02:00
|
|
|
return $wp_object_cache->set( $key, $data, $group, (int) $expire );
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
/**
|
2016-04-16 14:37:30 +02:00
|
|
|
* Switches the internal blog ID.
|
2012-08-02 20:31:14 +02:00
|
|
|
*
|
|
|
|
* This changes the blog id used to create keys in blog specific groups.
|
|
|
|
*
|
2012-09-30 16:02:59 +02:00
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::switch_to_blog()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2016-01-28 04:35:27 +01:00
|
|
|
* @param int $blog_id Site ID.
|
2012-08-02 20:31:14 +02:00
|
|
|
*/
|
|
|
|
function wp_cache_switch_to_blog( $blog_id ) {
|
|
|
|
global $wp_object_cache;
|
|
|
|
|
2015-05-21 22:22:24 +02:00
|
|
|
$wp_object_cache->switch_to_blog( $blog_id );
|
2012-08-02 20:31:14 +02:00
|
|
|
}
|
|
|
|
|
2008-05-22 19:28:54 +02:00
|
|
|
/**
|
2008-05-25 17:45:05 +02:00
|
|
|
* Adds a group or set of groups to the list of global groups.
|
2008-05-22 19:28:54 +02:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.6.0
|
2008-05-22 19:28:54 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @see WP_Object_Cache::add_global_groups()
|
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param string|array $groups A group or an array of groups to add.
|
2008-05-22 19:28:54 +02:00
|
|
|
*/
|
|
|
|
function wp_cache_add_global_groups( $groups ) {
|
2010-02-12 18:06:43 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2015-05-21 22:22:24 +02:00
|
|
|
$wp_object_cache->add_global_groups( $groups );
|
2008-05-22 19:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-25 17:45:05 +02:00
|
|
|
* Adds a group or set of groups to the list of non-persistent groups.
|
2008-05-22 19:28:54 +02:00
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.6.0
|
2008-05-22 19:28:54 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @param string|array $groups A group or an array of groups to add.
|
2008-05-22 19:28:54 +02:00
|
|
|
*/
|
|
|
|
function wp_cache_add_non_persistent_groups( $groups ) {
|
|
|
|
// Default cache doesn't persist so nothing to do here.
|
|
|
|
}
|
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
/**
|
2015-09-16 11:40:25 +02:00
|
|
|
* Reset internal cache keys and structures.
|
|
|
|
*
|
2016-02-25 13:53:27 +01:00
|
|
|
* If the cache back end uses global blog or site IDs as part of its cache keys,
|
|
|
|
* this function instructs the back end to reset those keys and perform any cleanup
|
2015-09-16 11:40:25 +02:00
|
|
|
* since blog or site IDs have changed since cache init.
|
2012-10-04 17:32:55 +02:00
|
|
|
*
|
|
|
|
* This function is deprecated. Use wp_cache_switch_to_blog() instead of this
|
|
|
|
* function when preparing the cache for a blog switch. For clearing the cache
|
|
|
|
* during unit tests, consider using wp_cache_init(). wp_cache_init() is not
|
2016-10-31 07:28:32 +01:00
|
|
|
* recommended outside of unit tests as the performance penalty for using it is
|
2012-10-04 17:32:55 +02:00
|
|
|
* high.
|
2010-02-12 18:06:43 +01:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2015-09-16 11:40:25 +02:00
|
|
|
* @deprecated 3.5.0 WP_Object_Cache::reset()
|
|
|
|
* @see WP_Object_Cache::reset()
|
2015-05-22 06:24:26 +02:00
|
|
|
*
|
2015-09-16 11:40:25 +02:00
|
|
|
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
|
2010-02-12 18:06:43 +01:00
|
|
|
*/
|
|
|
|
function wp_cache_reset() {
|
2017-06-25 02:05:44 +02:00
|
|
|
_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()' );
|
2012-08-02 20:31:14 +02:00
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2015-05-21 22:22:24 +02:00
|
|
|
$wp_object_cache->reset();
|
2010-02-12 18:06:43 +01:00
|
|
|
}
|