5 * @link http://codex.wordpress.org/Function_Reference/WP_Cache
12 * Adds data to the cache, if the cache key doesn't aleady exist.
15 * @uses $wp_object_cache Object Cache Class
16 * @see WP_Object_Cache::add()
18 * @param int|string $key The cache ID to use for retrieval later
19 * @param mixed $data The data to add to the cache store
20 * @param string $flag 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, $flag = '', $expire = 0) {
25 global $wp_object_cache;
27 return $wp_object_cache->add($key, $data, $flag, $expire);
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
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 * Removes the cache contents matching ID and flag.
50 * @uses $wp_object_cache Object Cache Class
51 * @see WP_Object_Cache::delete()
53 * @param int|string $id What the contents in the cache are called
54 * @param string $flag Where the cache contents are grouped
55 * @return bool True on successful removal, false on failure
57 function wp_cache_delete($id, $flag = '') {
58 global $wp_object_cache;
60 return $wp_object_cache->delete($id, $flag);
64 * Removes all cache items.
67 * @uses $wp_object_cache Object Cache Class
68 * @see WP_Object_Cache::flush()
70 * @return bool Always returns true
72 function wp_cache_flush() {
73 global $wp_object_cache;
75 return $wp_object_cache->flush();
79 * Retrieves the cache contents from the cache by ID and flag.
82 * @uses $wp_object_cache Object Cache Class
83 * @see WP_Object_Cache::get()
85 * @param int|string $id What the contents in the cache are called
86 * @param string $flag Where the cache contents are grouped
87 * @return bool|mixed False on failure to retrieve contents or the cache
90 function wp_cache_get($id, $flag = '') {
91 global $wp_object_cache;
93 return $wp_object_cache->get($id, $flag);
97 * Sets up Object Cache Global and assigns it.
100 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
102 function wp_cache_init() {
103 $GLOBALS['wp_object_cache'] =& new WP_Object_Cache();
107 * Replaces the contents of the cache with new data.
110 * @uses $wp_object_cache Object Cache Class
111 * @see WP_Object_Cache::replace()
113 * @param int|string $id What to call the contents in the cache
114 * @param mixed $data The contents to store in the cache
115 * @param string $flag Where to group the cache contents
116 * @param int $expire When to expire the cache contents
117 * @return bool False if cache ID and group already exists, true on success
119 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
120 global $wp_object_cache;
122 return $wp_object_cache->replace($key, $data, $flag, $expire);
126 * Saves the data to the cache.
129 * @uses $wp_object_cache Object Cache Class
130 * @see WP_Object_Cache::set()
132 * @param int|string $id What to call the contents in the cache
133 * @param mixed $data The contents to store in the cache
134 * @param string $flag Where to group the cache contents
135 * @param int $expire When to expire the cache contents
136 * @return bool False if cache ID and group already exists, true on success
138 function wp_cache_set($key, $data, $flag = '', $expire = 0) {
139 global $wp_object_cache;
141 return $wp_object_cache->set($key, $data, $flag, $expire);
145 * Adds a group or set of groups to the list of global groups.
149 * @param string|array $groups A group or an array of groups to add
151 function wp_cache_add_global_groups( $groups ) {
152 global $wp_object_cache;
154 return $wp_object_cache->add_global_groups($groups);
158 * Adds a group or set of groups to the list of non-persistent groups.
162 * @param string|array $groups A group or an array of groups to add
164 function wp_cache_add_non_persistent_groups( $groups ) {
165 // Default cache doesn't persist so nothing to do here.
170 * Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys,
171 * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
175 * @param string|array $groups A group or an array of groups to add
177 function wp_cache_reset() {
178 global $wp_object_cache;
180 return $wp_object_cache->reset();
184 * WordPress Object Cache
186 * The WordPress Object Cache is used to save on trips to the database. The
187 * Object Cache stores all of the cache data to memory and makes the cache
188 * contents available by using a key, which is used to name and later retrieve
189 * the cache contents.
191 * The Object Cache can be replaced by other caching mechanisms by placing files
192 * in the wp-content folder which is looked at in wp-settings. If that file
193 * exists, then this file will not be included.
199 class WP_Object_Cache {
202 * Holds the cached objects
208 var $cache = array ();
211 * Cache objects that do not exist in the cache
217 var $non_existent_objects = array ();
220 * The amount of times the cache data was already stored in the cache.
229 * Amount of times the cache did not have the request in cache
235 var $cache_misses = 0;
238 * List of global groups
244 var $global_groups = array();
247 * Adds data to the cache if it doesn't already exist.
249 * @uses WP_Object_Cache::get Checks to see if the cache already has data.
250 * @uses WP_Object_Cache::set Sets the data after the checking the cache
251 * contents existance.
255 * @param int|string $id What to call the contents in the cache
256 * @param mixed $data The contents to store in the cache
257 * @param string $group Where to group the cache contents
258 * @param int $expire When to expire the cache contents
259 * @return bool False if cache ID and group already exists, true on success
261 function add( $id, $data, $group = 'default', $expire = '' ) {
262 if ( empty ($group) )
265 if (false !== $this->get($id, $group))
268 return $this->set($id, $data, $group, $expire);
272 * Sets the list of global groups.
276 * @param array $groups List of groups that are global.
278 function add_global_groups( $groups ) {
279 $groups = (array) $groups;
281 $this->global_groups = array_merge($this->global_groups, $groups);
282 $this->global_groups = array_unique($this->global_groups);
286 * Remove the contents of the cache ID in the group
288 * If the cache ID does not exist in the group and $force parameter is set
289 * to false, then nothing will happen. The $force parameter is set to false
292 * On success the group and the id will be added to the
293 * $non_existent_objects property in the class.
297 * @param int|string $id What the contents in the cache are called
298 * @param string $group Where the cache contents are grouped
299 * @param bool $force Optional. Whether to force the unsetting of the cache
301 * @return bool False if the contents weren't deleted and true on success
303 function delete($id, $group = 'default', $force = false) {
307 if (!$force && false === $this->get($id, $group))
310 unset ($this->cache[$group][$id]);
311 $this->non_existent_objects[$group][$id] = true;
316 * Clears the object cache of all data
320 * @return bool Always returns true
323 $this->cache = array ();
329 * Retrieves the cache contents, if it exists
331 * The contents will be first attempted to be retrieved by searching by the
332 * ID in the cache group. If the cache is hit (success) then the contents
335 * On failure, the $non_existent_objects property is checked and if the
336 * cache group and ID exist in there the cache misses will not be
337 * incremented. If not in the nonexistent objects property, then the cache
338 * misses will be incremented and the cache group and ID will be added to
339 * the nonexistent objects.
343 * @param int|string $id What the contents in the cache are called
344 * @param string $group Where the cache contents are grouped
345 * @return bool|mixed False on failure to retrieve contents or the cache
346 * contents on success
348 function get($id, $group = 'default') {
349 if ( empty ($group) )
352 if ( isset ($this->cache[$group][$id]) ) {
353 $this->cache_hits += 1;
354 if ( is_object($this->cache[$group][$id]) )
355 return wp_clone($this->cache[$group][$id]);
357 return $this->cache[$group][$id];
360 if ( isset ($this->non_existent_objects[$group][$id]) )
363 $this->non_existent_objects[$group][$id] = true;
364 $this->cache_misses += 1;
369 * Replace the contents in the cache, if contents already exist
372 * @see WP_Object_Cache::set()
374 * @param int|string $id What to call the contents in the cache
375 * @param mixed $data The contents to store in the cache
376 * @param string $group Where to group the cache contents
377 * @param int $expire When to expire the cache contents
378 * @return bool False if not exists, true if contents were replaced
380 function replace($id, $data, $group = 'default', $expire = '') {
384 if ( false === $this->get($id, $group) )
387 return $this->set($id, $data, $group, $expire);
396 // Clear out non-global caches since the blog ID has changed.
397 foreach ( array_keys($this->cache) as $group ) {
398 if ( !in_array($group, $this->global_groups) )
399 unset($this->cache[$group]);
404 * Sets the data contents into the cache
406 * The cache contents is grouped by the $group parameter followed by the
407 * $id. This allows for duplicate ids in unique groups. Therefore, naming of
408 * the group should be used with care and should follow normal function
409 * naming guidelines outside of core WordPress usage.
411 * The $expire parameter is not used, because the cache will automatically
412 * expire for each time a page is accessed and PHP finishes. The method is
413 * more for cache plugins which use files.
417 * @param int|string $id What to call the contents in the cache
418 * @param mixed $data The contents to store in the cache
419 * @param string $group Where to group the cache contents
420 * @param int $expire Not Used
421 * @return bool Always returns true
423 function set($id, $data, $group = 'default', $expire = '') {
424 if ( empty ($group) )
427 if ( NULL === $data )
430 if ( is_object($data) )
431 $data = wp_clone($data);
433 $this->cache[$group][$id] = $data;
435 if ( isset($this->non_existent_objects[$group][$id]) )
436 unset ($this->non_existent_objects[$group][$id]);
442 * Echoes the stats of the caching.
444 * Gives the cache hits, and cache misses. Also prints every cached group,
451 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
452 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
455 foreach ($this->cache as $group => $cache) {
457 echo "<strong>Group:</strong> $group<br />";
458 echo "<strong>Cache:</strong>";
466 * PHP4 constructor; Calls PHP 5 style constructor
470 * @return WP_Object_Cache
472 function WP_Object_Cache() {
473 return $this->__construct();
477 * Sets up object properties; PHP 5 style constructor
480 * @return null|WP_Object_Cache If cache is disabled, returns null.
482 function __construct() {
484 * @todo This should be moved to the PHP4 style constructor, PHP5
485 * already calls __destruct()
487 register_shutdown_function(array(&$this, "__destruct"));
491 * Will save the object cache before object is completely destroyed.
493 * Called upon object destruction, which should be when PHP ends.
497 * @return bool True value. Won't be used by PHP
499 function __destruct() {