Make WordPress Core


Ignore:
Timestamp:
08/29/2019 12:41:45 PM (7 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.