<!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>[21403] trunk/wp-includes: Introduce wp_cache_switch_to_blog() and WP_Object_Cache::switch_to_blog() as a lighter/faster way to switch the cache to a new blog id.</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 { 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, #logmsg > ol { margin-left: 0; 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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/21403">21403</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2012-08-02 18:31:14 +0000 (Thu, 02 Aug 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduce wp_cache_switch_to_blog() and WP_Object_Cache::switch_to_blog() as a lighter/faster way to switch the cache to a new blog id.

Add the blog id to the cache keys for multisite installs.

Use wp_cache_switch_to_blog() instead of wp_cache_init() in switch_to_blog().

Use wp_cache_switch_to_blog() instead of wp_cache_reset() in wp_start_object_cache().

Deprecate wp_cache_reset().

This avoids the many queries needed to re-prime the cache after switch_to_blog() on multisite installs using the default cache backend.

fixes <a href="http://core.trac.wordpress.org/ticket/21434">#21434</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludescachephp">trunk/wp-includes/cache.php</a></li>
<li><a href="#trunkwpincludesloadphp">trunk/wp-includes/load.php</a></li>
<li><a href="#trunkwpincludesmsblogsphp">trunk/wp-includes/ms-blogs.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludescachephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/cache.php (21402 => 21403)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/cache.php        2012-08-02 18:28:10 UTC (rev 21402)
+++ trunk/wp-includes/cache.php        2012-08-02 18:31:14 UTC (rev 21403)
</span><span class="lines">@@ -180,6 +180,19 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Switch the interal blog id.
+ *
+ * This changes the blog id used to create keys in blog specific groups.
+ *
+ * @param int $blog_id Blog ID
+ */
+function wp_cache_switch_to_blog( $blog_id ) {
+        global $wp_object_cache;
+
+        return $wp_object_cache-&gt;switch_to_blog( $blog_id );
+}
+
+/**
</ins><span class="cx">  * Adds a group or set of groups to the list of global groups.
</span><span class="cx">  *
</span><span class="cx">  * @since 2.6.0
</span><span class="lines">@@ -189,7 +202,7 @@
</span><span class="cx"> function wp_cache_add_global_groups( $groups ) {
</span><span class="cx">         global $wp_object_cache;
</span><span class="cx"> 
</span><del>-        return $wp_object_cache-&gt;add_global_groups($groups);
</del><ins>+        return $wp_object_cache-&gt;add_global_groups( $groups );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -209,8 +222,11 @@
</span><span class="cx">  * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
</span><span class="cx">  *
</span><span class="cx">  * @since 2.6.0
</span><ins>+ * @deprecated 3.5.0
</ins><span class="cx">  */
</span><span class="cx"> function wp_cache_reset() {
</span><ins>+        _deprecated_function( __FUNCTION__, '3.5', 'wp_cache_switch_to_blog()' );
+
</ins><span class="cx">         global $wp_object_cache;
</span><span class="cx"> 
</span><span class="cx">         return $wp_object_cache-&gt;reset();
</span><span class="lines">@@ -271,6 +287,15 @@
</span><span class="cx">         var $global_groups = array();
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * The blog prefix to prepend to keys in non-global groups.
+         *
+         * @var int
+         * @access private
+         * @since 3.5.0
+         */
+        var $blog_prefix;
+
+        /**
</ins><span class="cx">          * Adds data to the cache if it doesn't already exist.
</span><span class="cx">          *
</span><span class="cx">          * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
</span><span class="lines">@@ -292,7 +317,11 @@
</span><span class="cx">                 if ( empty( $group ) )
</span><span class="cx">                         $group = 'default';
</span><span class="cx"> 
</span><del>-                if ( $this-&gt;_exists($key, $group) )
</del><ins>+                $id = $key;
+                if ( $this-&gt;multisite &amp;&amp; ! isset( $this-&gt;global_groups[ $group ] ) )
+                        $id = $this-&gt;blog_prefix . $key;
+
+                if ( $this-&gt;_exists( $id, $group ) )
</ins><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="cx">                 return $this-&gt;set($key, $data, $group, $expire);
</span><span class="lines">@@ -308,8 +337,8 @@
</span><span class="cx">         function add_global_groups( $groups ) {
</span><span class="cx">                 $groups = (array) $groups;
</span><span class="cx"> 
</span><del>-                $this-&gt;global_groups = array_merge($this-&gt;global_groups, $groups);
-                $this-&gt;global_groups = array_unique($this-&gt;global_groups);
</del><ins>+                $groups = array_fill_keys( $groups, true );
+                $this-&gt;global_groups = array_merge( $this-&gt;global_groups, $groups );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -325,7 +354,10 @@
</span><span class="cx">         function decr( $key, $offset = 1, $group = 'default' ) {
</span><span class="cx">                 if ( empty( $group ) )
</span><span class="cx">                         $group = 'default';
</span><del>-                
</del><ins>+
+                if ( $this-&gt;multisite &amp;&amp; ! isset( $this-&gt;global_groups[ $group ] ) )
+                        $key = $this-&gt;blog_prefix . $key;
+
</ins><span class="cx">                 if ( ! $this-&gt;_exists( $key, $group ) )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -361,6 +393,9 @@
</span><span class="cx">                 if ( empty( $group ) )
</span><span class="cx">                         $group = 'default';
</span><span class="cx"> 
</span><ins>+                if ( $this-&gt;multisite &amp;&amp; ! isset( $this-&gt;global_groups[ $group ] ) )
+                        $key = $this-&gt;blog_prefix . $key;
+
</ins><span class="cx">                 if ( ! $force &amp;&amp; ! $this-&gt;_exists( $key, $group ) )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -402,6 +437,9 @@
</span><span class="cx">                 if ( empty( $group ) )
</span><span class="cx">                         $group = 'default';
</span><span class="cx"> 
</span><ins>+                if ( $this-&gt;multisite &amp;&amp; ! isset( $this-&gt;global_groups[ $group ] ) )
+                        $key = $this-&gt;blog_prefix . $key;
+
</ins><span class="cx">                 if ( $this-&gt;_exists( $key, $group ) ) {
</span><span class="cx">                         $found = true;
</span><span class="cx">                         $this-&gt;cache_hits += 1;
</span><span class="lines">@@ -430,6 +468,9 @@
</span><span class="cx">                 if ( empty( $group ) )
</span><span class="cx">                         $group = 'default';
</span><span class="cx"> 
</span><ins>+                if ( $this-&gt;multisite &amp;&amp; ! isset( $this-&gt;global_groups[ $group ] ) )
+                        $key = $this-&gt;blog_prefix . $key;
+
</ins><span class="cx">                 if ( ! $this-&gt;_exists( $key, $group ) )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -458,26 +499,33 @@
</span><span class="cx">          * @param int $expire When to expire the cache contents
</span><span class="cx">          * @return bool False if not exists, true if contents were replaced
</span><span class="cx">          */
</span><del>-        function replace($key, $data, $group = 'default', $expire = '') {
</del><ins>+        function replace( $key, $data, $group = 'default', $expire = '' ) {
</ins><span class="cx">                 if ( empty( $group ) )
</span><span class="cx">                         $group = 'default';
</span><span class="cx"> 
</span><del>-                if ( ! $this-&gt;_exists( $key, $group ) )
</del><ins>+                $id = $key;
+                if ( $this-&gt;multisite &amp;&amp; ! isset( $this-&gt;global_groups[ $group ] ) )
+                        $id = $this-&gt;blog_prefix . $key;
+
+                if ( ! $this-&gt;_exists( $id, $group ) )
</ins><span class="cx">                         return false;
</span><span class="cx"> 
</span><del>-                return $this-&gt;set($key, $data, $group, $expire);
</del><ins>+                return $this-&gt;set( $key, $data, $group, $expire );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * Reset keys
</span><span class="cx">          *
</span><span class="cx">          * @since 3.0.0
</span><ins>+         * @deprecated 3.5.0
</ins><span class="cx">          */
</span><span class="cx">         function reset() {
</span><ins>+                _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' );
+
</ins><span class="cx">                 // Clear out non-global caches since the blog ID has changed.
</span><del>-                foreach ( array_keys($this-&gt;cache) as $group ) {
-                        if ( !in_array($group, $this-&gt;global_groups) )
-                                unset($this-&gt;cache[$group]);
</del><ins>+                foreach ( array_keys( $this-&gt;cache ) as $group ) {
+                        if ( ! isset( $this-&gt;global_groups[ $group ] ) )
+                                unset( $this-&gt;cache[ $group ] );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -505,7 +553,10 @@
</span><span class="cx">                 if ( empty( $group ) )
</span><span class="cx">                         $group = 'default';
</span><span class="cx"> 
</span><del>-                if ( is_object($data) )
</del><ins>+                if ( $this-&gt;multisite &amp;&amp; ! isset( $this-&gt;global_groups[ $group ] ) )
+                        $key = $this-&gt;blog_prefix . $key;
+
+                if ( is_object( $data ) )
</ins><span class="cx">                         $data = clone $data;
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;cache[$group][$key] = $data;
</span><span class="lines">@@ -533,6 +584,18 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Switch the interal blog id.
+         *
+         * This changes the blog id used to create keys in blog specific groups.
+         *
+         * @param int $blog_id Blog ID
+         */
+        function switch_to_blog( $blog_id ) {
+                $blog_id = (int) $blog_id;
+                $this-&gt;blog_prefix = $this-&gt;multisite ? $blog_id . ':' : '';
+        }
+
+        /**
</ins><span class="cx">          * Utility function to determine whether a key exists in the cache.
</span><span class="cx">          *
</span><span class="cx">          * @since 3.4.0
</span><span class="lines">@@ -550,11 +613,17 @@
</span><span class="cx">          * @return null|WP_Object_Cache If cache is disabled, returns null.
</span><span class="cx">          */
</span><span class="cx">         function __construct() {
</span><ins>+                global $blog_id;
+
+                $this-&gt;multisite = is_multisite();
+                $this-&gt;blog_prefix =  $this-&gt;multisite ? $blog_id . ':' : '';
+                
+
</ins><span class="cx">                 /**
</span><span class="cx">                  * @todo This should be moved to the PHP4 style constructor, PHP5
</span><span class="cx">                  * already calls __destruct()
</span><span class="cx">                  */
</span><del>-                register_shutdown_function(array(&amp;$this, &quot;__destruct&quot;));
</del><ins>+                register_shutdown_function( array( &amp;$this, '__destruct' ) );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span></span></pre></div>
<a id="trunkwpincludesloadphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/load.php (21402 => 21403)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/load.php        2012-08-02 18:28:10 UTC (rev 21402)
+++ trunk/wp-includes/load.php        2012-08-02 18:31:14 UTC (rev 21403)
</span><span class="lines">@@ -381,7 +381,7 @@
</span><span class="cx">  * @since 3.0.0
</span><span class="cx">  */
</span><span class="cx"> function wp_start_object_cache() {
</span><del>-        global $_wp_using_ext_object_cache;
</del><ins>+        global $_wp_using_ext_object_cache, $blog_id;
</ins><span class="cx"> 
</span><span class="cx">         $first_init = false;
</span><span class="cx">          if ( ! function_exists( 'wp_cache_init' ) ) {
</span><span class="lines">@@ -403,8 +403,8 @@
</span><span class="cx">         // If cache supports reset, reset instead of init if already initialized.
</span><span class="cx">         // Reset signals to the cache that global IDs have changed and it may need to update keys
</span><span class="cx">         // and cleanup caches.
</span><del>-        if ( !$first_init &amp;&amp; function_exists('wp_cache_reset') )
-                wp_cache_reset();
</del><ins>+        if ( ! $first_init &amp;&amp; function_exists( 'wp_cache_switch_to_blog' ) )
+                wp_cache_switch_to_blog( $blog_id );
</ins><span class="cx">         else
</span><span class="cx">                 wp_cache_init();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesmsblogsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-blogs.php (21402 => 21403)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-blogs.php        2012-08-02 18:28:10 UTC (rev 21402)
+++ trunk/wp-includes/ms-blogs.php        2012-08-02 18:31:14 UTC (rev 21403)
</span><span class="lines">@@ -487,18 +487,22 @@
</span><span class="cx">                         $current_user-&gt;for_blog( $blog_id );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( is_object( $wp_object_cache ) &amp;&amp; isset( $wp_object_cache-&gt;global_groups ) )
-                $global_groups = $wp_object_cache-&gt;global_groups;
-        else
-                $global_groups = false;
-
-        wp_cache_init();
-        if ( function_exists('wp_cache_add_global_groups') ) {
-                if ( is_array( $global_groups ) )
-                        wp_cache_add_global_groups( $global_groups );
</del><ins>+        if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
+                wp_cache_switch_to_blog( $blog_id );
+        } else {
+                if ( is_object( $wp_object_cache ) &amp;&amp; isset( $wp_object_cache-&gt;global_groups ) )
+                        $global_groups = $wp_object_cache-&gt;global_groups;
</ins><span class="cx">                 else
</span><del>-                        wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
-                wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
</del><ins>+                        $global_groups = false;
+        
+                wp_cache_init();
+                if ( function_exists('wp_cache_add_global_groups') ) {
+                        if ( is_array( $global_groups ) )
+                                wp_cache_add_global_groups( $global_groups );
+                        else
+                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
+                        wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
+                }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         do_action('switch_blog', $blog_id, $prev_blog_id);
</span><span class="lines">@@ -551,18 +555,22 @@
</span><span class="cx">                         $current_user-&gt;for_blog( $blog_id );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( is_object( $wp_object_cache ) &amp;&amp; isset( $wp_object_cache-&gt;global_groups ) )
-                $global_groups = $wp_object_cache-&gt;global_groups;
-        else
-                $global_groups = false;
-
-        wp_cache_init();
-        if ( function_exists('wp_cache_add_global_groups') ) {
-                if ( is_array( $global_groups ) )
-                        wp_cache_add_global_groups( $global_groups );
</del><ins>+        if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
+                wp_cache_switch_to_blog( $blog_id );
+        } else {
+                if ( is_object( $wp_object_cache ) &amp;&amp; isset( $wp_object_cache-&gt;global_groups ) )
+                        $global_groups = $wp_object_cache-&gt;global_groups;
</ins><span class="cx">                 else
</span><del>-                        wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
-                wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
</del><ins>+                        $global_groups = false;
+        
+                wp_cache_init();
+                if ( function_exists('wp_cache_add_global_groups') ) {
+                        if ( is_array( $global_groups ) )
+                                wp_cache_add_global_groups( $global_groups );
+                        else
+                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
+                        wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
+                }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         do_action('switch_blog', $blog_id, $prev_blog_id);
</span></span></pre>
</div>
</div>

</body>
</html>