101 * @see WP_Object_Cache::get() |
101 * @see WP_Object_Cache::get() |
102 * |
102 * |
103 * @param int|string $key What the contents in the cache are called |
103 * @param int|string $key What the contents in the cache are called |
104 * @param string $group 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) |
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. |
106 * @param bool &$found Whether key was found in the cache. Disambiguates a return of false, a storable value. |
107 * @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 |
108 * contents on success |
108 * contents on success |
109 */ |
109 */ |
110 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
110 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
111 global $wp_object_cache; |
111 global $wp_object_cache; |
161 } |
161 } |
162 |
162 |
163 /** |
163 /** |
164 * Saves the data to the cache. |
164 * Saves the data to the cache. |
165 * |
165 * |
166 * @since 2.0 |
166 * @since 2.0.0 |
|
167 * |
167 * @uses $wp_object_cache Object Cache Class |
168 * @uses $wp_object_cache Object Cache Class |
168 * @see WP_Object_Cache::set() |
169 * @see WP_Object_Cache::set() |
169 * |
170 * |
170 * @param int|string $key What to call the contents in the cache |
171 * @param int|string $key What to call the contents in the cache |
171 * @param mixed $data The contents to store in the cache |
172 * @param mixed $data The contents to store in the cache |
214 * |
215 * |
215 * @param string|array $groups A group or an array of groups to add |
216 * @param string|array $groups A group or an array of groups to add |
216 */ |
217 */ |
217 function wp_cache_add_non_persistent_groups( $groups ) { |
218 function wp_cache_add_non_persistent_groups( $groups ) { |
218 // Default cache doesn't persist so nothing to do here. |
219 // Default cache doesn't persist so nothing to do here. |
219 return; |
|
220 } |
220 } |
221 |
221 |
222 /** |
222 /** |
223 * Reset internal cache keys and structures. If the cache backend uses global |
223 * Reset internal cache keys and structures. If the cache backend uses global |
224 * blog or site IDs as part of its cache keys, this function instructs the |
224 * blog or site IDs as part of its cache keys, this function instructs the |
254 * in the wp-content folder which is looked at in wp-settings. If that file |
254 * in the wp-content folder which is looked at in wp-settings. If that file |
255 * exists, then this file will not be included. |
255 * exists, then this file will not be included. |
256 * |
256 * |
257 * @package WordPress |
257 * @package WordPress |
258 * @subpackage Cache |
258 * @subpackage Cache |
259 * @since 2.0 |
259 * @since 2.0.0 |
260 */ |
260 */ |
261 class WP_Object_Cache { |
261 class WP_Object_Cache { |
262 |
262 |
263 /** |
263 /** |
264 * Holds the cached objects |
264 * Holds the cached objects |
265 * |
265 * |
266 * @var array |
266 * @var array |
267 * @access private |
267 * @access private |
268 * @since 2.0.0 |
268 * @since 2.0.0 |
269 */ |
269 */ |
270 var $cache = array (); |
270 private $cache = array(); |
271 |
271 |
272 /** |
272 /** |
273 * The amount of times the cache data was already stored in the cache. |
273 * The amount of times the cache data was already stored in the cache. |
274 * |
274 * |
275 * @since 2.5.0 |
275 * @since 2.5.0 |
276 * @access private |
276 * @access private |
277 * @var int |
277 * @var int |
278 */ |
278 */ |
279 var $cache_hits = 0; |
279 private $cache_hits = 0; |
280 |
280 |
281 /** |
281 /** |
282 * Amount of times the cache did not have the request in cache |
282 * Amount of times the cache did not have the request in cache |
283 * |
283 * |
284 * @var int |
284 * @var int |
285 * @access public |
285 * @access public |
286 * @since 2.0.0 |
286 * @since 2.0.0 |
287 */ |
287 */ |
288 var $cache_misses = 0; |
288 public $cache_misses = 0; |
289 |
289 |
290 /** |
290 /** |
291 * List of global groups |
291 * List of global groups |
292 * |
292 * |
293 * @var array |
293 * @var array |
294 * @access protected |
294 * @access protected |
295 * @since 3.0.0 |
295 * @since 3.0.0 |
296 */ |
296 */ |
297 var $global_groups = array(); |
297 protected $global_groups = array(); |
298 |
298 |
299 /** |
299 /** |
300 * The blog prefix to prepend to keys in non-global groups. |
300 * The blog prefix to prepend to keys in non-global groups. |
301 * |
301 * |
302 * @var int |
302 * @var int |
303 * @access private |
303 * @access private |
304 * @since 3.5.0 |
304 * @since 3.5.0 |
305 */ |
305 */ |
306 var $blog_prefix; |
306 private $blog_prefix; |
|
307 |
|
308 /** |
|
309 * Holds the value of `is_multisite()` |
|
310 * |
|
311 * @var bool |
|
312 * @access private |
|
313 * @since 3.5.0 |
|
314 */ |
|
315 private $multisite; |
|
316 |
|
317 /** |
|
318 * Make private properties readable for backwards compatibility. |
|
319 * |
|
320 * @since 4.0.0 |
|
321 * @access public |
|
322 * |
|
323 * @param string $name Property to get. |
|
324 * @return mixed Property. |
|
325 */ |
|
326 public function __get( $name ) { |
|
327 return $this->$name; |
|
328 } |
|
329 |
|
330 /** |
|
331 * Make private properties settable for backwards compatibility. |
|
332 * |
|
333 * @since 4.0.0 |
|
334 * @access public |
|
335 * |
|
336 * @param string $name Property to set. |
|
337 * @param mixed $value Property value. |
|
338 * @return mixed Newly-set property. |
|
339 */ |
|
340 public function __set( $name, $value ) { |
|
341 return $this->$name = $value; |
|
342 } |
|
343 |
|
344 /** |
|
345 * Make private properties checkable for backwards compatibility. |
|
346 * |
|
347 * @since 4.0.0 |
|
348 * @access public |
|
349 * |
|
350 * @param string $name Property to check if set. |
|
351 * @return bool Whether the property is set. |
|
352 */ |
|
353 public function __isset( $name ) { |
|
354 return isset( $this->$name ); |
|
355 } |
|
356 |
|
357 /** |
|
358 * Make private properties un-settable for backwards compatibility. |
|
359 * |
|
360 * @since 4.0.0 |
|
361 * @access public |
|
362 * |
|
363 * @param string $name Property to unset. |
|
364 */ |
|
365 public function __unset( $name ) { |
|
366 unset( $this->$name ); |
|
367 } |
307 |
368 |
308 /** |
369 /** |
309 * Adds data to the cache if it doesn't already exist. |
370 * Adds data to the cache if it doesn't already exist. |
310 * |
371 * |
311 * @uses WP_Object_Cache::_exists Checks to see if the cache already has data. |
372 * @uses WP_Object_Cache::_exists Checks to see if the cache already has data. |
318 * @param mixed $data The contents to store in the cache |
379 * @param mixed $data The contents to store in the cache |
319 * @param string $group Where to group the cache contents |
380 * @param string $group Where to group the cache contents |
320 * @param int $expire When to expire the cache contents |
381 * @param int $expire When to expire the cache contents |
321 * @return bool False if cache key and group already exist, true on success |
382 * @return bool False if cache key and group already exist, true on success |
322 */ |
383 */ |
323 function add( $key, $data, $group = 'default', $expire = 0 ) { |
384 public function add( $key, $data, $group = 'default', $expire = 0 ) { |
324 if ( wp_suspend_cache_addition() ) |
385 if ( wp_suspend_cache_addition() ) |
325 return false; |
386 return false; |
326 |
387 |
327 if ( empty( $group ) ) |
388 if ( empty( $group ) ) |
328 $group = 'default'; |
389 $group = 'default'; |
342 * |
403 * |
343 * @since 3.0.0 |
404 * @since 3.0.0 |
344 * |
405 * |
345 * @param array $groups List of groups that are global. |
406 * @param array $groups List of groups that are global. |
346 */ |
407 */ |
347 function add_global_groups( $groups ) { |
408 public function add_global_groups( $groups ) { |
348 $groups = (array) $groups; |
409 $groups = (array) $groups; |
349 |
410 |
350 $groups = array_fill_keys( $groups, true ); |
411 $groups = array_fill_keys( $groups, true ); |
351 $this->global_groups = array_merge( $this->global_groups, $groups ); |
412 $this->global_groups = array_merge( $this->global_groups, $groups ); |
352 } |
413 } |
359 * @param int|string $key The cache key to increment |
420 * @param int|string $key The cache key to increment |
360 * @param int $offset The amount by which to decrement the item's value. Default is 1. |
421 * @param int $offset The amount by which to decrement the item's value. Default is 1. |
361 * @param string $group The group the key is in. |
422 * @param string $group The group the key is in. |
362 * @return false|int False on failure, the item's new value on success. |
423 * @return false|int False on failure, the item's new value on success. |
363 */ |
424 */ |
364 function decr( $key, $offset = 1, $group = 'default' ) { |
425 public function decr( $key, $offset = 1, $group = 'default' ) { |
365 if ( empty( $group ) ) |
426 if ( empty( $group ) ) |
366 $group = 'default'; |
427 $group = 'default'; |
367 |
428 |
368 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
429 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
369 $key = $this->blog_prefix . $key; |
430 $key = $this->blog_prefix . $key; |
385 } |
446 } |
386 |
447 |
387 /** |
448 /** |
388 * Remove the contents of the cache key in the group |
449 * Remove the contents of the cache key in the group |
389 * |
450 * |
390 * If the cache key does not exist in the group and $force parameter is set |
451 * If the cache key does not exist in the group, then nothing will happen. |
391 * to false, then nothing will happen. The $force parameter is set to false |
|
392 * by default. |
|
393 * |
452 * |
394 * @since 2.0.0 |
453 * @since 2.0.0 |
395 * |
454 * |
396 * @param int|string $key What the contents in the cache are called |
455 * @param int|string $key What the contents in the cache are called |
397 * @param string $group Where the cache contents are grouped |
456 * @param string $group Where the cache contents are grouped |
398 * @param bool $force Optional. Whether to force the unsetting of the cache |
457 * @param bool $deprecated Deprecated. |
399 * key in the group |
458 * |
400 * @return bool False if the contents weren't deleted and true on success |
459 * @return bool False if the contents weren't deleted and true on success |
401 */ |
460 */ |
402 function delete($key, $group = 'default', $force = false) { |
461 public function delete( $key, $group = 'default', $deprecated = false ) { |
403 if ( empty( $group ) ) |
462 if ( empty( $group ) ) |
404 $group = 'default'; |
463 $group = 'default'; |
405 |
464 |
406 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
465 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
407 $key = $this->blog_prefix . $key; |
466 $key = $this->blog_prefix . $key; |
408 |
467 |
409 if ( ! $force && ! $this->_exists( $key, $group ) ) |
468 if ( ! $this->_exists( $key, $group ) ) |
410 return false; |
469 return false; |
411 |
470 |
412 unset( $this->cache[$group][$key] ); |
471 unset( $this->cache[$group][$key] ); |
413 return true; |
472 return true; |
414 } |
473 } |
441 * @param string $group Where the cache contents are grouped |
500 * @param string $group Where the cache contents are grouped |
442 * @param string $force Whether to force a refetch rather than relying on the local cache (default is false) |
501 * @param string $force Whether to force a refetch rather than relying on the local cache (default is false) |
443 * @return bool|mixed False on failure to retrieve contents or the cache |
502 * @return bool|mixed False on failure to retrieve contents or the cache |
444 * contents on success |
503 * contents on success |
445 */ |
504 */ |
446 function get( $key, $group = 'default', $force = false, &$found = null ) { |
505 public function get( $key, $group = 'default', $force = false, &$found = null ) { |
447 if ( empty( $group ) ) |
506 if ( empty( $group ) ) |
448 $group = 'default'; |
507 $group = 'default'; |
449 |
508 |
450 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
509 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
451 $key = $this->blog_prefix . $key; |
510 $key = $this->blog_prefix . $key; |
472 * @param int|string $key The cache key to increment |
531 * @param int|string $key The cache key to increment |
473 * @param int $offset The amount by which to increment the item's value. Default is 1. |
532 * @param int $offset The amount by which to increment the item's value. Default is 1. |
474 * @param string $group The group the key is in. |
533 * @param string $group The group the key is in. |
475 * @return false|int False on failure, the item's new value on success. |
534 * @return false|int False on failure, the item's new value on success. |
476 */ |
535 */ |
477 function incr( $key, $offset = 1, $group = 'default' ) { |
536 public function incr( $key, $offset = 1, $group = 'default' ) { |
478 if ( empty( $group ) ) |
537 if ( empty( $group ) ) |
479 $group = 'default'; |
538 $group = 'default'; |
480 |
539 |
481 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
540 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
482 $key = $this->blog_prefix . $key; |
541 $key = $this->blog_prefix . $key; |
507 * @param mixed $data The contents to store in the cache |
566 * @param mixed $data The contents to store in the cache |
508 * @param string $group Where to group the cache contents |
567 * @param string $group Where to group the cache contents |
509 * @param int $expire When to expire the cache contents |
568 * @param int $expire When to expire the cache contents |
510 * @return bool False if not exists, true if contents were replaced |
569 * @return bool False if not exists, true if contents were replaced |
511 */ |
570 */ |
512 function replace( $key, $data, $group = 'default', $expire = 0 ) { |
571 public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
513 if ( empty( $group ) ) |
572 if ( empty( $group ) ) |
514 $group = 'default'; |
573 $group = 'default'; |
515 |
574 |
516 $id = $key; |
575 $id = $key; |
517 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
576 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
527 * Reset keys |
586 * Reset keys |
528 * |
587 * |
529 * @since 3.0.0 |
588 * @since 3.0.0 |
530 * @deprecated 3.5.0 |
589 * @deprecated 3.5.0 |
531 */ |
590 */ |
532 function reset() { |
591 public function reset() { |
533 _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' ); |
592 _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' ); |
534 |
593 |
535 // Clear out non-global caches since the blog ID has changed. |
594 // Clear out non-global caches since the blog ID has changed. |
536 foreach ( array_keys( $this->cache ) as $group ) { |
595 foreach ( array_keys( $this->cache ) as $group ) { |
537 if ( ! isset( $this->global_groups[ $group ] ) ) |
596 if ( ! isset( $this->global_groups[ $group ] ) ) |
557 * @param mixed $data The contents to store in the cache |
616 * @param mixed $data The contents to store in the cache |
558 * @param string $group Where to group the cache contents |
617 * @param string $group Where to group the cache contents |
559 * @param int $expire Not Used |
618 * @param int $expire Not Used |
560 * @return bool Always returns true |
619 * @return bool Always returns true |
561 */ |
620 */ |
562 function set( $key, $data, $group = 'default', $expire = 0 ) { |
621 public function set( $key, $data, $group = 'default', $expire = 0 ) { |
563 if ( empty( $group ) ) |
622 if ( empty( $group ) ) |
564 $group = 'default'; |
623 $group = 'default'; |
565 |
624 |
566 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
625 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
567 $key = $this->blog_prefix . $key; |
626 $key = $this->blog_prefix . $key; |
600 * |
659 * |
601 * @since 3.5.0 |
660 * @since 3.5.0 |
602 * |
661 * |
603 * @param int $blog_id Blog ID |
662 * @param int $blog_id Blog ID |
604 */ |
663 */ |
605 function switch_to_blog( $blog_id ) { |
664 public function switch_to_blog( $blog_id ) { |
606 $blog_id = (int) $blog_id; |
665 $blog_id = (int) $blog_id; |
607 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
666 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
608 } |
667 } |
609 |
668 |
610 /** |
669 /** |
611 * Utility function to determine whether a key exists in the cache. |
670 * Utility function to determine whether a key exists in the cache. |
612 * |
671 * |
613 * @since 3.4.0 |
672 * @since 3.4.0 |
614 * |
673 * |
615 * @access protected |
674 * @access protected |
|
675 * @param string $key |
|
676 * @param string $group |
|
677 * @return bool |
616 */ |
678 */ |
617 protected function _exists( $key, $group ) { |
679 protected function _exists( $key, $group ) { |
618 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); |
680 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); |
619 } |
681 } |
620 |
682 |
621 /** |
683 /** |
622 * Sets up object properties; PHP 5 style constructor |
684 * Sets up object properties; PHP 5 style constructor |
623 * |
685 * |
624 * @since 2.0.8 |
686 * @since 2.0.8 |
625 * @return null|WP_Object_Cache If cache is disabled, returns null. |
687 */ |
626 */ |
688 public function __construct() { |
627 function __construct() { |
|
628 global $blog_id; |
689 global $blog_id; |
629 |
690 |
630 $this->multisite = is_multisite(); |
691 $this->multisite = is_multisite(); |
631 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
692 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
632 |
693 |