2005-11-07 16:56:03 -05:00
|
|
|
<?php
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
|
|
|
* Object Cache API
|
|
|
|
*
|
2008-05-25 11:45:05 -04:00
|
|
|
* @link http://codex.wordpress.org/Function_Reference/WP_Cache
|
|
|
|
*
|
2008-01-02 23:35:47 -05:00
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Cache
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
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
|
2008-01-02 23:35:47 -05:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::add()
|
|
|
|
*
|
2011-09-08 13:19:09 -04:00
|
|
|
* @param int|string $key The cache key to use for retrieval later
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param mixed $data The data to add to the cache store
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param string $group The group to add the cache to
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param int $expire When the cache data should be expired
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function wp_cache_add($key, $data, $group = '', $expire = 0) {
|
2005-11-07 16:56:03 -05:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
return $wp_object_cache->add($key, $data, $group, $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
|
2011-09-03 12:02:41 -04:00
|
|
|
* functionality was removed along with the rest of the persistent cache. This
|
2008-05-25 11:45:05 -04: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-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
|
|
|
*
|
|
|
|
* @return bool Always returns True
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* 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 13:19:09 -04:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-08-22 13:39:44 -04:00
|
|
|
* @param int $offset The amount by which to decrement the item's value. Default is 1.
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 13:39:44 -04:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
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
|
2008-01-02 23:35:47 -05:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::delete()
|
|
|
|
*
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param int|string $key What the contents in the cache are called
|
|
|
|
* @param string $group Where the cache contents are grouped
|
2008-01-02 23:35:47 -05:00
|
|
|
* @return bool True on successful removal, false on failure
|
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function wp_cache_delete($key, $group = '') {
|
2005-11-07 16:56:03 -05:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 17:13:11 -04: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
|
2008-01-02 23:35:47 -05:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::flush()
|
|
|
|
*
|
|
|
|
* @return bool Always returns true
|
|
|
|
*/
|
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
|
2008-01-02 23:35:47 -05:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::get()
|
|
|
|
*
|
2011-09-06 17:13:11 -04: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 15:59:44 -04:00
|
|
|
* @param bool $force Whether to force an update of the local cache from the persistent cache (default is false)
|
2008-05-25 11:45:05 -04:00
|
|
|
* @return bool|mixed False on failure to retrieve contents or the cache
|
|
|
|
* contents on success
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2011-09-09 15:59:44 -04:00
|
|
|
function wp_cache_get( $key, $group = '', $force = false ) {
|
2005-11-07 16:56:03 -05:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-09 15:59:44 -04:00
|
|
|
return $wp_object_cache->get( $key, $group, $force );
|
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
|
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::incr()
|
|
|
|
*
|
2011-09-08 13:19:09 -04:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-08-22 13:39:44 -04:00
|
|
|
* @param int $offset The amount by which to increment the item's value. Default is 1.
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 13:39:44 -04:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
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
|
2008-01-02 23:35:47 -05:00
|
|
|
* @global WP_Object_Cache $wp_object_cache WordPress Object Cache
|
|
|
|
*/
|
2005-11-07 16:56:03 -05:00
|
|
|
function wp_cache_init() {
|
2006-11-18 19:12:57 -05: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
|
2008-01-02 23:35:47 -05:00
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::replace()
|
|
|
|
*
|
2010-09-07 07:21:11 -04:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param mixed $data The contents to store in the cache
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param string $group Where to group the cache contents
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param int $expire When to expire the cache contents
|
2011-09-08 13:19:09 -04:00
|
|
|
* @return bool False if cache key and group already exist, true on success
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function wp_cache_replace($key, $data, $group = '', $expire = 0) {
|
2005-11-07 16:56:03 -05:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
return $wp_object_cache->replace($key, $data, $group, $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
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
* @uses $wp_object_cache Object Cache Class
|
|
|
|
* @see WP_Object_Cache::set()
|
|
|
|
*
|
2010-09-07 07:21:11 -04:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param mixed $data The contents to store in the cache
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param string $group Where to group the cache contents
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param int $expire When to expire the cache contents
|
2011-09-08 13:19:09 -04:00
|
|
|
* @return bool False if cache key and group already exist, true on success
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function wp_cache_set($key, $data, $group = '', $expire = 0) {
|
2005-11-07 16:56:03 -05:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
return $wp_object_cache->set($key, $data, $group, $expire);
|
2005-11-07 16:56:03 -05: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
|
|
|
*
|
|
|
|
* @param string|array $groups A group or an array of groups to add
|
|
|
|
*/
|
|
|
|
function wp_cache_add_global_groups( $groups ) {
|
2010-02-12 12:06:43 -05:00
|
|
|
global $wp_object_cache;
|
|
|
|
|
|
|
|
return $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
|
|
|
*
|
|
|
|
* @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 12:06:43 -05: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.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
function wp_cache_reset() {
|
|
|
|
global $wp_object_cache;
|
|
|
|
|
|
|
|
return $wp_object_cache->reset();
|
|
|
|
}
|
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
|
|
|
* WordPress Object Cache
|
|
|
|
*
|
2008-05-25 11:45:05 -04: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-02 23:35:47 -05:00
|
|
|
*
|
2008-05-25 11:45:05 -04: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-02 23:35:47 -05:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Cache
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2005-11-07 16:56:03 -05:00
|
|
|
class WP_Object_Cache {
|
2008-01-02 23:35:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the cached objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
2008-08-27 02:45:13 -04:00
|
|
|
* @since 2.0.0
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2005-11-07 16:56:03 -05:00
|
|
|
var $cache = array ();
|
2008-01-02 23:35:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache objects that do not exist in the cache
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
2008-08-27 02:45:13 -04:00
|
|
|
* @since 2.0.0
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2010-02-11 11:58:32 -05:00
|
|
|
var $non_existent_objects = array ();
|
2008-01-02 23:35:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount of times the cache data was already stored in the cache.
|
|
|
|
*
|
2008-08-27 02:45:13 -04:00
|
|
|
* @since 2.5.0
|
2008-01-02 23:35:47 -05:00
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-01-02 17:13:16 -05:00
|
|
|
var $cache_hits = 0;
|
2008-01-02 23:35:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Amount of times the cache did not have the request in cache
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access public
|
2008-08-27 02:45:13 -04:00
|
|
|
* @since 2.0.0
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2005-11-07 16:56:03 -05:00
|
|
|
var $cache_misses = 0;
|
2006-01-13 19:05:22 -05:00
|
|
|
|
2010-02-12 12:06:43 -05:00
|
|
|
/**
|
|
|
|
* List of global groups
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
var $global_groups = array();
|
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
|
|
|
* Adds data to the cache if it doesn't already exist.
|
|
|
|
*
|
|
|
|
* @uses WP_Object_Cache::get Checks to see if the cache already has data.
|
2008-05-25 11:45:05 -04:00
|
|
|
* @uses WP_Object_Cache::set Sets the data after the checking the cache
|
2011-09-03 12:02:41 -04:00
|
|
|
* contents existence.
|
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
|
|
|
*
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-02 23:35:47 -05: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 13:19:09 -04:00
|
|
|
* @return bool False if cache key and group already exist, true on success
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function add( $key, $data, $group = 'default', $expire = '' ) {
|
2011-09-15 16:23:00 -04:00
|
|
|
if ( wp_suspend_cache_addition() )
|
|
|
|
return false;
|
|
|
|
|
2010-02-12 12:06:43 -05:00
|
|
|
if ( empty ($group) )
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if (false !== $this->get($key, $group))
|
2005-11-07 16:56:03 -05:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
return $this->set($key, $data, $group, $expire);
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
2010-02-12 12:06:43 -05: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;
|
|
|
|
|
|
|
|
$this->global_groups = array_merge($this->global_groups, $groups);
|
|
|
|
$this->global_groups = array_unique($this->global_groups);
|
|
|
|
}
|
|
|
|
|
2011-08-22 13:39:44 -04:00
|
|
|
/**
|
|
|
|
* Decrement numeric cache item's value
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
|
|
|
*
|
2011-09-08 13:19:09 -04:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-08-22 13:39:44 -04:00
|
|
|
* @param int $offset The amount by which to decrement the item's value. Default is 1.
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 13:39:44 -04:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function decr( $key, $offset = 1, $group = 'default' ) {
|
|
|
|
if ( ! isset( $this->cache[ $group ][ $key ] ) )
|
2011-08-22 13:39:44 -04:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 13:39:44 -04:00
|
|
|
|
|
|
|
$offset = (int) $offset;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
$this->cache[ $group ][ $key ] -= $offset;
|
2011-08-22 13:39:44 -04:00
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( $this->cache[ $group ][ $key ] < 0 )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 13:39:44 -04:00
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
return $this->cache[ $group ][ $key ];
|
2011-08-22 13:39:44 -04:00
|
|
|
}
|
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
2011-09-08 13:19:09 -04:00
|
|
|
* Remove the contents of the cache key in the group
|
2008-01-02 23:35:47 -05:00
|
|
|
*
|
2011-09-08 13:19:09 -04:00
|
|
|
* If the cache key does not exist in the group and $force parameter is set
|
2008-05-25 11:45:05 -04:00
|
|
|
* to false, then nothing will happen. The $force parameter is set to false
|
|
|
|
* by default.
|
2008-01-02 23:35:47 -05:00
|
|
|
*
|
2008-08-09 01:36:14 -04:00
|
|
|
* On success the group and the id will be added to the
|
2010-02-11 11:58:32 -05:00
|
|
|
* $non_existent_objects property in the class.
|
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
|
|
|
*
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param int|string $key What the contents in the cache are called
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param string $group Where the cache contents are grouped
|
2008-05-25 11:45:05 -04:00
|
|
|
* @param bool $force Optional. Whether to force the unsetting of the cache
|
2011-09-08 13:19:09 -04:00
|
|
|
* key in the group
|
2008-01-02 23:35:47 -05:00
|
|
|
* @return bool False if the contents weren't deleted and true on success
|
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function delete($key, $group = 'default', $force = false) {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (empty ($group))
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if (!$force && false === $this->get($key, $group))
|
2005-11-07 16:56:03 -05:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
unset ($this->cache[$group][$key]);
|
|
|
|
$this->non_existent_objects[$group][$key] = true;
|
2005-11-07 16:56:03 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
|
|
|
* Clears the object cache of all data
|
|
|
|
*
|
2008-08-27 02:45:13 -04:00
|
|
|
* @since 2.0.0
|
2008-01-02 23:35:47 -05:00
|
|
|
*
|
|
|
|
* @return bool Always returns true
|
|
|
|
*/
|
2005-11-14 17:10:28 -05:00
|
|
|
function flush() {
|
2005-11-14 17:32:03 -05:00
|
|
|
$this->cache = array ();
|
2006-01-13 19:05:22 -05:00
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
return true;
|
2005-11-14 17:10:28 -05:00
|
|
|
}
|
2005-11-14 17:32:03 -05:00
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the cache contents, if it exists
|
|
|
|
*
|
2008-05-25 11:45:05 -04:00
|
|
|
* The contents will be first attempted to be retrieved by searching by the
|
2011-09-08 13:19:09 -04:00
|
|
|
* key in the cache group. If the cache is hit (success) then the contents
|
2008-05-25 11:45:05 -04:00
|
|
|
* are returned.
|
2008-01-02 23:35:47 -05:00
|
|
|
*
|
2010-02-11 11:58:32 -05:00
|
|
|
* On failure, the $non_existent_objects property is checked and if the
|
2011-09-08 13:19:09 -04:00
|
|
|
* cache group and key exist in there the cache misses will not be
|
2010-02-11 11:58:32 -05:00
|
|
|
* incremented. If not in the nonexistent objects property, then the cache
|
2011-09-08 13:19:09 -04:00
|
|
|
* misses will be incremented and the cache group and key will be added to
|
2010-02-11 11:58:32 -05:00
|
|
|
* the nonexistent objects.
|
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
|
|
|
*
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param int|string $key What the contents in the cache are called
|
2008-01-02 23:35:47 -05:00
|
|
|
* @param string $group Where the cache contents are grouped
|
2011-09-09 15:59:44 -04:00
|
|
|
* @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
|
2008-05-25 11:45:05 -04:00
|
|
|
* @return bool|mixed False on failure to retrieve contents or the cache
|
|
|
|
* contents on success
|
2008-01-02 23:35:47 -05:00
|
|
|
*/
|
2011-09-09 15:59:44 -04:00
|
|
|
function get( $key, $group = 'default', $force = false) {
|
2010-02-12 12:06:43 -05:00
|
|
|
if ( empty ($group) )
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( isset ($this->cache[$group][$key]) ) {
|
2008-01-02 17:13:16 -05:00
|
|
|
$this->cache_hits += 1;
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( is_object($this->cache[$group][$key]) )
|
|
|
|
return clone $this->cache[$group][$key];
|
2008-11-17 16:01:12 -05:00
|
|
|
else
|
2011-09-06 17:13:11 -04:00
|
|
|
return $this->cache[$group][$key];
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( isset ($this->non_existent_objects[$group][$key]) )
|
2005-11-07 16:56:03 -05:00
|
|
|
return false;
|
2006-11-19 02:56:05 -05:00
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
$this->non_existent_objects[$group][$key] = true;
|
2008-01-02 17:13:16 -05:00
|
|
|
$this->cache_misses += 1;
|
|
|
|
return false;
|
2006-01-13 19:05:22 -05:00
|
|
|
}
|
|
|
|
|
2011-08-22 13:39:44 -04:00
|
|
|
/**
|
|
|
|
* Increment numeric cache item's value
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
|
|
|
*
|
2011-09-08 13:19:09 -04:00
|
|
|
* @param int|string $key The cache key to increment
|
2011-08-22 13:39:44 -04:00
|
|
|
* @param int $offset The amount by which to increment the item's value. Default is 1.
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param string $group The group the key is in.
|
2011-08-22 13:39:44 -04:00
|
|
|
* @return false|int False on failure, the item's new value on success.
|
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function incr( $key, $offset = 1, $group = 'default' ) {
|
|
|
|
if ( ! isset( $this->cache[ $group ][ $key ] ) )
|
2011-08-22 13:39:44 -04:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 13:39:44 -04:00
|
|
|
|
|
|
|
$offset = (int) $offset;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
$this->cache[ $group ][ $key ] += $offset;
|
2011-08-22 13:39:44 -04:00
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( $this->cache[ $group ][ $key ] < 0 )
|
|
|
|
$this->cache[ $group ][ $key ] = 0;
|
2011-08-22 13:39:44 -04:00
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
return $this->cache[ $group ][ $key ];
|
2011-08-22 13:39:44 -04:00
|
|
|
}
|
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
|
|
|
* Replace the contents in the cache, if contents already exist
|
|
|
|
*
|
2008-08-27 02:45:13 -04:00
|
|
|
* @since 2.0.0
|
2008-01-02 23:35:47 -05:00
|
|
|
* @see WP_Object_Cache::set()
|
|
|
|
*
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-02 23:35:47 -05: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
|
|
|
|
*/
|
2011-09-06 17:13:11 -04:00
|
|
|
function replace($key, $data, $group = 'default', $expire = '') {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (empty ($group))
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( false === $this->get($key, $group) )
|
2005-11-07 16:56:03 -05:00
|
|
|
return false;
|
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
return $this->set($key, $data, $group, $expire);
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
2010-02-12 12:06:43 -05:00
|
|
|
/**
|
|
|
|
* Reset keys
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
function reset() {
|
|
|
|
// Clear out non-global caches since the blog ID has changed.
|
|
|
|
foreach ( array_keys($this->cache) as $group ) {
|
|
|
|
if ( !in_array($group, $this->global_groups) )
|
|
|
|
unset($this->cache[$group]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
|
|
|
* Sets the data contents into the cache
|
|
|
|
*
|
2008-05-25 11:45:05 -04:00
|
|
|
* The cache contents is grouped by the $group parameter followed by the
|
2011-09-06 17:13:11 -04:00
|
|
|
* $key. This allows for duplicate ids in unique groups. Therefore, naming of
|
2008-05-25 11:45:05 -04:00
|
|
|
* the group should be used with care and should follow normal function
|
|
|
|
* naming guidelines outside of core WordPress usage.
|
2008-01-02 23:35:47 -05:00
|
|
|
*
|
2008-05-25 11:45:05 -04: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-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
|
|
|
*
|
2011-09-06 17:13:11 -04:00
|
|
|
* @param int|string $key What to call the contents in the cache
|
2008-01-02 23:35:47 -05: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 17:13:11 -04:00
|
|
|
function set($key, $data, $group = 'default', $expire = '') {
|
2010-02-12 12:06:43 -05:00
|
|
|
if ( empty ($group) )
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2010-02-12 12:06:43 -05:00
|
|
|
if ( NULL === $data )
|
2005-11-09 06:10:34 -05:00
|
|
|
$data = '';
|
|
|
|
|
2008-11-17 16:01:12 -05:00
|
|
|
if ( is_object($data) )
|
2011-04-06 13:44:29 -04:00
|
|
|
$data = clone $data;
|
2008-11-17 16:01:12 -05:00
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
$this->cache[$group][$key] = $data;
|
2008-02-02 13:42:09 -05:00
|
|
|
|
2011-09-06 17:13:11 -04:00
|
|
|
if ( isset($this->non_existent_objects[$group][$key]) )
|
|
|
|
unset ($this->non_existent_objects[$group][$key]);
|
2006-01-13 19:05:22 -05:00
|
|
|
|
|
|
|
return true;
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
2008-01-02 23:35:47 -05:00
|
|
|
/**
|
2010-06-06 01:16:32 -04:00
|
|
|
* Echoes the stats of the caching.
|
2008-01-02 23:35:47 -05:00
|
|
|
*
|
2008-05-25 11:45:05 -04:00
|
|
|
* Gives the cache hits, and cache misses. Also prints every cached group,
|
|
|
|
* key and the data.
|
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
|
|
|
*/
|
2005-11-07 16:56:03 -05:00
|
|
|
function stats() {
|
|
|
|
echo "<p>";
|
2008-01-02 17:13:16 -05:00
|
|
|
echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
|
2007-08-13 23:12:24 -04:00
|
|
|
echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
|
2005-11-07 16:56:03 -05:00
|
|
|
echo "</p>";
|
2010-11-11 05:55:06 -05:00
|
|
|
echo '<ul>';
|
2005-11-07 16:56:03 -05:00
|
|
|
foreach ($this->cache as $group => $cache) {
|
2010-11-11 05:55:06 -05:00
|
|
|
echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
2010-11-11 05:55:06 -05:00
|
|
|
echo '</ul>';
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
2008-01-02 23:35:47 -05: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-06 18:36:51 -05:00
|
|
|
function __construct() {
|
2008-05-25 11:45:05 -04:00
|
|
|
/**
|
|
|
|
* @todo This should be moved to the PHP4 style constructor, PHP5
|
|
|
|
* already calls __destruct()
|
|
|
|
*/
|
|
|
|
register_shutdown_function(array(&$this, "__destruct"));
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
2007-01-06 18:36:51 -05:00
|
|
|
|
2008-01-02 23:35:47 -05: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-06 18:36:51 -05:00
|
|
|
function __destruct() {
|
2007-02-27 10:24:54 -05:00
|
|
|
return true;
|
2007-01-06 18:36:51 -05:00
|
|
|
}
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
2005-12-19 14:14:22 -05:00
|
|
|
?>
|