WordPress.org

Make WordPress Core

Changeset 45910


Ignore:
Timestamp:
08/29/2019 12:41:45 PM (2 years ago)
Author:
flixos90
Message:

Multisite: Improve performance by caching not found lookups for sites and networks.

With this change, the result of a site or network lookup by ID will be cached even if the ID does not exist. When a new site or network is created, the cache for the respective new ID is cleared.

Props mnelson4, nielsdeblaauw.
Fixes #42251.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/schema.php

    r45805 r45910  
    11601160    }
    11611161
     1162    if ( function_exists( 'clean_network_cache' ) ) {
     1163        clean_network_cache( $network_id );
     1164    } else {
     1165        wp_cache_delete( $network_id, 'networks' );
     1166    }
     1167
    11621168    wp_cache_delete( 'networks_have_paths', 'site-options' );
    11631169
  • trunk/src/wp-includes/class-wp-network.php

    r42876 r45910  
    102102        $_network = wp_cache_get( $network_id, 'networks' );
    103103
    104         if ( ! $_network ) {
     104        if ( false === $_network ) {
    105105            $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
    106106
    107107            if ( empty( $_network ) || is_wp_error( $_network ) ) {
    108                 return false;
     108                $_network = -1;
    109109            }
    110110
    111111            wp_cache_add( $network_id, $_network, 'networks' );
     112        }
     113
     114        if ( is_numeric( $_network ) ) {
     115            return false;
    112116        }
    113117
     
    232236        }
    233237
    234         if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $this->domain === DOMAIN_CURRENT_SITE && $this->path === PATH_CURRENT_SITE )
    235             || ( defined( 'SITE_ID_CURRENT_SITE' ) && $this->id == SITE_ID_CURRENT_SITE ) ) {
     238        if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
     239            || ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
    236240            if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
    237241                $this->blog_id = (string) BLOG_ID_CURRENT_SITE;
     
    458462                }
    459463            }
    460             if ( $network->path === '/' ) {
     464            if ( '/' === $network->path ) {
    461465                $found = true;
    462466                break;
  • trunk/src/wp-includes/class-wp-site.php

    r45232 r45910  
    163163        $_site = wp_cache_get( $site_id, 'sites' );
    164164
    165         if ( ! $_site ) {
     165        if ( false === $_site ) {
    166166            $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
    167167
    168168            if ( empty( $_site ) || is_wp_error( $_site ) ) {
    169                 return false;
     169                $_site = -1;
    170170            }
    171171
    172172            wp_cache_add( $site_id, $_site, 'sites' );
     173        }
     174
     175        if ( is_numeric( $_site ) ) {
     176            return false;
    173177        }
    174178
  • trunk/src/wp-includes/ms-site.php

    r45236 r45910  
    7070    }
    7171
     72    clean_blog_cache( $wpdb->insert_id );
     73
    7274    $new_site = get_site( $wpdb->insert_id );
    7375
     
    7577        return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
    7678    }
    77 
    78     clean_blog_cache( $new_site );
    7979
    8080    /**
  • trunk/tests/phpunit/tests/multisite/network.php

    r42343 r45910  
    614614            $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
    615615        }
     616
     617        /**
     618         * @ticket 42251
     619         */
     620        public function test_get_network_not_found_cache() {
     621            global $wpdb;
     622
     623            $new_network_id = $this->_get_next_network_id();
     624            $this->assertNull( get_network( $new_network_id ) );
     625
     626            $num_queries = $wpdb->num_queries;
     627            $this->assertNull( get_network( $new_network_id ) );
     628            $this->assertSame( $num_queries, $wpdb->num_queries );
     629        }
     630
     631        /**
     632         * @ticket 42251
     633         */
     634        public function test_get_network_not_found_cache_clear() {
     635            $new_network_id = $this->_get_next_network_id();
     636            $this->assertNull( get_network( $new_network_id ) );
     637
     638            $new_network = $this->factory()->network->create_and_get();
     639
     640            // Double-check we got the ID of the new network correct.
     641            $this->assertEquals( $new_network_id, $new_network->id );
     642
     643            // Verify that if we fetch the network now, it's no longer false.
     644            $fetched_network = get_network( $new_network_id );
     645            $this->assertInstanceOf( 'WP_Network', $fetched_network );
     646            $this->assertEquals( $new_network_id, $fetched_network->id );
     647        }
     648
     649        /**
     650         * Gets the ID of the site with the highest ID
     651         * @return int
     652         */
     653        protected function _get_next_network_id() {
     654            global $wpdb;
     655            //create an extra network, just to make sure we know the ID of the following one
     656            static::factory()->network->create();
     657            return (int) $wpdb->get_var( 'SELECT id FROM ' . $wpdb->site . ' ORDER BY id DESC LIMIT 1' ) + 1;
     658        }
    616659    }
    617660
  • trunk/tests/phpunit/tests/multisite/site.php

    r45607 r45910  
    23852385
    23862386        /**
     2387         * @ticket 42251
     2388         */
     2389        public function test_get_site_not_found_cache() {
     2390            global $wpdb;
     2391
     2392            $new_site_id = $this->_get_next_site_id();
     2393            $this->assertNull( get_site( $new_site_id ) );
     2394
     2395            $num_queries = $wpdb->num_queries;
     2396            $this->assertNull( get_site( $new_site_id ) );
     2397            $this->assertSame( $num_queries, $wpdb->num_queries );
     2398        }
     2399
     2400        /**
     2401         * @ticket 42251
     2402         */
     2403        public function test_get_site_not_found_cache_clear() {
     2404            $new_site_id = $this->_get_next_site_id();
     2405            $this->assertNull( get_site( $new_site_id ) );
     2406
     2407            $new_site = $this->factory()->blog->create_and_get();
     2408
     2409            // Double-check we got the ID of the new site correct.
     2410            $this->assertEquals( $new_site_id, $new_site->blog_id );
     2411
     2412            // Verify that if we fetch the site now, it's no longer false.
     2413            $fetched_site = get_site( $new_site_id );
     2414            $this->assertInstanceOf( 'WP_Site', $fetched_site );
     2415            $this->assertEquals( $new_site_id, $fetched_site->blog_id );
     2416
     2417        }
     2418
     2419        /**
     2420         * Gets the ID of the next site that will get inserted
     2421         * @return int
     2422         */
     2423        protected function _get_next_site_id() {
     2424            global $wpdb;
     2425            //create an entry
     2426            static::factory()->blog->create();
     2427            //get the ID after it
     2428            return (int) $wpdb->get_var( 'SELECT blog_id FROM ' . $wpdb->blogs . ' ORDER BY blog_ID DESC LIMIT 1' ) + 1;
     2429        }
     2430
     2431        /**
    23872432         * Capture the $meta value passed to the wpmu_new_blog action and compare it.
    23882433         */
Note: See TracChangeset for help on using the changeset viewer.