<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[45910] trunk: Multisite: Improve performance by caching not found lookups for sites and networks.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/45910">45910</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/45910","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-08-29 12:41:45 +0000 (Thu, 29 Aug 2019)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/42251">#42251</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesschemaphp">trunk/src/wp-admin/includes/schema.php</a></li>
<li><a href="#trunksrcwpincludesclasswpnetworkphp">trunk/src/wp-includes/class-wp-network.php</a></li>
<li><a href="#trunksrcwpincludesclasswpsitephp">trunk/src/wp-includes/class-wp-site.php</a></li>
<li><a href="#trunksrcwpincludesmssitephp">trunk/src/wp-includes/ms-site.php</a></li>
<li><a href="#trunktestsphpunittestsmultisitenetworkphp">trunk/tests/phpunit/tests/multisite/network.php</a></li>
<li><a href="#trunktestsphpunittestsmultisitesitephp">trunk/tests/phpunit/tests/multisite/site.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesschemaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/schema.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/schema.php 2019-08-29 07:52:43 UTC (rev 45909)
+++ trunk/src/wp-admin/includes/schema.php 2019-08-29 12:41:45 UTC (rev 45910)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1159,6 +1159,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( function_exists( 'clean_network_cache' ) ) {
+ clean_network_cache( $network_id );
+ } else {
+ wp_cache_delete( $network_id, 'networks' );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( 'networks_have_paths', 'site-options' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_multisite() ) {
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpnetworkphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-network.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-network.php 2019-08-29 07:52:43 UTC (rev 45909)
+++ trunk/src/wp-includes/class-wp-network.php 2019-08-29 12:41:45 UTC (rev 45910)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -101,16 +101,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $_network = wp_cache_get( $network_id, 'networks' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $_network ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( false === $_network ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $_network ) || is_wp_error( $_network ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $_network = -1;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_add( $network_id, $_network, 'networks' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_numeric( $_network ) ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Network( $_network );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -231,8 +235,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return (int) $this->blog_id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $this->domain === DOMAIN_CURRENT_SITE && $this->path === PATH_CURRENT_SITE )
- || ( defined( 'SITE_ID_CURRENT_SITE' ) && $this->id == SITE_ID_CURRENT_SITE ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
+ || ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->blog_id = (string) BLOG_ID_CURRENT_SITE;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -457,7 +461,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $network->path === '/' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( '/' === $network->path ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $found = true;
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpsitephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-site.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-site.php 2019-08-29 07:52:43 UTC (rev 45909)
+++ trunk/src/wp-includes/class-wp-site.php 2019-08-29 12:41:45 UTC (rev 45910)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -162,16 +162,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $_site = wp_cache_get( $site_id, 'sites' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $_site ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( false === $_site ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $_site ) || is_wp_error( $_site ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $_site = -1;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_add( $site_id, $_site, 'sites' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_numeric( $_site ) ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Site( $_site );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesmssitephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-site.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-site.php 2019-08-29 07:52:43 UTC (rev 45909)
+++ trunk/src/wp-includes/ms-site.php 2019-08-29 12:41:45 UTC (rev 45910)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -69,6 +69,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ clean_blog_cache( $wpdb->insert_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $new_site = get_site( $wpdb->insert_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $new_site ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -75,8 +77,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- clean_blog_cache( $new_site );
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires once a site has been inserted into the database.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunktestsphpunittestsmultisitenetworkphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/multisite/network.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/network.php 2019-08-29 07:52:43 UTC (rev 45909)
+++ trunk/tests/phpunit/tests/multisite/network.php 2019-08-29 12:41:45 UTC (rev 45910)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -613,6 +613,49 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 42251
+ */
+ public function test_get_network_not_found_cache() {
+ global $wpdb;
+
+ $new_network_id = $this->_get_next_network_id();
+ $this->assertNull( get_network( $new_network_id ) );
+
+ $num_queries = $wpdb->num_queries;
+ $this->assertNull( get_network( $new_network_id ) );
+ $this->assertSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 42251
+ */
+ public function test_get_network_not_found_cache_clear() {
+ $new_network_id = $this->_get_next_network_id();
+ $this->assertNull( get_network( $new_network_id ) );
+
+ $new_network = $this->factory()->network->create_and_get();
+
+ // Double-check we got the ID of the new network correct.
+ $this->assertEquals( $new_network_id, $new_network->id );
+
+ // Verify that if we fetch the network now, it's no longer false.
+ $fetched_network = get_network( $new_network_id );
+ $this->assertInstanceOf( 'WP_Network', $fetched_network );
+ $this->assertEquals( $new_network_id, $fetched_network->id );
+ }
+
+ /**
+ * Gets the ID of the site with the highest ID
+ * @return int
+ */
+ protected function _get_next_network_id() {
+ global $wpdb;
+ //create an extra network, just to make sure we know the ID of the following one
+ static::factory()->network->create();
+ return (int) $wpdb->get_var( 'SELECT id FROM ' . $wpdb->site . ' ORDER BY id DESC LIMIT 1' ) + 1;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> endif;
</span></span></pre></div>
<a id="trunktestsphpunittestsmultisitesitephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/multisite/site.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/site.php 2019-08-29 07:52:43 UTC (rev 45909)
+++ trunk/tests/phpunit/tests/multisite/site.php 2019-08-29 12:41:45 UTC (rev 45910)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2384,6 +2384,51 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 42251
+ */
+ public function test_get_site_not_found_cache() {
+ global $wpdb;
+
+ $new_site_id = $this->_get_next_site_id();
+ $this->assertNull( get_site( $new_site_id ) );
+
+ $num_queries = $wpdb->num_queries;
+ $this->assertNull( get_site( $new_site_id ) );
+ $this->assertSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 42251
+ */
+ public function test_get_site_not_found_cache_clear() {
+ $new_site_id = $this->_get_next_site_id();
+ $this->assertNull( get_site( $new_site_id ) );
+
+ $new_site = $this->factory()->blog->create_and_get();
+
+ // Double-check we got the ID of the new site correct.
+ $this->assertEquals( $new_site_id, $new_site->blog_id );
+
+ // Verify that if we fetch the site now, it's no longer false.
+ $fetched_site = get_site( $new_site_id );
+ $this->assertInstanceOf( 'WP_Site', $fetched_site );
+ $this->assertEquals( $new_site_id, $fetched_site->blog_id );
+
+ }
+
+ /**
+ * Gets the ID of the next site that will get inserted
+ * @return int
+ */
+ protected function _get_next_site_id() {
+ global $wpdb;
+ //create an entry
+ static::factory()->blog->create();
+ //get the ID after it
+ return (int) $wpdb->get_var( 'SELECT blog_id FROM ' . $wpdb->blogs . ' ORDER BY blog_ID DESC LIMIT 1' ) + 1;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Capture the $meta value passed to the wpmu_new_blog action and compare it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function wpmu_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
</span></span></pre>
</div>
</div>
</body>
</html>