Make WordPress Core

Ticket #21434: 21434.3.diff

File 21434.3.diff, 8.7 KB (added by ryan, 13 years ago)

Performance twaeks, wp_start_object_cache() changes

  • wp-includes/load.php

     
    381381 * @since 3.0.0
    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;
    387387        if ( ! function_exists( 'wp_cache_init' ) ) {
     
    403403        // If cache supports reset, reset instead of init if already initialized.
    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();
    410410
  • wp-includes/ms-blogs.php

     
    487487                        $current_user->for_blog( $blog_id );
    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
    504508        do_action('switch_blog', $blog_id, $prev_blog_id);
     
    551555                        $current_user->for_blog( $blog_id );
    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
    568576        do_action('switch_blog', $blog_id, $prev_blog_id);
  • wp-includes/cache.php

     
    180180}
    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 *
    185198 * @since 2.6.0
     
    189202function wp_cache_add_global_groups( $groups ) {
    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
    195208/**
     
    271284        var $global_groups = array();
    272285
    273286        /**
     287         * The blog prefix to prepend to keys in non-global groups.
     288         *
     289         * @var int
     290         * @access private
     291         * @since 3.5.0
     292         */
     293        var $blog_prefix;
     294
     295        /**
    274296         * Adds data to the cache if it doesn't already exist.
    275297         *
    276298         * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
     
    292314                if ( empty( $group ) )
    293315                        $group = 'default';
    294316
    295                 if ( $this->_exists($key, $group) )
     317                $id = $key;
     318                if ( $this->multisite && ! in_array( $group, $this->global_groups ) )
     319                        $id = $this->blog_prefix . $key;
     320
     321                if ( $this->_exists( $id, $group ) )
    296322                        return false;
    297323
    298324                return $this->set($key, $data, $group, $expire);
     
    325351        function decr( $key, $offset = 1, $group = 'default' ) {
    326352                if ( empty( $group ) )
    327353                        $group = 'default';
    328                
     354
     355                if ( $this->multisite && ! in_array( $group, $this->global_groups ) )
     356                        $key = $this->blog_prefix . $key;
     357
    329358                if ( ! $this->_exists( $key, $group ) )
    330359                        return false;
    331360
     
    361390                if ( empty( $group ) )
    362391                        $group = 'default';
    363392
     393                if ( $this->multisite && ! in_array( $group, $this->global_groups ) )
     394                        $key = $this->blog_prefix . $key;
     395
    364396                if ( ! $force && ! $this->_exists( $key, $group ) )
    365397                        return false;
    366398
     
    402434                if ( empty( $group ) )
    403435                        $group = 'default';
    404436
     437                if ( $this->multisite && ! in_array( $group, $this->global_groups ) )
     438                        $key = $this->blog_prefix . $key;
     439
    405440                if ( $this->_exists( $key, $group ) ) {
    406441                        $found = true;
    407442                        $this->cache_hits += 1;
     
    430465                if ( empty( $group ) )
    431466                        $group = 'default';
    432467
     468                if ( $this->multisite && ! in_array( $group, $this->global_groups ) )
     469                        $key = $this->blog_prefix . $key;
     470
    433471                if ( ! $this->_exists( $key, $group ) )
    434472                        return false;
    435473
     
    458496         * @param int $expire When to expire the cache contents
    459497         * @return bool False if not exists, true if contents were replaced
    460498         */
    461         function replace($key, $data, $group = 'default', $expire = '') {
     499        function replace( $key, $data, $group = 'default', $expire = '' ) {
    462500                if ( empty( $group ) )
    463501                        $group = 'default';
    464502
    465                 if ( ! $this->_exists( $key, $group ) )
     503                $id = $key;
     504                if ( $this->multisite && ! in_array( $group, $this->global_groups ) )
     505                        $id = $this->blog_prefix . $key;
     506
     507                if ( ! $this->_exists( $id, $group ) )
    466508                        return false;
    467509
    468                 return $this->set($key, $data, $group, $expire);
     510                return $this->set( $key, $data, $group, $expire );
    469511        }
    470512
    471513        /**
     
    475517         */
    476518        function reset() {
    477519                // 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]);
     520                foreach ( array_keys( $this->cache ) as $group ) {
     521                        if ( !in_array( $group, $this->global_groups ) )
     522                                unset( $this->cache[$group] );
    481523                }
    482524        }
    483525
     
    505547                if ( empty( $group ) )
    506548                        $group = 'default';
    507549
    508                 if ( is_object($data) )
     550                if ( $this->multisite && ! in_array( $group, $this->global_groups ) )
     551                        $key = $this->blog_prefix . $key;
     552
     553                if ( is_object( $data ) )
    509554                        $data = clone $data;
    510555
    511556                $this->cache[$group][$key] = $data;
     
    533578        }
    534579
    535580        /**
     581         * Switch the interal blog id.
     582         *
     583         * This changes the blog id used to create keys in blog specific groups.
     584         *
     585         * @param int $blog_id Blog ID
     586         */
     587        function switch_to_blog( $blog_id ) {
     588                $blog_id = (int) $blog_id;
     589                $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
     590        }
     591
     592        /**
    536593         * Utility function to determine whether a key exists in the cache.
    537594         *
    538595         * @since 3.4.0
     
    550607         * @return null|WP_Object_Cache If cache is disabled, returns null.
    551608         */
    552609        function __construct() {
     610                global $blog_id;
     611
     612                $this->multisite = is_multisite();
     613                $this->blog_prefix =  $this->multisite ? $blog_id . ':' : '';
     614               
     615
    553616                /**
    554617                 * @todo This should be moved to the PHP4 style constructor, PHP5
    555618                 * already calls __destruct()
    556619                 */
    557                 register_shutdown_function(array(&$this, "__destruct"));
     620                register_shutdown_function( array( &$this, "__destruct" ) );
    558621        }
    559622
    560623        /**