Make WordPress Core

Changeset 21403


Ignore:
Timestamp:
08/02/2012 06:31:14 PM (13 years ago)
Author:
ryan
Message:

Introduce wp_cache_switch_to_blog() and WP_Object_Cache::switch_to_blog() as a lighter/faster way to switch the cache to a new blog id.

Add the blog id to the cache keys for multisite installs.

Use wp_cache_switch_to_blog() instead of wp_cache_init() in switch_to_blog().

Use wp_cache_switch_to_blog() instead of wp_cache_reset() in wp_start_object_cache().

Deprecate wp_cache_reset().

This avoids the many queries needed to re-prime the cache after switch_to_blog() on multisite installs using the default cache backend.

fixes #21434

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/cache.php

    r21294 r21403  
    181181
    182182/**
     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 */
     189function 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/**
    183196 * Adds a group or set of groups to the list of global groups.
    184197 *
     
    190203    global $wp_object_cache;
    191204
    192     return $wp_object_cache->add_global_groups($groups);
     205    return $wp_object_cache->add_global_groups( $groups );
    193206}
    194207
     
    210223 *
    211224 * @since 2.6.0
     225 * @deprecated 3.5.0
    212226 */
    213227function wp_cache_reset() {
     228    _deprecated_function( __FUNCTION__, '3.5', 'wp_cache_switch_to_blog()' );
     229
    214230    global $wp_object_cache;
    215231
     
    270286     */
    271287    var $global_groups = array();
     288
     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;
    272297
    273298    /**
     
    293318            $group = 'default';
    294319
    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 ) )
    296325            return false;
    297326
     
    309338        $groups = (array) $groups;
    310339
    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 );
    313342    }
    314343
     
    326355        if ( empty( $group ) )
    327356            $group = 'default';
    328        
     357
     358        if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
     359            $key = $this->blog_prefix . $key;
     360
    329361        if ( ! $this->_exists( $key, $group ) )
    330362            return false;
     
    362394            $group = 'default';
    363395
     396        if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
     397            $key = $this->blog_prefix . $key;
     398
    364399        if ( ! $force && ! $this->_exists( $key, $group ) )
    365400            return false;
     
    402437        if ( empty( $group ) )
    403438            $group = 'default';
     439
     440        if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
     441            $key = $this->blog_prefix . $key;
    404442
    405443        if ( $this->_exists( $key, $group ) ) {
     
    431469            $group = 'default';
    432470
     471        if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
     472            $key = $this->blog_prefix . $key;
     473
    433474        if ( ! $this->_exists( $key, $group ) )
    434475            return false;
     
    459500     * @return bool False if not exists, true if contents were replaced
    460501     */
    461     function replace($key, $data, $group = 'default', $expire = '') {
     502    function replace( $key, $data, $group = 'default', $expire = '' ) {
    462503        if ( empty( $group ) )
    463504            $group = 'default';
    464505
    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 ) )
    466511            return false;
    467512
    468         return $this->set($key, $data, $group, $expire);
     513        return $this->set( $key, $data, $group, $expire );
    469514    }
    470515
     
    473518     *
    474519     * @since 3.0.0
     520     * @deprecated 3.5.0
    475521     */
    476522    function reset() {
     523        _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' );
     524
    477525        // 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 ] );
    481529        }
    482530    }
     
    506554            $group = 'default';
    507555
    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 ) )
    509560            $data = clone $data;
    510561
     
    534585
    535586    /**
     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    /**
    536599     * Utility function to determine whether a key exists in the cache.
    537600     *
     
    551614     */
    552615    function __construct() {
     616        global $blog_id;
     617
     618        $this->multisite = is_multisite();
     619        $this->blog_prefix =  $this->multisite ? $blog_id . ':' : '';
     620       
     621
    553622        /**
    554623         * @todo This should be moved to the PHP4 style constructor, PHP5
    555624         * already calls __destruct()
    556625         */
    557         register_shutdown_function(array(&$this, "__destruct"));
     626        register_shutdown_function( array( &$this, '__destruct' ) );
    558627    }
    559628
  • trunk/wp-includes/load.php

    r21363 r21403  
    382382 */
    383383function wp_start_object_cache() {
    384     global $_wp_using_ext_object_cache;
     384    global $_wp_using_ext_object_cache, $blog_id;
    385385
    386386    $first_init = false;
     
    404404    // Reset signals to the cache that global IDs have changed and it may need to update keys
    405405    // 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 );
    408408    else
    409409        wp_cache_init();
  • trunk/wp-includes/ms-blogs.php

    r21378 r21403  
    488488    }
    489489
    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;
    499495        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        }
    502506    }
    503507
     
    552556    }
    553557
    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;
    563563        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        }
    566574    }
    567575
Note: See TracChangeset for help on using the changeset viewer.