79 * @return bool True on successful removal, false on failure. |
79 * @return bool True on successful removal, false on failure. |
80 */ |
80 */ |
81 function wp_cache_delete( $key, $group = '' ) { |
81 function wp_cache_delete( $key, $group = '' ) { |
82 global $wp_object_cache; |
82 global $wp_object_cache; |
83 |
83 |
84 return $wp_object_cache->delete($key, $group); |
84 return $wp_object_cache->delete( $key, $group ); |
85 } |
85 } |
86 |
86 |
87 /** |
87 /** |
88 * Removes all cache items. |
88 * Removes all cache items. |
89 * |
89 * |
113 * @param bool $force Optional. Whether to force an update of the local cache from the persistent |
113 * @param bool $force Optional. Whether to force an update of the local cache from the persistent |
114 * cache. Default false. |
114 * cache. Default false. |
115 * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
115 * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
116 * Disambiguates a return of false, a storable value. Default null. |
116 * Disambiguates a return of false, a storable value. Default null. |
117 * @return bool|mixed False on failure to retrieve contents or the cache |
117 * @return bool|mixed False on failure to retrieve contents or the cache |
118 * contents on success |
118 * contents on success |
119 */ |
119 */ |
120 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
120 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
121 global $wp_object_cache; |
121 global $wp_object_cache; |
122 |
122 |
123 return $wp_object_cache->get( $key, $group, $force, $found ); |
123 return $wp_object_cache->get( $key, $group, $force, $found ); |
388 * |
388 * |
389 * @since 2.0.0 |
389 * @since 2.0.0 |
390 * |
390 * |
391 * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data. |
391 * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data. |
392 * @uses WP_Object_Cache::set() Sets the data after the checking the cache |
392 * @uses WP_Object_Cache::set() Sets the data after the checking the cache |
393 * contents existence. |
393 * contents existence. |
394 * |
394 * |
395 * @param int|string $key What to call the contents in the cache. |
395 * @param int|string $key What to call the contents in the cache. |
396 * @param mixed $data The contents to store in the cache. |
396 * @param mixed $data The contents to store in the cache. |
397 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
397 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
398 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
398 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
399 * @return bool False if cache key and group already exist, true on success |
399 * @return bool False if cache key and group already exist, true on success |
400 */ |
400 */ |
401 public function add( $key, $data, $group = 'default', $expire = 0 ) { |
401 public function add( $key, $data, $group = 'default', $expire = 0 ) { |
402 if ( wp_suspend_cache_addition() ) |
402 if ( wp_suspend_cache_addition() ) { |
403 return false; |
403 return false; |
404 |
404 } |
405 if ( empty( $group ) ) |
405 |
|
406 if ( empty( $group ) ) { |
406 $group = 'default'; |
407 $group = 'default'; |
|
408 } |
407 |
409 |
408 $id = $key; |
410 $id = $key; |
409 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
411 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
410 $id = $this->blog_prefix . $key; |
412 $id = $this->blog_prefix . $key; |
411 |
413 } |
412 if ( $this->_exists( $id, $group ) ) |
414 |
|
415 if ( $this->_exists( $id, $group ) ) { |
413 return false; |
416 return false; |
|
417 } |
414 |
418 |
415 return $this->set( $key, $data, $group, (int) $expire ); |
419 return $this->set( $key, $data, $group, (int) $expire ); |
416 } |
420 } |
417 |
421 |
418 /** |
422 /** |
423 * @param array $groups List of groups that are global. |
427 * @param array $groups List of groups that are global. |
424 */ |
428 */ |
425 public function add_global_groups( $groups ) { |
429 public function add_global_groups( $groups ) { |
426 $groups = (array) $groups; |
430 $groups = (array) $groups; |
427 |
431 |
428 $groups = array_fill_keys( $groups, true ); |
432 $groups = array_fill_keys( $groups, true ); |
429 $this->global_groups = array_merge( $this->global_groups, $groups ); |
433 $this->global_groups = array_merge( $this->global_groups, $groups ); |
430 } |
434 } |
431 |
435 |
432 /** |
436 /** |
433 * Decrements numeric cache item's value. |
437 * Decrements numeric cache item's value. |
438 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. |
442 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. |
439 * @param string $group Optional. The group the key is in. Default 'default'. |
443 * @param string $group Optional. The group the key is in. Default 'default'. |
440 * @return false|int False on failure, the item's new value on success. |
444 * @return false|int False on failure, the item's new value on success. |
441 */ |
445 */ |
442 public function decr( $key, $offset = 1, $group = 'default' ) { |
446 public function decr( $key, $offset = 1, $group = 'default' ) { |
443 if ( empty( $group ) ) |
447 if ( empty( $group ) ) { |
444 $group = 'default'; |
448 $group = 'default'; |
445 |
449 } |
446 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
450 |
|
451 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
447 $key = $this->blog_prefix . $key; |
452 $key = $this->blog_prefix . $key; |
448 |
453 } |
449 if ( ! $this->_exists( $key, $group ) ) |
454 |
|
455 if ( ! $this->_exists( $key, $group ) ) { |
450 return false; |
456 return false; |
451 |
457 } |
452 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
458 |
|
459 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
453 $this->cache[ $group ][ $key ] = 0; |
460 $this->cache[ $group ][ $key ] = 0; |
|
461 } |
454 |
462 |
455 $offset = (int) $offset; |
463 $offset = (int) $offset; |
456 |
464 |
457 $this->cache[ $group ][ $key ] -= $offset; |
465 $this->cache[ $group ][ $key ] -= $offset; |
458 |
466 |
459 if ( $this->cache[ $group ][ $key ] < 0 ) |
467 if ( $this->cache[ $group ][ $key ] < 0 ) { |
460 $this->cache[ $group ][ $key ] = 0; |
468 $this->cache[ $group ][ $key ] = 0; |
|
469 } |
461 |
470 |
462 return $this->cache[ $group ][ $key ]; |
471 return $this->cache[ $group ][ $key ]; |
463 } |
472 } |
464 |
473 |
465 /** |
474 /** |
473 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
482 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
474 * @param bool $deprecated Optional. Unused. Default false. |
483 * @param bool $deprecated Optional. Unused. Default false. |
475 * @return bool False if the contents weren't deleted and true on success. |
484 * @return bool False if the contents weren't deleted and true on success. |
476 */ |
485 */ |
477 public function delete( $key, $group = 'default', $deprecated = false ) { |
486 public function delete( $key, $group = 'default', $deprecated = false ) { |
478 if ( empty( $group ) ) |
487 if ( empty( $group ) ) { |
479 $group = 'default'; |
488 $group = 'default'; |
480 |
489 } |
481 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
490 |
|
491 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
482 $key = $this->blog_prefix . $key; |
492 $key = $this->blog_prefix . $key; |
483 |
493 } |
484 if ( ! $this->_exists( $key, $group ) ) |
494 |
|
495 if ( ! $this->_exists( $key, $group ) ) { |
485 return false; |
496 return false; |
486 |
497 } |
487 unset( $this->cache[$group][$key] ); |
498 |
|
499 unset( $this->cache[ $group ][ $key ] ); |
488 return true; |
500 return true; |
489 } |
501 } |
490 |
502 |
491 /** |
503 /** |
492 * Clears the object cache of all data. |
504 * Clears the object cache of all data. |
512 * |
524 * |
513 * @since 2.0.0 |
525 * @since 2.0.0 |
514 * |
526 * |
515 * @param int|string $key What the contents in the cache are called. |
527 * @param int|string $key What the contents in the cache are called. |
516 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
528 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
517 * @param string $force Optional. Unused. Whether to force a refetch rather than relying on the local |
529 * @param bool $force Optional. Unused. Whether to force a refetch rather than relying on the local |
518 * cache. Default false. |
530 * cache. Default false. |
519 * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
531 * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
520 * Disambiguates a return of false, a storable value. Default null. |
532 * Disambiguates a return of false, a storable value. Default null. |
521 * @return false|mixed False on failure to retrieve contents or the cache contents on success. |
533 * @return false|mixed False on failure to retrieve contents or the cache contents on success. |
522 */ |
534 */ |
523 public function get( $key, $group = 'default', $force = false, &$found = null ) { |
535 public function get( $key, $group = 'default', $force = false, &$found = null ) { |
524 if ( empty( $group ) ) |
536 if ( empty( $group ) ) { |
525 $group = 'default'; |
537 $group = 'default'; |
526 |
538 } |
527 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
539 |
|
540 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
528 $key = $this->blog_prefix . $key; |
541 $key = $this->blog_prefix . $key; |
|
542 } |
529 |
543 |
530 if ( $this->_exists( $key, $group ) ) { |
544 if ( $this->_exists( $key, $group ) ) { |
531 $found = true; |
545 $found = true; |
532 $this->cache_hits += 1; |
546 $this->cache_hits += 1; |
533 if ( is_object($this->cache[$group][$key]) ) |
547 if ( is_object( $this->cache[ $group ][ $key ] ) ) { |
534 return clone $this->cache[$group][$key]; |
548 return clone $this->cache[ $group ][ $key ]; |
535 else |
549 } else { |
536 return $this->cache[$group][$key]; |
550 return $this->cache[ $group ][ $key ]; |
537 } |
551 } |
538 |
552 } |
539 $found = false; |
553 |
|
554 $found = false; |
540 $this->cache_misses += 1; |
555 $this->cache_misses += 1; |
541 return false; |
556 return false; |
542 } |
557 } |
543 |
558 |
544 /** |
559 /** |
550 * @param int $offset Optional. The amount by which to increment the item's value. Default 1. |
565 * @param int $offset Optional. The amount by which to increment the item's value. Default 1. |
551 * @param string $group Optional. The group the key is in. Default 'default'. |
566 * @param string $group Optional. The group the key is in. Default 'default'. |
552 * @return false|int False on failure, the item's new value on success. |
567 * @return false|int False on failure, the item's new value on success. |
553 */ |
568 */ |
554 public function incr( $key, $offset = 1, $group = 'default' ) { |
569 public function incr( $key, $offset = 1, $group = 'default' ) { |
555 if ( empty( $group ) ) |
570 if ( empty( $group ) ) { |
556 $group = 'default'; |
571 $group = 'default'; |
557 |
572 } |
558 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
573 |
|
574 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
559 $key = $this->blog_prefix . $key; |
575 $key = $this->blog_prefix . $key; |
560 |
576 } |
561 if ( ! $this->_exists( $key, $group ) ) |
577 |
|
578 if ( ! $this->_exists( $key, $group ) ) { |
562 return false; |
579 return false; |
563 |
580 } |
564 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
581 |
|
582 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
565 $this->cache[ $group ][ $key ] = 0; |
583 $this->cache[ $group ][ $key ] = 0; |
|
584 } |
566 |
585 |
567 $offset = (int) $offset; |
586 $offset = (int) $offset; |
568 |
587 |
569 $this->cache[ $group ][ $key ] += $offset; |
588 $this->cache[ $group ][ $key ] += $offset; |
570 |
589 |
571 if ( $this->cache[ $group ][ $key ] < 0 ) |
590 if ( $this->cache[ $group ][ $key ] < 0 ) { |
572 $this->cache[ $group ][ $key ] = 0; |
591 $this->cache[ $group ][ $key ] = 0; |
|
592 } |
573 |
593 |
574 return $this->cache[ $group ][ $key ]; |
594 return $this->cache[ $group ][ $key ]; |
575 } |
595 } |
576 |
596 |
577 /** |
597 /** |
586 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
606 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
587 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
607 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
588 * @return bool False if not exists, true if contents were replaced. |
608 * @return bool False if not exists, true if contents were replaced. |
589 */ |
609 */ |
590 public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
610 public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
591 if ( empty( $group ) ) |
611 if ( empty( $group ) ) { |
592 $group = 'default'; |
612 $group = 'default'; |
|
613 } |
593 |
614 |
594 $id = $key; |
615 $id = $key; |
595 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
616 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
596 $id = $this->blog_prefix . $key; |
617 $id = $this->blog_prefix . $key; |
597 |
618 } |
598 if ( ! $this->_exists( $id, $group ) ) |
619 |
|
620 if ( ! $this->_exists( $id, $group ) ) { |
599 return false; |
621 return false; |
|
622 } |
600 |
623 |
601 return $this->set( $key, $data, $group, (int) $expire ); |
624 return $this->set( $key, $data, $group, (int) $expire ); |
602 } |
625 } |
603 |
626 |
604 /** |
627 /** |
612 public function reset() { |
635 public function reset() { |
613 _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' ); |
636 _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' ); |
614 |
637 |
615 // Clear out non-global caches since the blog ID has changed. |
638 // Clear out non-global caches since the blog ID has changed. |
616 foreach ( array_keys( $this->cache ) as $group ) { |
639 foreach ( array_keys( $this->cache ) as $group ) { |
617 if ( ! isset( $this->global_groups[ $group ] ) ) |
640 if ( ! isset( $this->global_groups[ $group ] ) ) { |
618 unset( $this->cache[ $group ] ); |
641 unset( $this->cache[ $group ] ); |
|
642 } |
619 } |
643 } |
620 } |
644 } |
621 |
645 |
622 /** |
646 /** |
623 * Sets the data contents into the cache. |
647 * Sets the data contents into the cache. |
624 * |
648 * |
625 * The cache contents is grouped by the $group parameter followed by the |
649 * The cache contents are grouped by the $group parameter followed by the |
626 * $key. This allows for duplicate ids in unique groups. Therefore, naming of |
650 * $key. This allows for duplicate ids in unique groups. Therefore, naming of |
627 * the group should be used with care and should follow normal function |
651 * the group should be used with care and should follow normal function |
628 * naming guidelines outside of core WordPress usage. |
652 * naming guidelines outside of core WordPress usage. |
629 * |
653 * |
630 * The $expire parameter is not used, because the cache will automatically |
654 * The $expire parameter is not used, because the cache will automatically |
638 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
662 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
639 * @param int $expire Not Used. |
663 * @param int $expire Not Used. |
640 * @return true Always returns true. |
664 * @return true Always returns true. |
641 */ |
665 */ |
642 public function set( $key, $data, $group = 'default', $expire = 0 ) { |
666 public function set( $key, $data, $group = 'default', $expire = 0 ) { |
643 if ( empty( $group ) ) |
667 if ( empty( $group ) ) { |
644 $group = 'default'; |
668 $group = 'default'; |
645 |
669 } |
646 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
670 |
|
671 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
647 $key = $this->blog_prefix . $key; |
672 $key = $this->blog_prefix . $key; |
648 |
673 } |
649 if ( is_object( $data ) ) |
674 |
|
675 if ( is_object( $data ) ) { |
650 $data = clone $data; |
676 $data = clone $data; |
651 |
677 } |
652 $this->cache[$group][$key] = $data; |
678 |
|
679 $this->cache[ $group ][ $key ] = $data; |
653 return true; |
680 return true; |
654 } |
681 } |
655 |
682 |
656 /** |
683 /** |
657 * Echoes the stats of the caching. |
684 * Echoes the stats of the caching. |
660 * key and the data. |
687 * key and the data. |
661 * |
688 * |
662 * @since 2.0.0 |
689 * @since 2.0.0 |
663 */ |
690 */ |
664 public function stats() { |
691 public function stats() { |
665 echo "<p>"; |
692 echo '<p>'; |
666 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; |
693 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; |
667 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; |
694 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; |
668 echo "</p>"; |
695 echo '</p>'; |
669 echo '<ul>'; |
696 echo '<ul>'; |
670 foreach ($this->cache as $group => $cache) { |
697 foreach ( $this->cache as $group => $cache ) { |
671 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>'; |
698 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>'; |
672 } |
699 } |
673 echo '</ul>'; |
700 echo '</ul>'; |
674 } |
701 } |
675 |
702 |
681 * @since 3.5.0 |
708 * @since 3.5.0 |
682 * |
709 * |
683 * @param int $blog_id Blog ID. |
710 * @param int $blog_id Blog ID. |
684 */ |
711 */ |
685 public function switch_to_blog( $blog_id ) { |
712 public function switch_to_blog( $blog_id ) { |
686 $blog_id = (int) $blog_id; |
713 $blog_id = (int) $blog_id; |
687 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
714 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
688 } |
715 } |
689 |
716 |
690 /** |
717 /** |
691 * Serves as a utility function to determine whether a key exists in the cache. |
718 * Serves as a utility function to determine whether a key exists in the cache. |
704 * Sets up object properties; PHP 5 style constructor. |
731 * Sets up object properties; PHP 5 style constructor. |
705 * |
732 * |
706 * @since 2.0.8 |
733 * @since 2.0.8 |
707 */ |
734 */ |
708 public function __construct() { |
735 public function __construct() { |
709 $this->multisite = is_multisite(); |
736 $this->multisite = is_multisite(); |
710 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; |
737 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; |
711 |
|
712 |
738 |
713 /** |
739 /** |
714 * @todo This should be moved to the PHP4 style constructor, PHP5 |
740 * @todo This should be moved to the PHP4 style constructor, PHP5 |
715 * already calls __destruct() |
741 * already calls __destruct() |
716 */ |
742 */ |