Changeset 42343 for trunk/src/wp-includes/cache.php
- Timestamp:
- 11/30/2017 11:09:33 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/cache.php
r41688 r42343 82 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 … … 116 116 * Disambiguates a return of false, a storable value. Default null. 117 117 * @return bool|mixed False on failure to retrieve contents or the cache 118 * 118 * contents on success 119 119 */ 120 120 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { … … 391 391 * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data. 392 392 * @uses WP_Object_Cache::set() Sets the data after the checking the cache 393 * 393 * contents existence. 394 394 * 395 395 * @param int|string $key What to call the contents in the cache. … … 400 400 */ 401 401 public function add( $key, $data, $group = 'default', $expire = 0 ) { 402 if ( wp_suspend_cache_addition() ) 402 if ( wp_suspend_cache_addition() ) { 403 403 return false; 404 405 if ( empty( $group ) ) 404 } 405 406 if ( empty( $group ) ) { 406 407 $group = 'default'; 408 } 407 409 408 410 $id = $key; 409 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 411 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 410 412 $id = $this->blog_prefix . $key; 411 412 if ( $this->_exists( $id, $group ) ) 413 } 414 415 if ( $this->_exists( $id, $group ) ) { 413 416 return false; 417 } 414 418 415 419 return $this->set( $key, $data, $group, (int) $expire ); … … 426 430 $groups = (array) $groups; 427 431 428 $groups = array_fill_keys( $groups, true );432 $groups = array_fill_keys( $groups, true ); 429 433 $this->global_groups = array_merge( $this->global_groups, $groups ); 430 434 } … … 441 445 */ 442 446 public function decr( $key, $offset = 1, $group = 'default' ) { 443 if ( empty( $group ) ) 447 if ( empty( $group ) ) { 444 448 $group = 'default'; 445 446 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 449 } 450 451 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 447 452 $key = $this->blog_prefix . $key; 448 449 if ( ! $this->_exists( $key, $group ) ) 453 } 454 455 if ( ! $this->_exists( $key, $group ) ) { 450 456 return false; 451 452 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) 457 } 458 459 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { 453 460 $this->cache[ $group ][ $key ] = 0; 461 } 454 462 455 463 $offset = (int) $offset; … … 457 465 $this->cache[ $group ][ $key ] -= $offset; 458 466 459 if ( $this->cache[ $group ][ $key ] < 0 ) 467 if ( $this->cache[ $group ][ $key ] < 0 ) { 460 468 $this->cache[ $group ][ $key ] = 0; 469 } 461 470 462 471 return $this->cache[ $group ][ $key ]; … … 476 485 */ 477 486 public function delete( $key, $group = 'default', $deprecated = false ) { 478 if ( empty( $group ) ) 487 if ( empty( $group ) ) { 479 488 $group = 'default'; 480 481 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 489 } 490 491 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 482 492 $key = $this->blog_prefix . $key; 483 484 if ( ! $this->_exists( $key, $group ) ) 493 } 494 495 if ( ! $this->_exists( $key, $group ) ) { 485 496 return false; 486 487 unset( $this->cache[$group][$key] ); 497 } 498 499 unset( $this->cache[ $group ][ $key ] ); 488 500 return true; 489 501 } … … 522 534 */ 523 535 public function get( $key, $group = 'default', $force = false, &$found = null ) { 524 if ( empty( $group ) ) 536 if ( empty( $group ) ) { 525 537 $group = 'default'; 526 527 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 538 } 539 540 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 528 541 $key = $this->blog_prefix . $key; 542 } 529 543 530 544 if ( $this->_exists( $key, $group ) ) { 531 $found = true;545 $found = true; 532 546 $this->cache_hits += 1; 533 if ( is_object($this->cache[$group][$key]) ) 534 return clone $this->cache[$group][$key]; 535 else 536 return $this->cache[$group][$key]; 537 } 538 539 $found = false; 547 if ( is_object( $this->cache[ $group ][ $key ] ) ) { 548 return clone $this->cache[ $group ][ $key ]; 549 } else { 550 return $this->cache[ $group ][ $key ]; 551 } 552 } 553 554 $found = false; 540 555 $this->cache_misses += 1; 541 556 return false; … … 553 568 */ 554 569 public function incr( $key, $offset = 1, $group = 'default' ) { 555 if ( empty( $group ) ) 570 if ( empty( $group ) ) { 556 571 $group = 'default'; 557 558 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 572 } 573 574 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 559 575 $key = $this->blog_prefix . $key; 560 561 if ( ! $this->_exists( $key, $group ) ) 576 } 577 578 if ( ! $this->_exists( $key, $group ) ) { 562 579 return false; 563 564 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) 580 } 581 582 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { 565 583 $this->cache[ $group ][ $key ] = 0; 584 } 566 585 567 586 $offset = (int) $offset; … … 569 588 $this->cache[ $group ][ $key ] += $offset; 570 589 571 if ( $this->cache[ $group ][ $key ] < 0 ) 590 if ( $this->cache[ $group ][ $key ] < 0 ) { 572 591 $this->cache[ $group ][ $key ] = 0; 592 } 573 593 574 594 return $this->cache[ $group ][ $key ]; … … 589 609 */ 590 610 public function replace( $key, $data, $group = 'default', $expire = 0 ) { 591 if ( empty( $group ) ) 611 if ( empty( $group ) ) { 592 612 $group = 'default'; 613 } 593 614 594 615 $id = $key; 595 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 616 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 596 617 $id = $this->blog_prefix . $key; 597 598 if ( ! $this->_exists( $id, $group ) ) 618 } 619 620 if ( ! $this->_exists( $id, $group ) ) { 599 621 return false; 622 } 600 623 601 624 return $this->set( $key, $data, $group, (int) $expire ); … … 615 638 // Clear out non-global caches since the blog ID has changed. 616 639 foreach ( array_keys( $this->cache ) as $group ) { 617 if ( ! isset( $this->global_groups[ $group ] ) ) 640 if ( ! isset( $this->global_groups[ $group ] ) ) { 618 641 unset( $this->cache[ $group ] ); 642 } 619 643 } 620 644 } … … 641 665 */ 642 666 public function set( $key, $data, $group = 'default', $expire = 0 ) { 643 if ( empty( $group ) ) 667 if ( empty( $group ) ) { 644 668 $group = 'default'; 645 646 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 669 } 670 671 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 647 672 $key = $this->blog_prefix . $key; 648 649 if ( is_object( $data ) ) 673 } 674 675 if ( is_object( $data ) ) { 650 676 $data = clone $data; 651 652 $this->cache[$group][$key] = $data; 677 } 678 679 $this->cache[ $group ][ $key ] = $data; 653 680 return true; 654 681 } … … 663 690 */ 664 691 public function stats() { 665 echo "<p>";692 echo '<p>'; 666 693 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; 667 694 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; 668 echo "</p>";695 echo '</p>'; 669 696 echo '<ul>'; 670 foreach ( $this->cache as $group => $cache) {697 foreach ( $this->cache as $group => $cache ) { 671 698 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>'; 672 699 } … … 684 711 */ 685 712 public function switch_to_blog( $blog_id ) { 686 $blog_id = (int) $blog_id;713 $blog_id = (int) $blog_id; 687 714 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; 688 715 } … … 707 734 */ 708 735 public function __construct() { 709 $this->multisite = is_multisite(); 710 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; 711 736 $this->multisite = is_multisite(); 737 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; 712 738 713 739 /**
Note: See TracChangeset
for help on using the changeset viewer.