2005-11-07 22:56:03 +01:00
|
|
|
<?php
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Object Cache API
|
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* @link http://codex.wordpress.org/Function_Reference/WP_Cache
|
|
|
|
*
|
2008-01-03 05:35:47 +01:00
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Cache
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
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
|
2008-01-03 05:35:47 +01:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::add()
|
|
|
|
*
|
2011-09-08 19:19:09 +02:00
|
|
|
* @param int|string $key The cache key to use for retrieval later
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param mixed $data The data to add to the cache store
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param string $group The group to add the cache to
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param int $expire When the cache data should be expired
|
2013-01-22 21:44:30 +01:00
|
|
|
* @return bool False if cache key and group already exist, true on success
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function wp_cache_add($key, $data, $group = '', $expire = 0) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $wp_object_cache->add($key, $data, $group, $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
|
2011-09-03 18:02:41 +02:00
|
|
|
* functionality was removed along with the rest of the persistent cache. This
|
2008-05-25 17:45:05 +02:00
|
|
|
* 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
|
|
|
*
|
|
|
|
* @return bool Always returns True
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Decrement numeric cache item's value
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::decr()
|
|
|
|
*
|
2011-09-08 19:19:09 +02:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-12-14 00:45:31 +01:00
|
|
|
* @param int $offset The amount by which to decrement the item's value. Default is 1.
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 19:39:44 +02:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
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
|
2008-01-03 05:35:47 +01:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::delete()
|
|
|
|
*
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param int|string $key What the contents in the cache are called
|
|
|
|
* @param string $group Where the cache contents are grouped
|
2008-01-03 05:35:47 +01:00
|
|
|
* @return bool True on successful removal, false on failure
|
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function wp_cache_delete($key, $group = '') {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 23:13:11 +02: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
|
2008-01-03 05:35:47 +01:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::flush()
|
|
|
|
*
|
2013-01-22 21:44:30 +01:00
|
|
|
* @return bool False on failure, true on success
|
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
|
2008-01-03 05:35:47 +01:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::get()
|
|
|
|
*
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param int|string $key What the contents in the cache are called
|
|
|
|
* @param string $group Where the cache contents are grouped
|
2011-09-09 21:59:44 +02:00
|
|
|
* @param bool $force Whether to force an update of the local cache from the persistent cache (default is false)
|
2012-03-02 22:10:37 +01:00
|
|
|
* @param &bool $found Whether key was found in the cache. Disambiguates a return of false, a storable value.
|
2008-05-25 17:45:05 +02:00
|
|
|
* @return bool|mixed False on failure to retrieve contents or the cache
|
|
|
|
* 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
|
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::incr()
|
|
|
|
*
|
2011-09-08 19:19:09 +02:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-12-14 00:45:31 +01:00
|
|
|
* @param int $offset The amount by which to increment the item's value. Default is 1.
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 19:39:44 +02:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
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
|
2008-01-03 05:35:47 +01:00
|
|
|
* @global WP_Object_Cache $wp_object_cache WordPress Object Cache
|
|
|
|
*/
|
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
|
2008-01-03 05:35:47 +01:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::replace()
|
|
|
|
*
|
2010-09-07 13:21:11 +02:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param mixed $data The contents to store in the cache
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param string $group Where to group the cache contents
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param int $expire When to expire the cache contents
|
2013-01-22 21:44:30 +01:00
|
|
|
* @return bool False if not exists, true if contents were replaced
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function wp_cache_replace($key, $data, $group = '', $expire = 0) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $wp_object_cache->replace($key, $data, $group, $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
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::set()
|
|
|
|
*
|
2010-09-07 13:21:11 +02:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param mixed $data The contents to store in the cache
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param string $group Where to group the cache contents
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param int $expire When to expire the cache contents
|
2013-01-22 21:44:30 +01:00
|
|
|
* @return bool False on failure, true on success
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function wp_cache_set($key, $data, $group = '', $expire = 0) {
|
2005-11-07 22:56:03 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $wp_object_cache->set($key, $data, $group, $expire);
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
/**
|
|
|
|
* Switch the interal blog id.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2012-08-02 20:31:14 +02:00
|
|
|
* @param int $blog_id Blog ID
|
|
|
|
*/
|
|
|
|
function wp_cache_switch_to_blog( $blog_id ) {
|
|
|
|
global $wp_object_cache;
|
|
|
|
|
|
|
|
return $wp_object_cache->switch_to_blog( $blog_id );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param string|array $groups A group or an array of groups to add
|
|
|
|
*/
|
|
|
|
function wp_cache_add_global_groups( $groups ) {
|
2010-02-12 18:06:43 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
return $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
|
|
|
*
|
|
|
|
* @param string|array $groups A group or an array of groups to add
|
|
|
|
*/
|
|
|
|
function wp_cache_add_non_persistent_groups( $groups ) {
|
|
|
|
// Default cache doesn't persist so nothing to do here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
/**
|
2012-10-04 17:32:55 +02:00
|
|
|
* Reset internal cache keys and structures. If the cache backend uses global
|
|
|
|
* blog or site IDs as part of its cache keys, this function instructs the
|
|
|
|
* backend to reset those keys and perform any cleanup since blog or site IDs
|
|
|
|
* have changed since cache init.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* recommended outside of unit tests as the performance penality for using it is
|
|
|
|
* high.
|
2010-02-12 18:06:43 +01:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2012-08-02 20:31:14 +02:00
|
|
|
* @deprecated 3.5.0
|
2010-02-12 18:06:43 +01:00
|
|
|
*/
|
|
|
|
function wp_cache_reset() {
|
2012-10-04 17:32:55 +02:00
|
|
|
_deprecated_function( __FUNCTION__, '3.5' );
|
2012-08-02 20:31:14 +02:00
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
|
|
|
return $wp_object_cache->reset();
|
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* WordPress Object Cache
|
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* The WordPress Object Cache is used to save on trips to the database. The
|
|
|
|
* Object Cache stores all of the cache data to memory and makes the cache
|
|
|
|
* contents available by using a key, which is used to name and later retrieve
|
|
|
|
* the cache contents.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* The Object Cache can be replaced by other caching mechanisms by placing files
|
|
|
|
* in the wp-content folder which is looked at in wp-settings. If that file
|
|
|
|
* exists, then this file will not be included.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Cache
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2005-11-07 22:56:03 +01:00
|
|
|
class WP_Object_Cache {
|
2008-01-03 05:35:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the cached objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2005-11-07 22:56:03 +01:00
|
|
|
var $cache = array ();
|
2008-01-03 05:35:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount of times the cache data was already stored in the cache.
|
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.5.0
|
2008-01-03 05:35:47 +01:00
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-01-02 23:13:16 +01:00
|
|
|
var $cache_hits = 0;
|
2008-01-03 05:35:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Amount of times the cache did not have the request in cache
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access public
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2005-11-07 22:56:03 +01:00
|
|
|
var $cache_misses = 0;
|
2006-01-14 01:05:22 +01:00
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
/**
|
|
|
|
* List of global groups
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
var $global_groups = array();
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
/**
|
|
|
|
* The blog prefix to prepend to keys in non-global groups.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access private
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
var $blog_prefix;
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Adds data to the cache if it doesn't already exist.
|
|
|
|
*
|
2012-03-02 22:10:37 +01:00
|
|
|
* @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
|
2008-05-25 17:45:05 +02:00
|
|
|
* @uses WP_Object_Cache::set Sets the data after the checking the cache
|
2011-09-03 18:02:41 +02:00
|
|
|
* contents existence.
|
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
|
|
|
*
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param mixed $data The contents to store in the cache
|
|
|
|
* @param string $group Where to group the cache contents
|
|
|
|
* @param int $expire When to expire the cache contents
|
2011-09-08 19:19:09 +02:00
|
|
|
* @return bool False if cache key and group already exist, true on success
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function add( $key, $data, $group = 'default', $expire = '' ) {
|
2011-09-15 22:23:00 +02:00
|
|
|
if ( wp_suspend_cache_addition() )
|
|
|
|
return false;
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( empty( $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
$group = 'default';
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
$id = $key;
|
|
|
|
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
$id = $this->blog_prefix . $key;
|
|
|
|
|
|
|
|
if ( $this->_exists( $id, $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $this->set($key, $data, $group, $expire);
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
/**
|
|
|
|
* Sets the list of global groups.
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param array $groups List of groups that are global.
|
|
|
|
*/
|
|
|
|
function add_global_groups( $groups ) {
|
|
|
|
$groups = (array) $groups;
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
$groups = array_fill_keys( $groups, true );
|
|
|
|
$this->global_groups = array_merge( $this->global_groups, $groups );
|
2010-02-12 18:06:43 +01:00
|
|
|
}
|
|
|
|
|
2011-08-22 19:39:44 +02:00
|
|
|
/**
|
|
|
|
* Decrement numeric cache item's value
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
|
|
|
*
|
2011-09-08 19:19:09 +02:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-12-14 00:45:31 +01:00
|
|
|
* @param int $offset The amount by which to decrement the item's value. Default is 1.
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 19:39:44 +02:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function decr( $key, $offset = 1, $group = 'default' ) {
|
2012-07-20 22:15:25 +02:00
|
|
|
if ( empty( $group ) )
|
|
|
|
$group = 'default';
|
2012-08-02 20:31:14 +02:00
|
|
|
|
|
|
|
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
$key = $this->blog_prefix . $key;
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( ! $this->_exists( $key, $group ) )
|
2011-08-22 19:39:44 +02:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 19:39:44 +02:00
|
|
|
|
|
|
|
$offset = (int) $offset;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
$this->cache[ $group ][ $key ] -= $offset;
|
2011-08-22 19:39:44 +02:00
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
if ( $this->cache[ $group ][ $key ] < 0 )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 19:39:44 +02:00
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $this->cache[ $group ][ $key ];
|
2011-08-22 19:39:44 +02:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2011-09-08 19:19:09 +02:00
|
|
|
* Remove the contents of the cache key in the group
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2011-09-08 19:19:09 +02:00
|
|
|
* If the cache key does not exist in the group and $force parameter is set
|
2008-05-25 17:45:05 +02:00
|
|
|
* to false, then nothing will happen. The $force parameter is set to false
|
|
|
|
* by default.
|
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
|
|
|
*
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param int|string $key What the contents in the cache are called
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param string $group Where the cache contents are grouped
|
2008-05-25 17:45:05 +02:00
|
|
|
* @param bool $force Optional. Whether to force the unsetting of the cache
|
2011-09-08 19:19:09 +02:00
|
|
|
* key in the group
|
2008-01-03 05:35:47 +01:00
|
|
|
* @return bool False if the contents weren't deleted and true on success
|
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function delete($key, $group = 'default', $force = false) {
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( empty( $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
$group = 'default';
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
$key = $this->blog_prefix . $key;
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( ! $force && ! $this->_exists( $key, $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
return false;
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
unset( $this->cache[$group][$key] );
|
2005-11-07 22:56:03 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Clears the object cache of all data
|
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
|
|
|
* @return bool Always returns true
|
|
|
|
*/
|
2005-11-14 23:10:28 +01:00
|
|
|
function flush() {
|
2005-11-14 23:32:03 +01:00
|
|
|
$this->cache = array ();
|
2006-01-14 01:05:22 +01:00
|
|
|
|
2005-11-14 23:32:03 +01:00
|
|
|
return true;
|
2005-11-14 23:10:28 +01:00
|
|
|
}
|
2005-11-14 23:32:03 +01:00
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the cache contents, if it exists
|
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* The contents will be first attempted to be retrieved by searching by the
|
2011-09-08 19:19:09 +02:00
|
|
|
* key in the cache group. If the cache is hit (success) then the contents
|
2008-05-25 17:45:05 +02:00
|
|
|
* are returned.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2011-09-15 22:28:06 +02:00
|
|
|
* On failure, the number of cache misses will be incremented.
|
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
|
|
|
*
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param int|string $key What the contents in the cache are called
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param string $group Where the cache contents are grouped
|
2011-09-09 21:59:44 +02:00
|
|
|
* @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
|
2008-05-25 17:45:05 +02:00
|
|
|
* @return bool|mixed False on failure to retrieve contents or the cache
|
|
|
|
* contents on success
|
2008-01-03 05:35:47 +01:00
|
|
|
*/
|
2012-03-02 22:10:37 +01:00
|
|
|
function get( $key, $group = 'default', $force = false, &$found = null ) {
|
|
|
|
if ( empty( $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
$group = 'default';
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
$key = $this->blog_prefix . $key;
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( $this->_exists( $key, $group ) ) {
|
|
|
|
$found = true;
|
2008-01-02 23:13:16 +01:00
|
|
|
$this->cache_hits += 1;
|
2011-09-06 23:13:11 +02:00
|
|
|
if ( is_object($this->cache[$group][$key]) )
|
|
|
|
return clone $this->cache[$group][$key];
|
2008-11-17 22:01:12 +01:00
|
|
|
else
|
2011-09-06 23:13:11 +02:00
|
|
|
return $this->cache[$group][$key];
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
$found = false;
|
2008-01-02 23:13:16 +01:00
|
|
|
$this->cache_misses += 1;
|
|
|
|
return false;
|
2006-01-14 01:05:22 +01:00
|
|
|
}
|
|
|
|
|
2011-08-22 19:39:44 +02:00
|
|
|
/**
|
|
|
|
* Increment numeric cache item's value
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
|
|
|
*
|
2011-09-08 19:19:09 +02:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-12-14 00:45:31 +01:00
|
|
|
* @param int $offset The amount by which to increment the item's value. Default is 1.
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 19:39:44 +02:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function incr( $key, $offset = 1, $group = 'default' ) {
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( empty( $group ) )
|
|
|
|
$group = 'default';
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
$key = $this->blog_prefix . $key;
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( ! $this->_exists( $key, $group ) )
|
2011-08-22 19:39:44 +02:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 19:39:44 +02:00
|
|
|
|
|
|
|
$offset = (int) $offset;
|
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
$this->cache[ $group ][ $key ] += $offset;
|
2011-08-22 19:39:44 +02:00
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
if ( $this->cache[ $group ][ $key ] < 0 )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 19:39:44 +02:00
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
return $this->cache[ $group ][ $key ];
|
2011-08-22 19:39:44 +02:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Replace the contents in the cache, if contents already exist
|
|
|
|
*
|
2008-08-27 08:45:13 +02:00
|
|
|
* @since 2.0.0
|
2008-01-03 05:35:47 +01:00
|
|
|
* @see WP_Object_Cache::set()
|
|
|
|
*
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param mixed $data The contents to store in the cache
|
|
|
|
* @param string $group Where to group the cache contents
|
|
|
|
* @param int $expire When to expire the cache contents
|
|
|
|
* @return bool False if not exists, true if contents were replaced
|
|
|
|
*/
|
2012-08-02 20:31:14 +02:00
|
|
|
function replace( $key, $data, $group = 'default', $expire = '' ) {
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( empty( $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
$group = 'default';
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
$id = $key;
|
|
|
|
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
$id = $this->blog_prefix . $key;
|
|
|
|
|
|
|
|
if ( ! $this->_exists( $id, $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
return false;
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
return $this->set( $key, $data, $group, $expire );
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
/**
|
|
|
|
* Reset keys
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
2012-08-02 20:31:14 +02:00
|
|
|
* @deprecated 3.5.0
|
2010-02-12 18:06:43 +01:00
|
|
|
*/
|
|
|
|
function reset() {
|
2012-08-02 20:31:14 +02:00
|
|
|
_deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' );
|
|
|
|
|
2010-02-12 18:06:43 +01:00
|
|
|
// Clear out non-global caches since the blog ID has changed.
|
2012-08-02 20:31:14 +02:00
|
|
|
foreach ( array_keys( $this->cache ) as $group ) {
|
|
|
|
if ( ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
unset( $this->cache[ $group ] );
|
2010-02-12 18:06:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Sets the data contents into the cache
|
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* The cache contents is grouped by the $group parameter followed by the
|
2011-09-06 23:13:11 +02:00
|
|
|
* $key. This allows for duplicate ids in unique groups. Therefore, naming of
|
2008-05-25 17:45:05 +02:00
|
|
|
* the group should be used with care and should follow normal function
|
|
|
|
* naming guidelines outside of core WordPress usage.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* The $expire parameter is not used, because the cache will automatically
|
|
|
|
* expire for each time a page is accessed and PHP finishes. The method is
|
|
|
|
* more for cache plugins which use files.
|
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
|
|
|
*
|
2011-09-06 23:13:11 +02:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-03 05:35:47 +01:00
|
|
|
* @param mixed $data The contents to store in the cache
|
|
|
|
* @param string $group Where to group the cache contents
|
|
|
|
* @param int $expire Not Used
|
|
|
|
* @return bool Always returns true
|
|
|
|
*/
|
2011-09-06 23:13:11 +02:00
|
|
|
function set($key, $data, $group = 'default', $expire = '') {
|
2012-03-02 22:10:37 +01:00
|
|
|
if ( empty( $group ) )
|
2005-11-07 22:56:03 +01:00
|
|
|
$group = 'default';
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
|
|
|
|
$key = $this->blog_prefix . $key;
|
|
|
|
|
|
|
|
if ( is_object( $data ) )
|
2011-04-06 19:44:29 +02:00
|
|
|
$data = clone $data;
|
2008-11-17 22:01:12 +01:00
|
|
|
|
2011-09-06 23:13:11 +02:00
|
|
|
$this->cache[$group][$key] = $data;
|
2006-01-14 01:05:22 +01:00
|
|
|
return true;
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
2010-06-06 07:16:32 +02:00
|
|
|
* Echoes the stats of the caching.
|
2008-01-03 05:35:47 +01:00
|
|
|
*
|
2008-05-25 17:45:05 +02:00
|
|
|
* Gives the cache hits, and cache misses. Also prints every cached group,
|
|
|
|
* key and the data.
|
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
|
|
|
*/
|
2005-11-07 22:56:03 +01:00
|
|
|
function stats() {
|
|
|
|
echo "<p>";
|
2008-01-02 23:13:16 +01:00
|
|
|
echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
|
2007-08-14 05:12:24 +02:00
|
|
|
echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
|
2005-11-07 22:56:03 +01:00
|
|
|
echo "</p>";
|
2010-11-11 11:55:06 +01:00
|
|
|
echo '<ul>';
|
2005-11-07 22:56:03 +01:00
|
|
|
foreach ($this->cache as $group => $cache) {
|
2010-11-11 11:55:06 +01:00
|
|
|
echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
2010-11-11 11:55:06 +01:00
|
|
|
echo '</ul>';
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
/**
|
|
|
|
* Switch the interal blog id.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2012-08-02 20:31:14 +02:00
|
|
|
* @param int $blog_id Blog ID
|
|
|
|
*/
|
|
|
|
function switch_to_blog( $blog_id ) {
|
|
|
|
$blog_id = (int) $blog_id;
|
|
|
|
$this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
|
|
|
|
}
|
|
|
|
|
2012-03-02 22:10:37 +01:00
|
|
|
/**
|
|
|
|
* Utility function to determine whether a key exists in the cache.
|
2012-07-20 06:21:42 +02:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @access protected
|
2012-03-02 22:10:37 +01:00
|
|
|
*/
|
2012-07-20 06:21:42 +02:00
|
|
|
protected function _exists( $key, $group ) {
|
|
|
|
return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
|
2012-03-02 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Sets up object properties; PHP 5 style constructor
|
|
|
|
*
|
|
|
|
* @since 2.0.8
|
|
|
|
* @return null|WP_Object_Cache If cache is disabled, returns null.
|
|
|
|
*/
|
2007-01-07 00:36:51 +01:00
|
|
|
function __construct() {
|
2012-08-02 20:31:14 +02:00
|
|
|
global $blog_id;
|
|
|
|
|
|
|
|
$this->multisite = is_multisite();
|
|
|
|
$this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
|
2012-11-17 16:11:29 +01:00
|
|
|
|
2012-08-02 20:31:14 +02:00
|
|
|
|
2008-05-25 17:45:05 +02:00
|
|
|
/**
|
|
|
|
* @todo This should be moved to the PHP4 style constructor, PHP5
|
|
|
|
* already calls __destruct()
|
|
|
|
*/
|
2012-10-04 22:00:16 +02:00
|
|
|
register_shutdown_function( array( $this, '__destruct' ) );
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|
2007-01-07 00:36:51 +01:00
|
|
|
|
2008-01-03 05:35:47 +01:00
|
|
|
/**
|
|
|
|
* Will save the object cache before object is completely destroyed.
|
|
|
|
*
|
|
|
|
* Called upon object destruction, which should be when PHP ends.
|
|
|
|
*
|
|
|
|
* @since 2.0.8
|
|
|
|
*
|
|
|
|
* @return bool True value. Won't be used by PHP
|
|
|
|
*/
|
2007-01-07 00:36:51 +01:00
|
|
|
function __destruct() {
|
2007-02-27 16:24:54 +01:00
|
|
|
return true;
|
2007-01-07 00:36:51 +01:00
|
|
|
}
|
2005-11-07 22:56:03 +01:00
|
|
|
}
|