Ticket #21434: 21434.5.diff
File 21434.5.diff, 9.6 KB (added by , 12 years ago) |
---|
-
wp-includes/load.php
381 381 * @since 3.0.0 382 382 */ 383 383 function wp_start_object_cache() { 384 global $_wp_using_ext_object_cache ;384 global $_wp_using_ext_object_cache, $blog_id; 385 385 386 386 $first_init = false; 387 387 if ( ! function_exists( 'wp_cache_init' ) ) { … … 403 403 // If cache supports reset, reset instead of init if already initialized. 404 404 // Reset signals to the cache that global IDs have changed and it may need to update keys 405 405 // and cleanup caches. 406 if ( ! $first_init && function_exists('wp_cache_reset') )407 wp_cache_ reset();406 if ( ! $first_init && function_exists( 'wp_cache_switch_to_blog' ) ) 407 wp_cache_switch_to_blog( $blog_id ); 408 408 else 409 409 wp_cache_init(); 410 410 -
wp-includes/ms-blogs.php
487 487 $current_user->for_blog( $blog_id ); 488 488 } 489 489 490 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) 491 $global_groups = $wp_object_cache->global_groups; 492 else 493 $global_groups = false; 494 495 wp_cache_init(); 496 if ( function_exists('wp_cache_add_global_groups') ) { 497 if ( is_array( $global_groups ) ) 498 wp_cache_add_global_groups( $global_groups ); 490 if ( function_exists( 'wp_cache_switch_to_blog' ) ) { 491 wp_cache_switch_to_blog( $blog_id ); 492 } else { 493 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) 494 $global_groups = $wp_object_cache->global_groups; 499 495 else 500 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) ); 501 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' )); 496 $global_groups = false; 497 498 wp_cache_init(); 499 if ( function_exists('wp_cache_add_global_groups') ) { 500 if ( is_array( $global_groups ) ) 501 wp_cache_add_global_groups( $global_groups ); 502 else 503 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) ); 504 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' )); 505 } 502 506 } 503 507 504 508 do_action('switch_blog', $blog_id, $prev_blog_id); … … 551 555 $current_user->for_blog( $blog_id ); 552 556 } 553 557 554 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) 555 $global_groups = $wp_object_cache->global_groups; 556 else 557 $global_groups = false; 558 559 wp_cache_init(); 560 if ( function_exists('wp_cache_add_global_groups') ) { 561 if ( is_array( $global_groups ) ) 562 wp_cache_add_global_groups( $global_groups ); 558 if ( function_exists( 'wp_cache_switch_to_blog' ) ) { 559 wp_cache_switch_to_blog( $blog_id ); 560 } else { 561 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) 562 $global_groups = $wp_object_cache->global_groups; 563 563 else 564 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) ); 565 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' )); 564 $global_groups = false; 565 566 wp_cache_init(); 567 if ( function_exists('wp_cache_add_global_groups') ) { 568 if ( is_array( $global_groups ) ) 569 wp_cache_add_global_groups( $global_groups ); 570 else 571 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) ); 572 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' )); 573 } 566 574 } 567 575 568 576 do_action('switch_blog', $blog_id, $prev_blog_id); -
wp-includes/cache.php
180 180 } 181 181 182 182 /** 183 * Switch the interal blog id. 184 * 185 * This changes the blog id used to create keys in blog specific groups. 186 * 187 * @param int $blog_id Blog ID 188 */ 189 function wp_cache_switch_to_blog( $blog_id ) { 190 global $wp_object_cache; 191 192 return $wp_object_cache->switch_to_blog( $blog_id ); 193 } 194 195 /** 183 196 * Adds a group or set of groups to the list of global groups. 184 197 * 185 198 * @since 2.6.0 … … 189 202 function wp_cache_add_global_groups( $groups ) { 190 203 global $wp_object_cache; 191 204 192 return $wp_object_cache->add_global_groups( $groups);205 return $wp_object_cache->add_global_groups( $groups ); 193 206 } 194 207 195 208 /** … … 209 222 * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init. 210 223 * 211 224 * @since 2.6.0 225 * @deprecated 3.5.0 212 226 */ 213 227 function wp_cache_reset() { 228 _deprecated_function( __FUNCTION__, '3.5', 'wp_cache_switch_to_blog()' ); 229 214 230 global $wp_object_cache; 215 231 216 232 return $wp_object_cache->reset(); … … 271 287 var $global_groups = array(); 272 288 273 289 /** 290 * The blog prefix to prepend to keys in non-global groups. 291 * 292 * @var int 293 * @access private 294 * @since 3.5.0 295 */ 296 var $blog_prefix; 297 298 /** 274 299 * Adds data to the cache if it doesn't already exist. 275 300 * 276 301 * @uses WP_Object_Cache::_exists Checks to see if the cache already has data. … … 292 317 if ( empty( $group ) ) 293 318 $group = 'default'; 294 319 295 if ( $this->_exists($key, $group) ) 320 $id = $key; 321 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 322 $id = $this->blog_prefix . $key; 323 324 if ( $this->_exists( $id, $group ) ) 296 325 return false; 297 326 298 327 return $this->set($key, $data, $group, $expire); … … 308 337 function add_global_groups( $groups ) { 309 338 $groups = (array) $groups; 310 339 311 $ this->global_groups = array_merge($this->global_groups, $groups);312 $this->global_groups = array_ unique($this->global_groups);340 $groups = array_fill_keys( $groups, true ); 341 $this->global_groups = array_merge( $this->global_groups, $groups ); 313 342 } 314 343 315 344 /** … … 325 354 function decr( $key, $offset = 1, $group = 'default' ) { 326 355 if ( empty( $group ) ) 327 356 $group = 'default'; 328 357 358 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 359 $key = $this->blog_prefix . $key; 360 329 361 if ( ! $this->_exists( $key, $group ) ) 330 362 return false; 331 363 … … 361 393 if ( empty( $group ) ) 362 394 $group = 'default'; 363 395 396 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 397 $key = $this->blog_prefix . $key; 398 364 399 if ( ! $force && ! $this->_exists( $key, $group ) ) 365 400 return false; 366 401 … … 402 437 if ( empty( $group ) ) 403 438 $group = 'default'; 404 439 440 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 441 $key = $this->blog_prefix . $key; 442 405 443 if ( $this->_exists( $key, $group ) ) { 406 444 $found = true; 407 445 $this->cache_hits += 1; … … 430 468 if ( empty( $group ) ) 431 469 $group = 'default'; 432 470 471 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 472 $key = $this->blog_prefix . $key; 473 433 474 if ( ! $this->_exists( $key, $group ) ) 434 475 return false; 435 476 … … 458 499 * @param int $expire When to expire the cache contents 459 500 * @return bool False if not exists, true if contents were replaced 460 501 */ 461 function replace( $key, $data, $group = 'default', $expire = '') {502 function replace( $key, $data, $group = 'default', $expire = '' ) { 462 503 if ( empty( $group ) ) 463 504 $group = 'default'; 464 505 465 if ( ! $this->_exists( $key, $group ) ) 506 $id = $key; 507 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 508 $id = $this->blog_prefix . $key; 509 510 if ( ! $this->_exists( $id, $group ) ) 466 511 return false; 467 512 468 return $this->set( $key, $data, $group, $expire);513 return $this->set( $key, $data, $group, $expire ); 469 514 } 470 515 471 516 /** 472 517 * Reset keys 473 518 * 474 519 * @since 3.0.0 520 * @deprecated 3.5.0 475 521 */ 476 522 function reset() { 523 _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' ); 524 477 525 // Clear out non-global caches since the blog ID has changed. 478 foreach ( array_keys( $this->cache) as $group ) {479 if ( ! in_array($group, $this->global_groups) )480 unset( $this->cache[$group]);526 foreach ( array_keys( $this->cache ) as $group ) { 527 if ( ! isset( $this->global_groups[ $group ] ) ) 528 unset( $this->cache[ $group ] ); 481 529 } 482 530 } 483 531 … … 505 553 if ( empty( $group ) ) 506 554 $group = 'default'; 507 555 508 if ( is_object($data) ) 556 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 557 $key = $this->blog_prefix . $key; 558 559 if ( is_object( $data ) ) 509 560 $data = clone $data; 510 561 511 562 $this->cache[$group][$key] = $data; … … 533 584 } 534 585 535 586 /** 587 * Switch the interal blog id. 588 * 589 * This changes the blog id used to create keys in blog specific groups. 590 * 591 * @param int $blog_id Blog ID 592 */ 593 function switch_to_blog( $blog_id ) { 594 $blog_id = (int) $blog_id; 595 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; 596 } 597 598 /** 536 599 * Utility function to determine whether a key exists in the cache. 537 600 * 538 601 * @since 3.4.0 … … 550 613 * @return null|WP_Object_Cache If cache is disabled, returns null. 551 614 */ 552 615 function __construct() { 616 global $blog_id; 617 618 $this->multisite = is_multisite(); 619 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; 620 621 553 622 /** 554 623 * @todo This should be moved to the PHP4 style constructor, PHP5 555 624 * already calls __destruct() 556 625 */ 557 register_shutdown_function( array(&$this, "__destruct"));626 register_shutdown_function( array( &$this, "__destruct" ) ); 558 627 } 559 628 560 629 /**