web/wp-includes/cache.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage Cache
     8  * @subpackage Cache
     9  */
     9  */
    10 
    10 
    11 /**
    11 /**
    12  * Adds data to the cache, if the cache key doesn't aleady exist.
    12  * Adds data to the cache, if the cache key doesn't already exist.
    13  *
    13  *
    14  * @since 2.0.0
    14  * @since 2.0.0
    15  * @uses $wp_object_cache Object Cache Class
    15  * @uses $wp_object_cache Object Cache Class
    16  * @see WP_Object_Cache::add()
    16  * @see WP_Object_Cache::add()
    17  *
    17  *
    18  * @param int|string $key The cache ID to use for retrieval later
    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
    19  * @param mixed $data The data to add to the cache store
    20  * @param string $flag The group to add the cache to
    20  * @param string $group The group to add the cache to
    21  * @param int $expire When the cache data should be expired
    21  * @param int $expire When the cache data should be expired
    22  * @return unknown
    22  * @return unknown
    23  */
    23  */
    24 function wp_cache_add($key, $data, $flag = '', $expire = 0) {
    24 function wp_cache_add($key, $data, $group = '', $expire = 0) {
    25 	global $wp_object_cache;
    25 	global $wp_object_cache;
    26 
    26 
    27 	return $wp_object_cache->add($key, $data, $flag, $expire);
    27 	return $wp_object_cache->add($key, $data, $group, $expire);
    28 }
    28 }
    29 
    29 
    30 /**
    30 /**
    31  * Closes the cache.
    31  * Closes the cache.
    32  *
    32  *
    33  * This function has ceased to do anything since WordPress 2.5. The
    33  * This function has ceased to do anything since WordPress 2.5. The
    34  * functionality was removed along with the rest of the persistant cache. This
    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
    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.
    36  * make sure that the cache is cleaned up after WordPress no longer needs it.
    37  *
    37  *
    38  * @since 2.0.0
    38  * @since 2.0.0
    39  *
    39  *
    42 function wp_cache_close() {
    42 function wp_cache_close() {
    43 	return true;
    43 	return true;
    44 }
    44 }
    45 
    45 
    46 /**
    46 /**
    47  * Removes the cache contents matching ID and flag.
    47  * Decrement numeric cache item's value
       
    48  *
       
    49  * @since 3.3.0
       
    50  * @uses $wp_object_cache Object Cache Class
       
    51  * @see WP_Object_Cache::decr()
       
    52  *
       
    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.
       
    57  */
       
    58 function wp_cache_decr( $key, $offset = 1, $group = '' ) {
       
    59 	global $wp_object_cache;
       
    60 
       
    61 	return $wp_object_cache->decr( $key, $offset, $group );
       
    62 }
       
    63 
       
    64 /**
       
    65  * Removes the cache contents matching key and group.
    48  *
    66  *
    49  * @since 2.0.0
    67  * @since 2.0.0
    50  * @uses $wp_object_cache Object Cache Class
    68  * @uses $wp_object_cache Object Cache Class
    51  * @see WP_Object_Cache::delete()
    69  * @see WP_Object_Cache::delete()
    52  *
    70  *
    53  * @param int|string $id What the contents in the cache are called
    71  * @param int|string $key What the contents in the cache are called
    54  * @param string $flag Where the cache contents are grouped
    72  * @param string $group Where the cache contents are grouped
    55  * @return bool True on successful removal, false on failure
    73  * @return bool True on successful removal, false on failure
    56  */
    74  */
    57 function wp_cache_delete($id, $flag = '') {
    75 function wp_cache_delete($key, $group = '') {
    58 	global $wp_object_cache;
    76 	global $wp_object_cache;
    59 
    77 
    60 	return $wp_object_cache->delete($id, $flag);
    78 	return $wp_object_cache->delete($key, $group);
    61 }
    79 }
    62 
    80 
    63 /**
    81 /**
    64  * Removes all cache items.
    82  * Removes all cache items.
    65  *
    83  *
    74 
    92 
    75 	return $wp_object_cache->flush();
    93 	return $wp_object_cache->flush();
    76 }
    94 }
    77 
    95 
    78 /**
    96 /**
    79  * Retrieves the cache contents from the cache by ID and flag.
    97  * Retrieves the cache contents from the cache by key and group.
    80  *
    98  *
    81  * @since 2.0.0
    99  * @since 2.0.0
    82  * @uses $wp_object_cache Object Cache Class
   100  * @uses $wp_object_cache Object Cache Class
    83  * @see WP_Object_Cache::get()
   101  * @see WP_Object_Cache::get()
    84  *
   102  *
    85  * @param int|string $id What the contents in the cache are called
   103  * @param int|string $key What the contents in the cache are called
    86  * @param string $flag Where the cache contents are grouped
   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.
    87  * @return bool|mixed False on failure to retrieve contents or the cache
   107  * @return bool|mixed False on failure to retrieve contents or the cache
    88  *		contents on success
   108  *		contents on success
    89  */
   109  */
    90 function wp_cache_get($id, $flag = '') {
   110 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
    91 	global $wp_object_cache;
   111 	global $wp_object_cache;
    92 
   112 
    93 	return $wp_object_cache->get($id, $flag);
   113 	return $wp_object_cache->get( $key, $group, $force, $found );
       
   114 }
       
   115 
       
   116 /**
       
   117  * Increment numeric cache item's value
       
   118  *
       
   119  * @since 3.3.0
       
   120  * @uses $wp_object_cache Object Cache Class
       
   121  * @see WP_Object_Cache::incr()
       
   122  *
       
   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.
       
   127  */
       
   128 function wp_cache_incr( $key, $offset = 1, $group = '' ) {
       
   129 	global $wp_object_cache;
       
   130 
       
   131 	return $wp_object_cache->incr( $key, $offset, $group );
    94 }
   132 }
    95 
   133 
    96 /**
   134 /**
    97  * Sets up Object Cache Global and assigns it.
   135  * Sets up Object Cache Global and assigns it.
    98  *
   136  *
    99  * @since 2.0.0
   137  * @since 2.0.0
   100  * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
   138  * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
   101  */
   139  */
   102 function wp_cache_init() {
   140 function wp_cache_init() {
   103 	$GLOBALS['wp_object_cache'] =& new WP_Object_Cache();
   141 	$GLOBALS['wp_object_cache'] = new WP_Object_Cache();
   104 }
   142 }
   105 
   143 
   106 /**
   144 /**
   107  * Replaces the contents of the cache with new data.
   145  * Replaces the contents of the cache with new data.
   108  *
   146  *
   109  * @since 2.0.0
   147  * @since 2.0.0
   110  * @uses $wp_object_cache Object Cache Class
   148  * @uses $wp_object_cache Object Cache Class
   111  * @see WP_Object_Cache::replace()
   149  * @see WP_Object_Cache::replace()
   112  *
   150  *
   113  * @param int|string $id What to call the contents in the cache
   151  * @param int|string $key What to call the contents in the cache
   114  * @param mixed $data The contents to store in the cache
   152  * @param mixed $data The contents to store in the cache
   115  * @param string $flag Where to group the cache contents
   153  * @param string $group Where to group the cache contents
   116  * @param int $expire When to expire the cache contents
   154  * @param int $expire When to expire the cache contents
   117  * @return bool False if cache ID and group already exists, true on success
   155  * @return bool False if cache key and group already exist, true on success
   118  */
   156  */
   119 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
   157 function wp_cache_replace($key, $data, $group = '', $expire = 0) {
   120 	global $wp_object_cache;
   158 	global $wp_object_cache;
   121 
   159 
   122 	return $wp_object_cache->replace($key, $data, $flag, $expire);
   160 	return $wp_object_cache->replace($key, $data, $group, $expire);
   123 }
   161 }
   124 
   162 
   125 /**
   163 /**
   126  * Saves the data to the cache.
   164  * Saves the data to the cache.
   127  *
   165  *
   128  * @since 2.0
   166  * @since 2.0
   129  * @uses $wp_object_cache Object Cache Class
   167  * @uses $wp_object_cache Object Cache Class
   130  * @see WP_Object_Cache::set()
   168  * @see WP_Object_Cache::set()
   131  *
   169  *
   132  * @param int|string $id What to call the contents in the cache
   170  * @param int|string $key What to call the contents in the cache
   133  * @param mixed $data The contents to store in the cache
   171  * @param mixed $data The contents to store in the cache
   134  * @param string $flag Where to group the cache contents
   172  * @param string $group Where to group the cache contents
   135  * @param int $expire When to expire the cache contents
   173  * @param int $expire When to expire the cache contents
   136  * @return bool False if cache ID and group already exists, true on success
   174  * @return bool False if cache key and group already exist, true on success
   137  */
   175  */
   138 function wp_cache_set($key, $data, $flag = '', $expire = 0) {
   176 function wp_cache_set($key, $data, $group = '', $expire = 0) {
   139 	global $wp_object_cache;
   177 	global $wp_object_cache;
   140 
   178 
   141 	return $wp_object_cache->set($key, $data, $flag, $expire);
   179 	return $wp_object_cache->set($key, $data, $group, $expire);
   142 }
   180 }
   143 
   181 
   144 /**
   182 /**
   145  * Adds a group or set of groups to the list of global groups.
   183  * Adds a group or set of groups to the list of global groups.
   146  *
   184  *
   147  * @since 2.6.0
   185  * @since 2.6.0
   148  *
   186  *
   149  * @param string|array $groups A group or an array of groups to add
   187  * @param string|array $groups A group or an array of groups to add
   150  */
   188  */
   151 function wp_cache_add_global_groups( $groups ) {
   189 function wp_cache_add_global_groups( $groups ) {
   152 	// Default cache doesn't persist so nothing to do here.
   190 	global $wp_object_cache;
   153 	return;
   191 
       
   192 	return $wp_object_cache->add_global_groups($groups);
   154 }
   193 }
   155 
   194 
   156 /**
   195 /**
   157  * Adds a group or set of groups to the list of non-persistent groups.
   196  * Adds a group or set of groups to the list of non-persistent groups.
   158  *
   197  *
   161  * @param string|array $groups A group or an array of groups to add
   200  * @param string|array $groups A group or an array of groups to add
   162  */
   201  */
   163 function wp_cache_add_non_persistent_groups( $groups ) {
   202 function wp_cache_add_non_persistent_groups( $groups ) {
   164 	// Default cache doesn't persist so nothing to do here.
   203 	// Default cache doesn't persist so nothing to do here.
   165 	return;
   204 	return;
       
   205 }
       
   206 
       
   207 /**
       
   208  * Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys,
       
   209  * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
       
   210  *
       
   211  * @since 2.6.0
       
   212  */
       
   213 function wp_cache_reset() {
       
   214 	global $wp_object_cache;
       
   215 
       
   216 	return $wp_object_cache->reset();
   166 }
   217 }
   167 
   218 
   168 /**
   219 /**
   169  * WordPress Object Cache
   220  * WordPress Object Cache
   170  *
   221  *
   191 	 * @since 2.0.0
   242 	 * @since 2.0.0
   192 	 */
   243 	 */
   193 	var $cache = array ();
   244 	var $cache = array ();
   194 
   245 
   195 	/**
   246 	/**
   196 	 * Cache objects that do not exist in the cache
       
   197 	 *
       
   198 	 * @var array
       
   199 	 * @access private
       
   200 	 * @since 2.0.0
       
   201 	 */
       
   202 	var $non_existant_objects = array ();
       
   203 
       
   204 	/**
       
   205 	 * The amount of times the cache data was already stored in the cache.
   247 	 * The amount of times the cache data was already stored in the cache.
   206 	 *
   248 	 *
   207 	 * @since 2.5.0
   249 	 * @since 2.5.0
   208 	 * @access private
   250 	 * @access private
   209 	 * @var int
   251 	 * @var int
   218 	 * @since 2.0.0
   260 	 * @since 2.0.0
   219 	 */
   261 	 */
   220 	var $cache_misses = 0;
   262 	var $cache_misses = 0;
   221 
   263 
   222 	/**
   264 	/**
       
   265 	 * List of global groups
       
   266 	 *
       
   267 	 * @var array
       
   268 	 * @access protected
       
   269 	 * @since 3.0.0
       
   270 	 */
       
   271 	var $global_groups = array();
       
   272 
       
   273 	/**
   223 	 * Adds data to the cache if it doesn't already exist.
   274 	 * Adds data to the cache if it doesn't already exist.
   224 	 *
   275 	 *
   225 	 * @uses WP_Object_Cache::get Checks to see if the cache already has data.
   276 	 * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
   226 	 * @uses WP_Object_Cache::set Sets the data after the checking the cache
   277 	 * @uses WP_Object_Cache::set Sets the data after the checking the cache
   227 	 *		contents existance.
   278 	 *		contents existence.
   228 	 *
   279 	 *
   229 	 * @since 2.0.0
   280 	 * @since 2.0.0
   230 	 *
   281 	 *
   231 	 * @param int|string $id What to call the contents in the cache
   282 	 * @param int|string $key What to call the contents in the cache
   232 	 * @param mixed $data The contents to store in the cache
   283 	 * @param mixed $data The contents to store in the cache
   233 	 * @param string $group Where to group the cache contents
   284 	 * @param string $group Where to group the cache contents
   234 	 * @param int $expire When to expire the cache contents
   285 	 * @param int $expire When to expire the cache contents
   235 	 * @return bool False if cache ID and group already exists, true on success
   286 	 * @return bool False if cache key and group already exist, true on success
   236 	 */
   287 	 */
   237 	function add($id, $data, $group = 'default', $expire = '') {
   288 	function add( $key, $data, $group = 'default', $expire = '' ) {
   238 		if (empty ($group))
   289 		if ( wp_suspend_cache_addition() )
       
   290 			return false;
       
   291 
       
   292 		if ( empty( $group ) )
   239 			$group = 'default';
   293 			$group = 'default';
   240 
   294 
   241 		if (false !== $this->get($id, $group, false))
   295 		if ( $this->_exists($key, $group) )
   242 			return false;
   296 			return false;
   243 
   297 
   244 		return $this->set($id, $data, $group, $expire);
   298 		return $this->set($key, $data, $group, $expire);
   245 	}
   299 	}
   246 
   300 
   247 	/**
   301 	/**
   248 	 * Remove the contents of the cache ID in the group
   302 	 * Sets the list of global groups.
   249 	 *
   303 	 *
   250 	 * If the cache ID does not exist in the group and $force parameter is set
   304 	 * @since 3.0.0
       
   305 	 *
       
   306 	 * @param array $groups List of groups that are global.
       
   307 	 */
       
   308 	function add_global_groups( $groups ) {
       
   309 		$groups = (array) $groups;
       
   310 
       
   311 		$this->global_groups = array_merge($this->global_groups, $groups);
       
   312 		$this->global_groups = array_unique($this->global_groups);
       
   313 	}
       
   314 
       
   315 	/**
       
   316 	 * Decrement numeric cache item's value
       
   317 	 *
       
   318 	 * @since 3.3.0
       
   319 	 *
       
   320 	 * @param int|string $key The cache key to increment
       
   321 	 * @param int $offset The amount by which to decrement the item's value. Default is 1.
       
   322 	 * @param string $group The group the key is in.
       
   323 	 * @return false|int False on failure, the item's new value on success.
       
   324 	 */
       
   325 	function decr( $key, $offset = 1, $group = 'default' ) {
       
   326 		if ( ! $this->_exists( $key, $group ) )
       
   327 			return false;
       
   328 
       
   329 		if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
       
   330 			$this->cache[ $group ][ $key ] = 0;
       
   331 
       
   332 		$offset = (int) $offset;
       
   333 
       
   334 		$this->cache[ $group ][ $key ] -= $offset;
       
   335 
       
   336 		if ( $this->cache[ $group ][ $key ] < 0 )
       
   337 			$this->cache[ $group ][ $key ] = 0;
       
   338 
       
   339 		return $this->cache[ $group ][ $key ];
       
   340 	}
       
   341 
       
   342 	/**
       
   343 	 * Remove the contents of the cache key in the group
       
   344 	 *
       
   345 	 * If the cache key does not exist in the group and $force parameter is set
   251 	 * to false, then nothing will happen. The $force parameter is set to false
   346 	 * to false, then nothing will happen. The $force parameter is set to false
   252 	 * by default.
   347 	 * by default.
   253 	 *
   348 	 *
   254 	 * On success the group and the id will be added to the
   349 	 * @since 2.0.0
   255 	 * $non_existant_objects property in the class.
   350 	 *
   256 	 *
   351 	 * @param int|string $key What the contents in the cache are called
   257 	 * @since 2.0.0
       
   258 	 *
       
   259 	 * @param int|string $id What the contents in the cache are called
       
   260 	 * @param string $group Where the cache contents are grouped
   352 	 * @param string $group Where the cache contents are grouped
   261 	 * @param bool $force Optional. Whether to force the unsetting of the cache
   353 	 * @param bool $force Optional. Whether to force the unsetting of the cache
   262 	 *		ID in the group
   354 	 *		key in the group
   263 	 * @return bool False if the contents weren't deleted and true on success
   355 	 * @return bool False if the contents weren't deleted and true on success
   264 	 */
   356 	 */
   265 	function delete($id, $group = 'default', $force = false) {
   357 	function delete($key, $group = 'default', $force = false) {
   266 		if (empty ($group))
   358 		if ( empty( $group ) )
   267 			$group = 'default';
   359 			$group = 'default';
   268 
   360 
   269 		if (!$force && false === $this->get($id, $group, false))
   361 		if ( ! $force && ! $this->_exists( $key, $group ) )
   270 			return false;
   362 			return false;
   271 
   363 
   272 		unset ($this->cache[$group][$id]);
   364 		unset( $this->cache[$group][$key] );
   273 		$this->non_existant_objects[$group][$id] = true;
       
   274 		return true;
   365 		return true;
   275 	}
   366 	}
   276 
   367 
   277 	/**
   368 	/**
   278 	 * Clears the object cache of all data
   369 	 * Clears the object cache of all data
   289 
   380 
   290 	/**
   381 	/**
   291 	 * Retrieves the cache contents, if it exists
   382 	 * Retrieves the cache contents, if it exists
   292 	 *
   383 	 *
   293 	 * The contents will be first attempted to be retrieved by searching by the
   384 	 * The contents will be first attempted to be retrieved by searching by the
   294 	 * ID in the cache group. If the cache is hit (success) then the contents
   385 	 * key in the cache group. If the cache is hit (success) then the contents
   295 	 * are returned.
   386 	 * are returned.
   296 	 *
   387 	 *
   297 	 * On failure, the $non_existant_objects property is checked and if the
   388 	 * On failure, the number of cache misses will be incremented.
   298 	 * cache group and ID exist in there the cache misses will not be
   389 	 *
   299 	 * incremented. If not in the nonexistant objects property, then the cache
   390 	 * @since 2.0.0
   300 	 * misses will be incremented and the cache group and ID will be added to
   391 	 *
   301 	 * the nonexistant objects.
   392 	 * @param int|string $key What the contents in the cache are called
   302 	 *
       
   303 	 * @since 2.0.0
       
   304 	 *
       
   305 	 * @param int|string $id What the contents in the cache are called
       
   306 	 * @param string $group Where the cache contents are grouped
   393 	 * @param string $group Where the cache contents are grouped
       
   394 	 * @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
   307 	 * @return bool|mixed False on failure to retrieve contents or the cache
   395 	 * @return bool|mixed False on failure to retrieve contents or the cache
   308 	 *		contents on success
   396 	 *		contents on success
   309 	 */
   397 	 */
   310 	function get($id, $group = 'default') {
   398 	function get( $key, $group = 'default', $force = false, &$found = null ) {
   311 		if (empty ($group))
   399 		if ( empty( $group ) )
   312 			$group = 'default';
   400 			$group = 'default';
   313 
   401 
   314 		if (isset ($this->cache[$group][$id])) {
   402 		if ( $this->_exists( $key, $group ) ) {
       
   403 			$found = true;
   315 			$this->cache_hits += 1;
   404 			$this->cache_hits += 1;
   316 			if ( is_object($this->cache[$group][$id]) )
   405 			if ( is_object($this->cache[$group][$key]) )
   317 				return wp_clone($this->cache[$group][$id]);
   406 				return clone $this->cache[$group][$key];
   318 			else
   407 			else
   319 				return $this->cache[$group][$id];
   408 				return $this->cache[$group][$key];
   320 		}
   409 		}
   321 
   410 
   322 		if ( isset ($this->non_existant_objects[$group][$id]) )
   411 		$found = false;
   323 			return false;
       
   324 
       
   325 		$this->non_existant_objects[$group][$id] = true;
       
   326 		$this->cache_misses += 1;
   412 		$this->cache_misses += 1;
   327 		return false;
   413 		return false;
   328 	}
   414 	}
   329 
   415 
   330 	/**
   416 	/**
       
   417 	 * Increment numeric cache item's value
       
   418 	 *
       
   419 	 * @since 3.3.0
       
   420 	 *
       
   421 	 * @param int|string $key The cache key to increment
       
   422 	 * @param int $offset The amount by which to increment the item's value. Default is 1.
       
   423 	 * @param string $group The group the key is in.
       
   424 	 * @return false|int False on failure, the item's new value on success.
       
   425 	 */
       
   426 	function incr( $key, $offset = 1, $group = 'default' ) {
       
   427 		if ( empty( $group ) )
       
   428 			$group = 'default';
       
   429 
       
   430 		if ( ! $this->_exists( $key, $group ) )
       
   431 			return false;
       
   432 
       
   433 		if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
       
   434 			$this->cache[ $group ][ $key ] = 0;
       
   435 
       
   436 		$offset = (int) $offset;
       
   437 
       
   438 		$this->cache[ $group ][ $key ] += $offset;
       
   439 
       
   440 		if ( $this->cache[ $group ][ $key ] < 0 )
       
   441 			$this->cache[ $group ][ $key ] = 0;
       
   442 
       
   443 		return $this->cache[ $group ][ $key ];
       
   444 	}
       
   445 
       
   446 	/**
   331 	 * Replace the contents in the cache, if contents already exist
   447 	 * Replace the contents in the cache, if contents already exist
   332 	 *
   448 	 *
   333 	 * @since 2.0.0
   449 	 * @since 2.0.0
   334 	 * @see WP_Object_Cache::set()
   450 	 * @see WP_Object_Cache::set()
   335 	 *
   451 	 *
   336 	 * @param int|string $id What to call the contents in the cache
   452 	 * @param int|string $key What to call the contents in the cache
   337 	 * @param mixed $data The contents to store in the cache
   453 	 * @param mixed $data The contents to store in the cache
   338 	 * @param string $group Where to group the cache contents
   454 	 * @param string $group Where to group the cache contents
   339 	 * @param int $expire When to expire the cache contents
   455 	 * @param int $expire When to expire the cache contents
   340 	 * @return bool False if not exists, true if contents were replaced
   456 	 * @return bool False if not exists, true if contents were replaced
   341 	 */
   457 	 */
   342 	function replace($id, $data, $group = 'default', $expire = '') {
   458 	function replace($key, $data, $group = 'default', $expire = '') {
   343 		if (empty ($group))
   459 		if ( empty( $group ) )
   344 			$group = 'default';
   460 			$group = 'default';
   345 
   461 
   346 		if (false === $this->get($id, $group, false))
   462 		if ( ! $this->_exists( $key, $group ) )
   347 			return false;
   463 			return false;
   348 
   464 
   349 		return $this->set($id, $data, $group, $expire);
   465 		return $this->set($key, $data, $group, $expire);
       
   466 	}
       
   467 
       
   468 	/**
       
   469 	 * Reset keys
       
   470 	 *
       
   471 	 * @since 3.0.0
       
   472 	 */
       
   473 	function reset() {
       
   474 		// Clear out non-global caches since the blog ID has changed.
       
   475 		foreach ( array_keys($this->cache) as $group ) {
       
   476 			if ( !in_array($group, $this->global_groups) )
       
   477 				unset($this->cache[$group]);
       
   478 		}
   350 	}
   479 	}
   351 
   480 
   352 	/**
   481 	/**
   353 	 * Sets the data contents into the cache
   482 	 * Sets the data contents into the cache
   354 	 *
   483 	 *
   355 	 * The cache contents is grouped by the $group parameter followed by the
   484 	 * The cache contents is grouped by the $group parameter followed by the
   356 	 * $id. This allows for duplicate ids in unique groups. Therefore, naming of
   485 	 * $key. This allows for duplicate ids in unique groups. Therefore, naming of
   357 	 * the group should be used with care and should follow normal function
   486 	 * the group should be used with care and should follow normal function
   358 	 * naming guidelines outside of core WordPress usage.
   487 	 * naming guidelines outside of core WordPress usage.
   359 	 *
   488 	 *
   360 	 * The $expire parameter is not used, because the cache will automatically
   489 	 * The $expire parameter is not used, because the cache will automatically
   361 	 * expire for each time a page is accessed and PHP finishes. The method is
   490 	 * expire for each time a page is accessed and PHP finishes. The method is
   362 	 * more for cache plugins which use files.
   491 	 * more for cache plugins which use files.
   363 	 *
   492 	 *
   364 	 * @since 2.0.0
   493 	 * @since 2.0.0
   365 	 *
   494 	 *
   366 	 * @param int|string $id What to call the contents in the cache
   495 	 * @param int|string $key What to call the contents in the cache
   367 	 * @param mixed $data The contents to store in the cache
   496 	 * @param mixed $data The contents to store in the cache
   368 	 * @param string $group Where to group the cache contents
   497 	 * @param string $group Where to group the cache contents
   369 	 * @param int $expire Not Used
   498 	 * @param int $expire Not Used
   370 	 * @return bool Always returns true
   499 	 * @return bool Always returns true
   371 	 */
   500 	 */
   372 	function set($id, $data, $group = 'default', $expire = '') {
   501 	function set($key, $data, $group = 'default', $expire = '') {
   373 		if (empty ($group))
   502 		if ( empty( $group ) )
   374 			$group = 'default';
   503 			$group = 'default';
   375 
   504 
   376 		if (NULL === $data)
       
   377 			$data = '';
       
   378 
       
   379 		if ( is_object($data) )
   505 		if ( is_object($data) )
   380 			$data = wp_clone($data);
   506 			$data = clone $data;
   381 
   507 
   382 		$this->cache[$group][$id] = $data;
   508 		$this->cache[$group][$key] = $data;
   383 
       
   384 		if(isset($this->non_existant_objects[$group][$id]))
       
   385 			unset ($this->non_existant_objects[$group][$id]);
       
   386 
       
   387 		return true;
   509 		return true;
   388 	}
   510 	}
   389 
   511 
   390 	/**
   512 	/**
   391 	 * Echos the stats of the caching.
   513 	 * Echoes the stats of the caching.
   392 	 *
   514 	 *
   393 	 * Gives the cache hits, and cache misses. Also prints every cached group,
   515 	 * Gives the cache hits, and cache misses. Also prints every cached group,
   394 	 * key and the data.
   516 	 * key and the data.
   395 	 *
   517 	 *
   396 	 * @since 2.0.0
   518 	 * @since 2.0.0
   398 	function stats() {
   520 	function stats() {
   399 		echo "<p>";
   521 		echo "<p>";
   400 		echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
   522 		echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
   401 		echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
   523 		echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
   402 		echo "</p>";
   524 		echo "</p>";
   403 
   525 		echo '<ul>';
   404 		foreach ($this->cache as $group => $cache) {
   526 		foreach ($this->cache as $group => $cache) {
   405 			echo "<p>";
   527 			echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
   406 			echo "<strong>Group:</strong> $group<br />";
       
   407 			echo "<strong>Cache:</strong>";
       
   408 			echo "<pre>";
       
   409 			print_r($cache);
       
   410 			echo "</pre>";
       
   411 		}
   528 		}
   412 	}
   529 		echo '</ul>';
   413 
   530 	}
   414 	/**
   531 
   415 	 * PHP4 constructor; Calls PHP 5 style constructor
   532 	/**
   416 	 *
   533 	 * Utility function to determine whether a key exists in the cache.
   417 	 * @since 2.0.0
   534 	 * @access private
   418 	 *
   535 	 */
   419 	 * @return WP_Object_Cache
   536 	protected function _exists($key, $group) {
   420 	 */
   537 		return isset( $this->cache[$group] ) && is_array( $this->cache[$group] ) && array_key_exists( $key, $this->cache[$group] );
   421 	function WP_Object_Cache() {
       
   422 		return $this->__construct();
       
   423 	}
   538 	}
   424 
   539 
   425 	/**
   540 	/**
   426 	 * Sets up object properties; PHP 5 style constructor
   541 	 * Sets up object properties; PHP 5 style constructor
   427 	 *
   542 	 *
   447 	 */
   562 	 */
   448 	function __destruct() {
   563 	function __destruct() {
   449 		return true;
   564 		return true;
   450 	}
   565 	}
   451 }
   566 }
   452 ?>