raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Object Cache API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @link http://codex.wordpress.org/Function_Reference/WP_Cache
mp-wp_genesis 6 *
mp-wp_genesis 7 * @package WordPress
mp-wp_genesis 8 * @subpackage Cache
mp-wp_genesis 9 */
mp-wp_genesis 10
mp-wp_genesis 11 /**
mp-wp_genesis 12 * Adds data to the cache, if the cache key doesn't aleady exist.
mp-wp_genesis 13 *
mp-wp_genesis 14 * @since 2.0.0
mp-wp_genesis 15 * @uses $wp_object_cache Object Cache Class
mp-wp_genesis 16 * @see WP_Object_Cache::add()
mp-wp_genesis 17 *
mp-wp_genesis 18 * @param int|string $key The cache ID to use for retrieval later
mp-wp_genesis 19 * @param mixed $data The data to add to the cache store
mp-wp_genesis 20 * @param string $flag The group to add the cache to
mp-wp_genesis 21 * @param int $expire When the cache data should be expired
mp-wp_genesis 22 * @return unknown
mp-wp_genesis 23 */
mp-wp_genesis 24 function wp_cache_add($key, $data, $flag = '', $expire = 0) {
mp-wp_genesis 25 global $wp_object_cache;
mp-wp_genesis 26
mp-wp_genesis 27 return $wp_object_cache->add($key, $data, $flag, $expire);
mp-wp_genesis 28 }
mp-wp_genesis 29
mp-wp_genesis 30 /**
mp-wp_genesis 31 * Closes the cache.
mp-wp_genesis 32 *
mp-wp_genesis 33 * This function has ceased to do anything since WordPress 2.5. The
mp-wp_genesis 34 * functionality was removed along with the rest of the persistant cache. This
mp-wp_genesis 35 * does not mean that plugins can't implement this function when they need to
mp-wp_genesis 36 * make sure that the cache is cleaned up after WordPress no longer needs it.
mp-wp_genesis 37 *
mp-wp_genesis 38 * @since 2.0.0
mp-wp_genesis 39 *
mp-wp_genesis 40 * @return bool Always returns True
mp-wp_genesis 41 */
mp-wp_genesis 42 function wp_cache_close() {
mp-wp_genesis 43 return true;
mp-wp_genesis 44 }
mp-wp_genesis 45
mp-wp_genesis 46 /**
mp-wp_genesis 47 * Removes the cache contents matching ID and flag.
mp-wp_genesis 48 *
mp-wp_genesis 49 * @since 2.0.0
mp-wp_genesis 50 * @uses $wp_object_cache Object Cache Class
mp-wp_genesis 51 * @see WP_Object_Cache::delete()
mp-wp_genesis 52 *
mp-wp_genesis 53 * @param int|string $id What the contents in the cache are called
mp-wp_genesis 54 * @param string $flag Where the cache contents are grouped
mp-wp_genesis 55 * @return bool True on successful removal, false on failure
mp-wp_genesis 56 */
mp-wp_genesis 57 function wp_cache_delete($id, $flag = '') {
mp-wp_genesis 58 global $wp_object_cache;
mp-wp_genesis 59
mp-wp_genesis 60 return $wp_object_cache->delete($id, $flag);
mp-wp_genesis 61 }
mp-wp_genesis 62
mp-wp_genesis 63 /**
mp-wp_genesis 64 * Removes all cache items.
mp-wp_genesis 65 *
mp-wp_genesis 66 * @since 2.0.0
mp-wp_genesis 67 * @uses $wp_object_cache Object Cache Class
mp-wp_genesis 68 * @see WP_Object_Cache::flush()
mp-wp_genesis 69 *
mp-wp_genesis 70 * @return bool Always returns true
mp-wp_genesis 71 */
mp-wp_genesis 72 function wp_cache_flush() {
mp-wp_genesis 73 global $wp_object_cache;
mp-wp_genesis 74
mp-wp_genesis 75 return $wp_object_cache->flush();
mp-wp_genesis 76 }
mp-wp_genesis 77
mp-wp_genesis 78 /**
mp-wp_genesis 79 * Retrieves the cache contents from the cache by ID and flag.
mp-wp_genesis 80 *
mp-wp_genesis 81 * @since 2.0.0
mp-wp_genesis 82 * @uses $wp_object_cache Object Cache Class
mp-wp_genesis 83 * @see WP_Object_Cache::get()
mp-wp_genesis 84 *
mp-wp_genesis 85 * @param int|string $id What the contents in the cache are called
mp-wp_genesis 86 * @param string $flag Where the cache contents are grouped
mp-wp_genesis 87 * @return bool|mixed False on failure to retrieve contents or the cache
mp-wp_genesis 88 * contents on success
mp-wp_genesis 89 */
mp-wp_genesis 90 function wp_cache_get($id, $flag = '') {
mp-wp_genesis 91 global $wp_object_cache;
mp-wp_genesis 92
mp-wp_genesis 93 return $wp_object_cache->get($id, $flag);
mp-wp_genesis 94 }
mp-wp_genesis 95
mp-wp_genesis 96 /**
mp-wp_genesis 97 * Sets up Object Cache Global and assigns it.
mp-wp_genesis 98 *
mp-wp_genesis 99 * @since 2.0.0
mp-wp_genesis 100 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
mp-wp_genesis 101 */
mp-wp_genesis 102 function wp_cache_init() {
mp-wp_genesis 103 $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
mp-wp_genesis 104 }
mp-wp_genesis 105
mp-wp_genesis 106 /**
mp-wp_genesis 107 * Replaces the contents of the cache with new data.
mp-wp_genesis 108 *
mp-wp_genesis 109 * @since 2.0.0
mp-wp_genesis 110 * @uses $wp_object_cache Object Cache Class
mp-wp_genesis 111 * @see WP_Object_Cache::replace()
mp-wp_genesis 112 *
mp-wp_genesis 113 * @param int|string $id What to call the contents in the cache
mp-wp_genesis 114 * @param mixed $data The contents to store in the cache
mp-wp_genesis 115 * @param string $flag Where to group the cache contents
mp-wp_genesis 116 * @param int $expire When to expire the cache contents
mp-wp_genesis 117 * @return bool False if cache ID and group already exists, true on success
mp-wp_genesis 118 */
mp-wp_genesis 119 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
mp-wp_genesis 120 global $wp_object_cache;
mp-wp_genesis 121
mp-wp_genesis 122 return $wp_object_cache->replace($key, $data, $flag, $expire);
mp-wp_genesis 123 }
mp-wp_genesis 124
mp-wp_genesis 125 /**
mp-wp_genesis 126 * Saves the data to the cache.
mp-wp_genesis 127 *
mp-wp_genesis 128 * @since 2.0
mp-wp_genesis 129 * @uses $wp_object_cache Object Cache Class
mp-wp_genesis 130 * @see WP_Object_Cache::set()
mp-wp_genesis 131 *
mp-wp_genesis 132 * @param int|string $id What to call the contents in the cache
mp-wp_genesis 133 * @param mixed $data The contents to store in the cache
mp-wp_genesis 134 * @param string $flag Where to group the cache contents
mp-wp_genesis 135 * @param int $expire When to expire the cache contents
mp-wp_genesis 136 * @return bool False if cache ID and group already exists, true on success
mp-wp_genesis 137 */
mp-wp_genesis 138 function wp_cache_set($key, $data, $flag = '', $expire = 0) {
mp-wp_genesis 139 global $wp_object_cache;
mp-wp_genesis 140
mp-wp_genesis 141 return $wp_object_cache->set($key, $data, $flag, $expire);
mp-wp_genesis 142 }
mp-wp_genesis 143
mp-wp_genesis 144 /**
mp-wp_genesis 145 * Adds a group or set of groups to the list of global groups.
mp-wp_genesis 146 *
mp-wp_genesis 147 * @since 2.6.0
mp-wp_genesis 148 *
mp-wp_genesis 149 * @param string|array $groups A group or an array of groups to add
mp-wp_genesis 150 */
mp-wp_genesis 151 function wp_cache_add_global_groups( $groups ) {
mp-wp_genesis 152 // Default cache doesn't persist so nothing to do here.
mp-wp_genesis 153 return;
mp-wp_genesis 154 }
mp-wp_genesis 155
mp-wp_genesis 156 /**
mp-wp_genesis 157 * Adds a group or set of groups to the list of non-persistent groups.
mp-wp_genesis 158 *
mp-wp_genesis 159 * @since 2.6.0
mp-wp_genesis 160 *
mp-wp_genesis 161 * @param string|array $groups A group or an array of groups to add
mp-wp_genesis 162 */
mp-wp_genesis 163 function wp_cache_add_non_persistent_groups( $groups ) {
mp-wp_genesis 164 // Default cache doesn't persist so nothing to do here.
mp-wp_genesis 165 return;
mp-wp_genesis 166 }
mp-wp_genesis 167
mp-wp_genesis 168 /**
mp-wp_genesis 169 * WordPress Object Cache
mp-wp_genesis 170 *
mp-wp_genesis 171 * The WordPress Object Cache is used to save on trips to the database. The
mp-wp_genesis 172 * Object Cache stores all of the cache data to memory and makes the cache
mp-wp_genesis 173 * contents available by using a key, which is used to name and later retrieve
mp-wp_genesis 174 * the cache contents.
mp-wp_genesis 175 *
mp-wp_genesis 176 * The Object Cache can be replaced by other caching mechanisms by placing files
mp-wp_genesis 177 * in the wp-content folder which is looked at in wp-settings. If that file
mp-wp_genesis 178 * exists, then this file will not be included.
mp-wp_genesis 179 *
mp-wp_genesis 180 * @package WordPress
mp-wp_genesis 181 * @subpackage Cache
mp-wp_genesis 182 * @since 2.0
mp-wp_genesis 183 */
mp-wp_genesis 184 class WP_Object_Cache {
mp-wp_genesis 185
mp-wp_genesis 186 /**
mp-wp_genesis 187 * Holds the cached objects
mp-wp_genesis 188 *
mp-wp_genesis 189 * @var array
mp-wp_genesis 190 * @access private
mp-wp_genesis 191 * @since 2.0.0
mp-wp_genesis 192 */
mp-wp_genesis 193 var $cache = array ();
mp-wp_genesis 194
mp-wp_genesis 195 /**
mp-wp_genesis 196 * Cache objects that do not exist in the cache
mp-wp_genesis 197 *
mp-wp_genesis 198 * @var array
mp-wp_genesis 199 * @access private
mp-wp_genesis 200 * @since 2.0.0
mp-wp_genesis 201 */
mp-wp_genesis 202 var $non_existant_objects = array ();
mp-wp_genesis 203
mp-wp_genesis 204 /**
mp-wp_genesis 205 * The amount of times the cache data was already stored in the cache.
mp-wp_genesis 206 *
mp-wp_genesis 207 * @since 2.5.0
mp-wp_genesis 208 * @access private
mp-wp_genesis 209 * @var int
mp-wp_genesis 210 */
mp-wp_genesis 211 var $cache_hits = 0;
mp-wp_genesis 212
mp-wp_genesis 213 /**
mp-wp_genesis 214 * Amount of times the cache did not have the request in cache
mp-wp_genesis 215 *
mp-wp_genesis 216 * @var int
mp-wp_genesis 217 * @access public
mp-wp_genesis 218 * @since 2.0.0
mp-wp_genesis 219 */
mp-wp_genesis 220 var $cache_misses = 0;
mp-wp_genesis 221
mp-wp_genesis 222 /**
mp-wp_genesis 223 * Adds data to the cache if it doesn't already exist.
mp-wp_genesis 224 *
mp-wp_genesis 225 * @uses WP_Object_Cache::get Checks to see if the cache already has data.
mp-wp_genesis 226 * @uses WP_Object_Cache::set Sets the data after the checking the cache
mp-wp_genesis 227 * contents existance.
mp-wp_genesis 228 *
mp-wp_genesis 229 * @since 2.0.0
mp-wp_genesis 230 *
mp-wp_genesis 231 * @param int|string $id What to call the contents in the cache
mp-wp_genesis 232 * @param mixed $data The contents to store in the cache
mp-wp_genesis 233 * @param string $group Where to group the cache contents
mp-wp_genesis 234 * @param int $expire When to expire the cache contents
mp-wp_genesis 235 * @return bool False if cache ID and group already exists, true on success
mp-wp_genesis 236 */
mp-wp_genesis 237 function add($id, $data, $group = 'default', $expire = '') {
mp-wp_genesis 238 if (empty ($group))
mp-wp_genesis 239 $group = 'default';
mp-wp_genesis 240
mp-wp_genesis 241 if (false !== $this->get($id, $group, false))
mp-wp_genesis 242 return false;
mp-wp_genesis 243
mp-wp_genesis 244 return $this->set($id, $data, $group, $expire);
mp-wp_genesis 245 }
mp-wp_genesis 246
mp-wp_genesis 247 /**
mp-wp_genesis 248 * Remove the contents of the cache ID in the group
mp-wp_genesis 249 *
mp-wp_genesis 250 * If the cache ID does not exist in the group and $force parameter is set
mp-wp_genesis 251 * to false, then nothing will happen. The $force parameter is set to false
mp-wp_genesis 252 * by default.
mp-wp_genesis 253 *
mp-wp_genesis 254 * On success the group and the id will be added to the
mp-wp_genesis 255 * $non_existant_objects property in the class.
mp-wp_genesis 256 *
mp-wp_genesis 257 * @since 2.0.0
mp-wp_genesis 258 *
mp-wp_genesis 259 * @param int|string $id What the contents in the cache are called
mp-wp_genesis 260 * @param string $group Where the cache contents are grouped
mp-wp_genesis 261 * @param bool $force Optional. Whether to force the unsetting of the cache
mp-wp_genesis 262 * ID in the group
mp-wp_genesis 263 * @return bool False if the contents weren't deleted and true on success
mp-wp_genesis 264 */
mp-wp_genesis 265 function delete($id, $group = 'default', $force = false) {
mp-wp_genesis 266 if (empty ($group))
mp-wp_genesis 267 $group = 'default';
mp-wp_genesis 268
mp-wp_genesis 269 if (!$force && false === $this->get($id, $group, false))
mp-wp_genesis 270 return false;
mp-wp_genesis 271
mp-wp_genesis 272 unset ($this->cache[$group][$id]);
mp-wp_genesis 273 $this->non_existant_objects[$group][$id] = true;
mp-wp_genesis 274 return true;
mp-wp_genesis 275 }
mp-wp_genesis 276
mp-wp_genesis 277 /**
mp-wp_genesis 278 * Clears the object cache of all data
mp-wp_genesis 279 *
mp-wp_genesis 280 * @since 2.0.0
mp-wp_genesis 281 *
mp-wp_genesis 282 * @return bool Always returns true
mp-wp_genesis 283 */
mp-wp_genesis 284 function flush() {
mp-wp_genesis 285 $this->cache = array ();
mp-wp_genesis 286
mp-wp_genesis 287 return true;
mp-wp_genesis 288 }
mp-wp_genesis 289
mp-wp_genesis 290 /**
mp-wp_genesis 291 * Retrieves the cache contents, if it exists
mp-wp_genesis 292 *
mp-wp_genesis 293 * The contents will be first attempted to be retrieved by searching by the
mp-wp_genesis 294 * ID in the cache group. If the cache is hit (success) then the contents
mp-wp_genesis 295 * are returned.
mp-wp_genesis 296 *
mp-wp_genesis 297 * On failure, the $non_existant_objects property is checked and if the
mp-wp_genesis 298 * cache group and ID exist in there the cache misses will not be
mp-wp_genesis 299 * incremented. If not in the nonexistant objects property, then the cache
mp-wp_genesis 300 * misses will be incremented and the cache group and ID will be added to
mp-wp_genesis 301 * the nonexistant objects.
mp-wp_genesis 302 *
mp-wp_genesis 303 * @since 2.0.0
mp-wp_genesis 304 *
mp-wp_genesis 305 * @param int|string $id What the contents in the cache are called
mp-wp_genesis 306 * @param string $group Where the cache contents are grouped
mp-wp_genesis 307 * @return bool|mixed False on failure to retrieve contents or the cache
mp-wp_genesis 308 * contents on success
mp-wp_genesis 309 */
mp-wp_genesis 310 function get($id, $group = 'default') {
mp-wp_genesis 311 if (empty ($group))
mp-wp_genesis 312 $group = 'default';
mp-wp_genesis 313
mp-wp_genesis 314 if (isset ($this->cache[$group][$id])) {
mp-wp_genesis 315 $this->cache_hits += 1;
mp-wp_genesis 316 if ( is_object($this->cache[$group][$id]) )
mp-wp_genesis 317 return wp_clone($this->cache[$group][$id]);
mp-wp_genesis 318 else
mp-wp_genesis 319 return $this->cache[$group][$id];
mp-wp_genesis 320 }
mp-wp_genesis 321
mp-wp_genesis 322 if ( isset ($this->non_existant_objects[$group][$id]) )
mp-wp_genesis 323 return false;
mp-wp_genesis 324
mp-wp_genesis 325 $this->non_existant_objects[$group][$id] = true;
mp-wp_genesis 326 $this->cache_misses += 1;
mp-wp_genesis 327 return false;
mp-wp_genesis 328 }
mp-wp_genesis 329
mp-wp_genesis 330 /**
mp-wp_genesis 331 * Replace the contents in the cache, if contents already exist
mp-wp_genesis 332 *
mp-wp_genesis 333 * @since 2.0.0
mp-wp_genesis 334 * @see WP_Object_Cache::set()
mp-wp_genesis 335 *
mp-wp_genesis 336 * @param int|string $id What to call the contents in the cache
mp-wp_genesis 337 * @param mixed $data The contents to store in the cache
mp-wp_genesis 338 * @param string $group Where to group the cache contents
mp-wp_genesis 339 * @param int $expire When to expire the cache contents
mp-wp_genesis 340 * @return bool False if not exists, true if contents were replaced
mp-wp_genesis 341 */
mp-wp_genesis 342 function replace($id, $data, $group = 'default', $expire = '') {
mp-wp_genesis 343 if (empty ($group))
mp-wp_genesis 344 $group = 'default';
mp-wp_genesis 345
mp-wp_genesis 346 if (false === $this->get($id, $group, false))
mp-wp_genesis 347 return false;
mp-wp_genesis 348
mp-wp_genesis 349 return $this->set($id, $data, $group, $expire);
mp-wp_genesis 350 }
mp-wp_genesis 351
mp-wp_genesis 352 /**
mp-wp_genesis 353 * Sets the data contents into the cache
mp-wp_genesis 354 *
mp-wp_genesis 355 * The cache contents is grouped by the $group parameter followed by the
mp-wp_genesis 356 * $id. This allows for duplicate ids in unique groups. Therefore, naming of
mp-wp_genesis 357 * the group should be used with care and should follow normal function
mp-wp_genesis 358 * naming guidelines outside of core WordPress usage.
mp-wp_genesis 359 *
mp-wp_genesis 360 * The $expire parameter is not used, because the cache will automatically
mp-wp_genesis 361 * expire for each time a page is accessed and PHP finishes. The method is
mp-wp_genesis 362 * more for cache plugins which use files.
mp-wp_genesis 363 *
mp-wp_genesis 364 * @since 2.0.0
mp-wp_genesis 365 *
mp-wp_genesis 366 * @param int|string $id What to call the contents in the cache
mp-wp_genesis 367 * @param mixed $data The contents to store in the cache
mp-wp_genesis 368 * @param string $group Where to group the cache contents
mp-wp_genesis 369 * @param int $expire Not Used
mp-wp_genesis 370 * @return bool Always returns true
mp-wp_genesis 371 */
mp-wp_genesis 372 function set($id, $data, $group = 'default', $expire = '') {
mp-wp_genesis 373 if (empty ($group))
mp-wp_genesis 374 $group = 'default';
mp-wp_genesis 375
mp-wp_genesis 376 if (NULL === $data)
mp-wp_genesis 377 $data = '';
mp-wp_genesis 378
mp-wp_genesis 379 if ( is_object($data) )
mp-wp_genesis 380 $data = wp_clone($data);
mp-wp_genesis 381
mp-wp_genesis 382 $this->cache[$group][$id] = $data;
mp-wp_genesis 383
mp-wp_genesis 384 if(isset($this->non_existant_objects[$group][$id]))
mp-wp_genesis 385 unset ($this->non_existant_objects[$group][$id]);
mp-wp_genesis 386
mp-wp_genesis 387 return true;
mp-wp_genesis 388 }
mp-wp_genesis 389
mp-wp_genesis 390 /**
mp-wp_genesis 391 * Echos the stats of the caching.
mp-wp_genesis 392 *
mp-wp_genesis 393 * Gives the cache hits, and cache misses. Also prints every cached group,
mp-wp_genesis 394 * key and the data.
mp-wp_genesis 395 *
mp-wp_genesis 396 * @since 2.0.0
mp-wp_genesis 397 */
mp-wp_genesis 398 function stats() {
mp-wp_genesis 399 echo "<p>";
mp-wp_genesis 400 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
mp-wp_genesis 401 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
mp-wp_genesis 402 echo "</p>";
mp-wp_genesis 403
mp-wp_genesis 404 foreach ($this->cache as $group => $cache) {
mp-wp_genesis 405 echo "<p>";
mp-wp_genesis 406 echo "<strong>Group:</strong> $group<br />";
mp-wp_genesis 407 echo "<strong>Cache:</strong>";
mp-wp_genesis 408 echo "<pre>";
mp-wp_genesis 409 print_r($cache);
mp-wp_genesis 410 echo "</pre>";
mp-wp_genesis 411 }
mp-wp_genesis 412 }
mp-wp_genesis 413
mp-wp_genesis 414 /**
mp-wp_genesis 415 * PHP4 constructor; Calls PHP 5 style constructor
mp-wp_genesis 416 *
mp-wp_genesis 417 * @since 2.0.0
mp-wp_genesis 418 *
mp-wp_genesis 419 * @return WP_Object_Cache
mp-wp_genesis 420 */
mp-wp_genesis 421 function WP_Object_Cache() {
mp-wp_genesis 422 return $this->__construct();
mp-wp_genesis 423 }
mp-wp_genesis 424
mp-wp_genesis 425 /**
mp-wp_genesis 426 * Sets up object properties; PHP 5 style constructor
mp-wp_genesis 427 *
mp-wp_genesis 428 * @since 2.0.8
mp-wp_genesis 429 * @return null|WP_Object_Cache If cache is disabled, returns null.
mp-wp_genesis 430 */
mp-wp_genesis 431 function __construct() {
mp-wp_genesis 432 /**
mp-wp_genesis 433 * @todo This should be moved to the PHP4 style constructor, PHP5
mp-wp_genesis 434 * already calls __destruct()
mp-wp_genesis 435 */
mp-wp_genesis 436 register_shutdown_function(array(&$this, "__destruct"));
mp-wp_genesis 437 }
mp-wp_genesis 438
mp-wp_genesis 439 /**
mp-wp_genesis 440 * Will save the object cache before object is completely destroyed.
mp-wp_genesis 441 *
mp-wp_genesis 442 * Called upon object destruction, which should be when PHP ends.
mp-wp_genesis 443 *
mp-wp_genesis 444 * @since 2.0.8
mp-wp_genesis 445 *
mp-wp_genesis 446 * @return bool True value. Won't be used by PHP
mp-wp_genesis 447 */
mp-wp_genesis 448 function __destruct() {
mp-wp_genesis 449 return true;
mp-wp_genesis 450 }
mp-wp_genesis 451 }
mp-wp_genesis 452 ?>