Make WordPress Core


Ignore:
Timestamp:
08/29/2019 12:41:45 PM (5 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/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
Note: See TracChangeset for help on using the changeset viewer.