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