5 * @link http://codex.wordpress.org/Function_Reference/WP_Cache
12 * Adds data to the cache, if the cache key doesn't already exist.
15 * @uses $wp_object_cache Object Cache Class
16 * @see WP_Object_Cache::add()
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
20 * @param string $group The group to add the cache to
21 * @param int $expire When the cache data should be expired
24 function wp_cache_add($key, $data, $group = '', $expire = 0) {
25 global $wp_object_cache;
27 return $wp_object_cache->add($key, $data, $group, $expire);
33 * This function has ceased to do anything since WordPress 2.5. The
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
36 * make sure that the cache is cleaned up after WordPress no longer needs it.
40 * @return bool Always returns True
42 function wp_cache_close() {
47 * Decrement numeric cache item's value
50 * @uses $wp_object_cache Object Cache Class
51 * @see WP_Object_Cache::decr()
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.
58 function wp_cache_decr( $key, $offset = 1, $group = '' ) {
59 global $wp_object_cache;
61 return $wp_object_cache->decr( $key, $offset, $group );
65 * Removes the cache contents matching key and group.
68 * @uses $wp_object_cache Object Cache Class
69 * @see WP_Object_Cache::delete()
71 * @param int|string $key What the contents in the cache are called
72 * @param string $group Where the cache contents are grouped
73 * @return bool True on successful removal, false on failure
75 function wp_cache_delete($key, $group = '') {
76 global $wp_object_cache;
78 return $wp_object_cache->delete($key, $group);
82 * Removes all cache items.
85 * @uses $wp_object_cache Object Cache Class
86 * @see WP_Object_Cache::flush()
88 * @return bool Always returns true
90 function wp_cache_flush() {
91 global $wp_object_cache;
93 return $wp_object_cache->flush();
97 * Retrieves the cache contents from the cache by key and group.
100 * @uses $wp_object_cache Object Cache Class
101 * @see WP_Object_Cache::get()
103 * @param int|string $key What the contents in the cache are called
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 * @return bool|mixed False on failure to retrieve contents or the cache
107 * contents on success
109 function wp_cache_get( $key, $group = '', $force = false ) {
110 global $wp_object_cache;
112 return $wp_object_cache->get( $key, $group, $force );
116 * Increment numeric cache item's value
119 * @uses $wp_object_cache Object Cache Class
120 * @see WP_Object_Cache::incr()
122 * @param int|string $key The cache key to increment
123 * @param int $offset The amount by which to increment the item's value. Default is 1.
124 * @param string $group The group the key is in.
125 * @return false|int False on failure, the item's new value on success.
127 function wp_cache_incr( $key, $offset = 1, $group = '' ) {
128 global $wp_object_cache;
130 return $wp_object_cache->incr( $key, $offset, $group );
134 * Sets up Object Cache Global and assigns it.
137 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
139 function wp_cache_init() {
140 $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
144 * Replaces the contents of the cache with new data.
147 * @uses $wp_object_cache Object Cache Class
148 * @see WP_Object_Cache::replace()
150 * @param int|string $key What to call the contents in the cache
151 * @param mixed $data The contents to store in the cache
152 * @param string $group Where to group the cache contents
153 * @param int $expire When to expire the cache contents
154 * @return bool False if cache key and group already exist, true on success
156 function wp_cache_replace($key, $data, $group = '', $expire = 0) {
157 global $wp_object_cache;
159 return $wp_object_cache->replace($key, $data, $group, $expire);
163 * Saves the data to the cache.
166 * @uses $wp_object_cache Object Cache Class
167 * @see WP_Object_Cache::set()
169 * @param int|string $key What to call the contents in the cache
170 * @param mixed $data The contents to store in the cache
171 * @param string $group Where to group the cache contents
172 * @param int $expire When to expire the cache contents
173 * @return bool False if cache key and group already exist, true on success
175 function wp_cache_set($key, $data, $group = '', $expire = 0) {
176 global $wp_object_cache;
178 return $wp_object_cache->set($key, $data, $group, $expire);
182 * Adds a group or set of groups to the list of global groups.
186 * @param string|array $groups A group or an array of groups to add
188 function wp_cache_add_global_groups( $groups ) {
189 global $wp_object_cache;
191 return $wp_object_cache->add_global_groups($groups);
195 * Adds a group or set of groups to the list of non-persistent groups.
199 * @param string|array $groups A group or an array of groups to add
201 function wp_cache_add_non_persistent_groups( $groups ) {
202 // Default cache doesn't persist so nothing to do here.
207 * Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys,
208 * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
212 function wp_cache_reset() {
213 global $wp_object_cache;
215 return $wp_object_cache->reset();
219 * WordPress Object Cache
221 * The WordPress Object Cache is used to save on trips to the database. The
222 * Object Cache stores all of the cache data to memory and makes the cache
223 * contents available by using a key, which is used to name and later retrieve
224 * the cache contents.
226 * The Object Cache can be replaced by other caching mechanisms by placing files
227 * in the wp-content folder which is looked at in wp-settings. If that file
228 * exists, then this file will not be included.
234 class WP_Object_Cache {
237 * Holds the cached objects
243 var $cache = array ();
246 * The amount of times the cache data was already stored in the cache.
255 * Amount of times the cache did not have the request in cache
261 var $cache_misses = 0;
264 * List of global groups
270 var $global_groups = array();
273 * Adds data to the cache if it doesn't already exist.
275 * @uses WP_Object_Cache::get Checks to see if the cache already has data.
276 * @uses WP_Object_Cache::set Sets the data after the checking the cache
277 * contents existence.
281 * @param int|string $key What to call the contents in the cache
282 * @param mixed $data The contents to store in the cache
283 * @param string $group Where to group the cache contents
284 * @param int $expire When to expire the cache contents
285 * @return bool False if cache key and group already exist, true on success
287 function add( $key, $data, $group = 'default', $expire = '' ) {
288 if ( wp_suspend_cache_addition() )
291 if ( empty ($group) )
294 if (false !== $this->get($key, $group))
297 return $this->set($key, $data, $group, $expire);
301 * Sets the list of global groups.
305 * @param array $groups List of groups that are global.
307 function add_global_groups( $groups ) {
308 $groups = (array) $groups;
310 $this->global_groups = array_merge($this->global_groups, $groups);
311 $this->global_groups = array_unique($this->global_groups);
315 * Decrement numeric cache item's value
319 * @param int|string $key The cache key to increment
320 * @param int $offset The amount by which to decrement the item's value. Default is 1.
321 * @param string $group The group the key is in.
322 * @return false|int False on failure, the item's new value on success.
324 function decr( $key, $offset = 1, $group = 'default' ) {
325 if ( ! isset( $this->cache[ $group ][ $key ] ) )
328 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
329 $this->cache[ $group ][ $key ] = 0;
331 $offset = (int) $offset;
333 $this->cache[ $group ][ $key ] -= $offset;
335 if ( $this->cache[ $group ][ $key ] < 0 )
336 $this->cache[ $group ][ $key ] = 0;
338 return $this->cache[ $group ][ $key ];
342 * Remove the contents of the cache key in the group
344 * If the cache key does not exist in the group and $force parameter is set
345 * to false, then nothing will happen. The $force parameter is set to false
350 * @param int|string $key What the contents in the cache are called
351 * @param string $group Where the cache contents are grouped
352 * @param bool $force Optional. Whether to force the unsetting of the cache
354 * @return bool False if the contents weren't deleted and true on success
356 function delete($key, $group = 'default', $force = false) {
360 if (!$force && false === $this->get($key, $group))
363 unset ($this->cache[$group][$key]);
368 * Clears the object cache of all data
372 * @return bool Always returns true
375 $this->cache = array ();
381 * Retrieves the cache contents, if it exists
383 * The contents will be first attempted to be retrieved by searching by the
384 * key in the cache group. If the cache is hit (success) then the contents
387 * On failure, the number of cache misses will be incremented.
391 * @param int|string $key What the contents in the cache are called
392 * @param string $group Where the cache contents are grouped
393 * @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
394 * @return bool|mixed False on failure to retrieve contents or the cache
395 * contents on success
397 function get( $key, $group = 'default', $force = false) {
398 if ( empty ($group) )
401 if ( isset ($this->cache[$group][$key]) ) {
402 $this->cache_hits += 1;
403 if ( is_object($this->cache[$group][$key]) )
404 return clone $this->cache[$group][$key];
406 return $this->cache[$group][$key];
409 $this->cache_misses += 1;
414 * Increment numeric cache item's value
418 * @param int|string $key The cache key to increment
419 * @param int $offset The amount by which to increment the item's value. Default is 1.
420 * @param string $group The group the key is in.
421 * @return false|int False on failure, the item's new value on success.
423 function incr( $key, $offset = 1, $group = 'default' ) {
424 if ( ! isset( $this->cache[ $group ][ $key ] ) )
427 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
428 $this->cache[ $group ][ $key ] = 0;
430 $offset = (int) $offset;
432 $this->cache[ $group ][ $key ] += $offset;
434 if ( $this->cache[ $group ][ $key ] < 0 )
435 $this->cache[ $group ][ $key ] = 0;
437 return $this->cache[ $group ][ $key ];
441 * Replace the contents in the cache, if contents already exist
444 * @see WP_Object_Cache::set()
446 * @param int|string $key What to call the contents in the cache
447 * @param mixed $data The contents to store in the cache
448 * @param string $group Where to group the cache contents
449 * @param int $expire When to expire the cache contents
450 * @return bool False if not exists, true if contents were replaced
452 function replace($key, $data, $group = 'default', $expire = '') {
456 if ( false === $this->get($key, $group) )
459 return $this->set($key, $data, $group, $expire);
468 // Clear out non-global caches since the blog ID has changed.
469 foreach ( array_keys($this->cache) as $group ) {
470 if ( !in_array($group, $this->global_groups) )
471 unset($this->cache[$group]);
476 * Sets the data contents into the cache
478 * The cache contents is grouped by the $group parameter followed by the
479 * $key. This allows for duplicate ids in unique groups. Therefore, naming of
480 * the group should be used with care and should follow normal function
481 * naming guidelines outside of core WordPress usage.
483 * The $expire parameter is not used, because the cache will automatically
484 * expire for each time a page is accessed and PHP finishes. The method is
485 * more for cache plugins which use files.
489 * @param int|string $key What to call the contents in the cache
490 * @param mixed $data The contents to store in the cache
491 * @param string $group Where to group the cache contents
492 * @param int $expire Not Used
493 * @return bool Always returns true
495 function set($key, $data, $group = 'default', $expire = '') {
496 if ( empty ($group) )
499 if ( NULL === $data )
502 if ( is_object($data) )
505 $this->cache[$group][$key] = $data;
510 * Echoes the stats of the caching.
512 * Gives the cache hits, and cache misses. Also prints every cached group,
519 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
520 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
523 foreach ($this->cache as $group => $cache) {
524 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
530 * Sets up object properties; PHP 5 style constructor
533 * @return null|WP_Object_Cache If cache is disabled, returns null.
535 function __construct() {
537 * @todo This should be moved to the PHP4 style constructor, PHP5
538 * already calls __destruct()
540 register_shutdown_function(array(&$this, "__destruct"));
544 * Will save the object cache before object is completely destroyed.
546 * Called upon object destruction, which should be when PHP ends.
550 * @return bool True value. Won't be used by PHP
552 function __destruct() {