10 * wp_cache_add() - Adds data to the cache, if the cache key doesn't aleady exist
13 * @uses $wp_object_cache Object Cache Class
14 * @see WP_Object_Cache::add()
16 * @param int|string $key The cache ID to use for retrieval later
17 * @param mixed $data The data to add to the cache store
18 * @param string $flag The group to add the cache to
19 * @param int $expire When the cache data should be expired
22 function wp_cache_add($key, $data, $flag = '', $expire = 0) {
23 global $wp_object_cache;
25 return $wp_object_cache->add($key, $data, $flag, $expire);
29 * wp_cache_close() - Closes the cache
31 * This function has ceased to do anything since WordPress 2.5.
32 * The functionality was removed along with the rest of the
37 * @return bool Always returns True
39 function wp_cache_close() {
44 * wp_cache_delete() - Removes the cache contents matching ID and flag
47 * @uses $wp_object_cache Object Cache Class
48 * @see WP_Object_Cache::delete()
50 * @param int|string $id What the contents in the cache are called
51 * @param string $flag Where the cache contents are grouped
52 * @return bool True on successful removal, false on failure
54 function wp_cache_delete($id, $flag = '') {
55 global $wp_object_cache;
57 return $wp_object_cache->delete($id, $flag);
61 * wp_cache_flush() - Removes all cache items
64 * @uses $wp_object_cache Object Cache Class
65 * @see WP_Object_Cache::flush()
67 * @return bool Always returns true
69 function wp_cache_flush() {
70 global $wp_object_cache;
72 return $wp_object_cache->flush();
76 * wp_cache_get() - Retrieves the cache contents from the cache by ID and flag
79 * @uses $wp_object_cache Object Cache Class
80 * @see WP_Object_Cache::get()
82 * @param int|string $id What the contents in the cache are called
83 * @param string $flag Where the cache contents are grouped
84 * @return bool|mixed False on failure to retrieve contents or the cache contents on success
86 function wp_cache_get($id, $flag = '') {
87 global $wp_object_cache;
89 return $wp_object_cache->get($id, $flag);
93 * wp_cache_init() - Sets up Object Cache Global and assigns it
96 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
98 function wp_cache_init() {
99 $GLOBALS['wp_object_cache'] =& new WP_Object_Cache();
103 * wp_cache_replace() - Replaces the contents of the cache with new data
106 * @uses $wp_object_cache Object Cache Class
107 * @see WP_Object_Cache::replace()
109 * @param int|string $id What to call the contents in the cache
110 * @param mixed $data The contents to store in the cache
111 * @param string $flag Where to group the cache contents
112 * @param int $expire When to expire the cache contents
113 * @return bool False if cache ID and group already exists, true on success
115 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
116 global $wp_object_cache;
118 return $wp_object_cache->replace($key, $data, $flag, $expire);
122 * wp_cache_set() - Saves the data to the cache
125 * @uses $wp_object_cache Object Cache Class
126 * @see WP_Object_Cache::set()
128 * @param int|string $id What to call the contents in the cache
129 * @param mixed $data The contents to store in the cache
130 * @param string $flag Where to group the cache contents
131 * @param int $expire When to expire the cache contents
132 * @return bool False if cache ID and group already exists, true on success
134 function wp_cache_set($key, $data, $flag = '', $expire = 0) {
135 global $wp_object_cache;
137 return $wp_object_cache->set($key, $data, $flag, $expire);
141 * WordPress Object Cache
143 * The WordPress Object Cache is used to save on trips to the database.
144 * The Object Cache stores all of the cache data to memory and makes the
145 * cache contents available by using a key, which is used to name and
146 * later retrieve the cache contents.
148 * The Object Cache can be replaced by other caching mechanisms by placing
149 * files in the wp-content folder which is looked at in wp-settings. If
150 * that file exists, then this file will not be included.
156 class WP_Object_Cache {
159 * Holds the cached objects
165 var $cache = array ();
168 * Cache objects that do not exist in the cache
174 var $non_existant_objects = array ();
177 * Object caches that are global
183 var $global_groups = array ('users', 'userlogins', 'usermeta');
186 * The amount of times the cache data was already stored in the cache.
195 * Amount of times the cache did not have the request in cache
201 var $cache_misses = 0;
204 * Adds data to the cache if it doesn't already exist.
206 * @uses WP_Object_Cache::get Checks to see if the cache already has data.
207 * @uses WP_Object_Cache::set Sets the data after the checking the cache contents existance.
211 * @param int|string $id What to call the contents in the cache
212 * @param mixed $data The contents to store in the cache
213 * @param string $group Where to group the cache contents
214 * @param int $expire When to expire the cache contents
215 * @return bool False if cache ID and group already exists, true on success
217 function add($id, $data, $group = 'default', $expire = '') {
221 if (false !== $this->get($id, $group, false))
224 return $this->set($id, $data, $group, $expire);
228 * Remove the contents of the cache ID in the group
230 * If the cache ID does not exist in the group and $force parameter
231 * is set to false, then nothing will happen. The $force parameter
232 * is set to false by default.
234 * On success the group and the id will be added to the
235 * $non_existant_objects property in the class.
239 * @param int|string $id What the contents in the cache are called
240 * @param string $group Where the cache contents are grouped
241 * @param bool $force Optional. Whether to force the unsetting of the cache ID in the group
242 * @return bool False if the contents weren't deleted and true on success
244 function delete($id, $group = 'default', $force = false) {
248 if (!$force && false === $this->get($id, $group, false))
251 unset ($this->cache[$group][$id]);
252 $this->non_existant_objects[$group][$id] = true;
257 * Clears the object cache of all data
261 * @return bool Always returns true
264 $this->cache = array ();
270 * Retrieves the cache contents, if it exists
272 * The contents will be first attempted to be retrieved by searching
273 * by the ID in the cache group. If the cache is hit (success) then
274 * the contents are returned.
276 * On failure, the $non_existant_objects property is checked and if
277 * the cache group and ID exist in there the cache misses will not be
278 * incremented. If not in the nonexistant objects property, then the
279 * cache misses will be incremented and the cache group and ID will
280 * be added to the nonexistant objects.
284 * @param int|string $id What the contents in the cache are called
285 * @param string $group Where the cache contents are grouped
286 * @return bool|mixed False on failure to retrieve contents or the cache contents on success
288 function get($id, $group = 'default') {
292 if (isset ($this->cache[$group][$id])) {
293 $this->cache_hits += 1;
294 return $this->cache[$group][$id];
297 if ( isset ($this->non_existant_objects[$group][$id]) )
300 $this->non_existant_objects[$group][$id] = true;
301 $this->cache_misses += 1;
306 * Replace the contents in the cache, if contents already exist
309 * @see WP_Object_Cache::set()
311 * @param int|string $id What to call the contents in the cache
312 * @param mixed $data The contents to store in the cache
313 * @param string $group Where to group the cache contents
314 * @param int $expire When to expire the cache contents
315 * @return bool False if not exists, true if contents were replaced
317 function replace($id, $data, $group = 'default', $expire = '') {
321 if (false === $this->get($id, $group, false))
324 return $this->set($id, $data, $group, $expire);
328 * Sets the data contents into the cache
330 * The cache contents is grouped by the $group parameter followed
331 * by the $id. This allows for duplicate ids in unique groups.
332 * Therefore, naming of the group should be used with care and
333 * should follow normal function naming guidelines outside of
334 * core WordPress usage.
336 * The $expire parameter is not used, because the cache will
337 * automatically expire for each time a page is accessed and PHP
338 * finishes. The method is more for cache plugins which use files.
342 * @param int|string $id What to call the contents in the cache
343 * @param mixed $data The contents to store in the cache
344 * @param string $group Where to group the cache contents
345 * @param int $expire Not Used
346 * @return bool Always returns true
348 function set($id, $data, $group = 'default', $expire = '') {
355 $this->cache[$group][$id] = $data;
357 if(isset($this->non_existant_objects[$group][$id]))
358 unset ($this->non_existant_objects[$group][$id]);
364 * Echos the stats of the caching.
366 * Gives the cache hits, and cache misses. Also prints every cached
367 * group, key and the data.
373 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
374 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
377 foreach ($this->cache as $group => $cache) {
379 echo "<strong>Group:</strong> $group<br />";
380 echo "<strong>Cache:</strong>";
388 * PHP4 constructor; Calls PHP 5 style constructor
392 * @return WP_Object_Cache
394 function WP_Object_Cache() {
395 return $this->__construct();
399 * Sets up object properties; PHP 5 style constructor
402 * @return null|WP_Object_Cache If cache is disabled, returns null.
404 function __construct() {
405 register_shutdown_function(array(&$this, "__destruct")); /** @todo This should be moved to the PHP4 style constructor, PHP5 already calls __destruct() */
409 * Will save the object cache before object is completely destroyed.
411 * Called upon object destruction, which should be when PHP ends.
415 * @return bool True value. Won't be used by PHP
417 function __destruct() {