Index: wp-includes/ms-blogs.php
===================================================================
--- wp-includes/ms-blogs.php	(revision 21378)
+++ wp-includes/ms-blogs.php	(working copy)
@@ -487,18 +487,22 @@
 			$current_user->for_blog( $blog_id );
 	}
 
-	if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
-		$global_groups = $wp_object_cache->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 );
+	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
+		wp_cache_switch_to_blog( $blog_id );
+	} else {
+		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
+			$global_groups = $wp_object_cache->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' ));
+			$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' ));
+		}
 	}
 
 	do_action('switch_blog', $blog_id, $prev_blog_id);
@@ -551,18 +555,22 @@
 			$current_user->for_blog( $blog_id );
 	}
 
-	if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
-		$global_groups = $wp_object_cache->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 );
+	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
+		wp_cache_switch_to_blog( $blog_id );
+	} else {
+		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
+			$global_groups = $wp_object_cache->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' ));
+			$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' ));
+		}
 	}
 
 	do_action('switch_blog', $blog_id, $prev_blog_id);
Index: wp-includes/cache.php
===================================================================
--- wp-includes/cache.php	(revision 21377)
+++ wp-includes/cache.php	(working copy)
@@ -180,6 +180,19 @@
 }
 
 /**
+ * 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->switch_to_blog( $blog_id );
+}
+
+/**
  * Adds a group or set of groups to the list of global groups.
  *
  * @since 2.6.0
@@ -292,6 +305,8 @@
 		if ( empty( $group ) )
 			$group = 'default';
 
+		$key = $this->key( $id, $group );
+
 		if ( $this->_exists($key, $group) )
 			return false;
 
@@ -325,7 +340,9 @@
 	function decr( $key, $offset = 1, $group = 'default' ) {
 		if ( empty( $group ) )
 			$group = 'default';
-		
+
+		$key = $this->key( $id, $group );
+
 		if ( ! $this->_exists( $key, $group ) )
 			return false;
 
@@ -361,6 +378,8 @@
 		if ( empty( $group ) )
 			$group = 'default';
 
+		$key = $this->key( $id, $group );
+
 		if ( ! $force && ! $this->_exists( $key, $group ) )
 			return false;
 
@@ -402,6 +421,8 @@
 		if ( empty( $group ) )
 			$group = 'default';
 
+		$key = $this->key( $id, $group );
+
 		if ( $this->_exists( $key, $group ) ) {
 			$found = true;
 			$this->cache_hits += 1;
@@ -430,6 +451,8 @@
 		if ( empty( $group ) )
 			$group = 'default';
 
+		$key = $this->key( $id, $group );
+
 		if ( ! $this->_exists( $key, $group ) )
 			return false;
 
@@ -447,6 +470,27 @@
 	}
 
 	/**
+	 * Create a key from a key and group
+	 *
+	 * @since 3.5.0
+	 *
+	 * @param string $key A cache key
+	 * @param string $group A group
+	 * @return string A key for an individual cache bucket
+	 */
+	function key( $key, $group ) {	
+		if ( empty( $group ) )
+			$group = 'default';
+
+		if ( false !== array_search( $group, $this->global_groups ) )
+			$prefix = '';
+		else
+			$prefix = $this->blog_prefix;
+
+		return preg_replace('/\s+/', '', "$prefix$group:$key");
+	}
+
+	/**
 	 * Replace the contents in the cache, if contents already exist
 	 *
 	 * @since 2.0.0
@@ -462,6 +506,8 @@
 		if ( empty( $group ) )
 			$group = 'default';
 
+		$key = $this->key( $id, $group );
+
 		if ( ! $this->_exists( $key, $group ) )
 			return false;
 
@@ -505,6 +551,8 @@
 		if ( empty( $group ) )
 			$group = 'default';
 
+		$key = $this->key( $id, $group );
+
 		if ( is_object($data) )
 			$data = clone $data;
 
@@ -533,6 +581,17 @@
 	}
 
 	/**
+	 * 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 ) {
+		$this->blog_prefix = is_multisite() ? get_current_blog_id() . ':' : '';
+	}
+
+	/**
 	 * Utility function to determine whether a key exists in the cache.
 	 *
 	 * @since 3.4.0
@@ -550,6 +609,8 @@
 	 * @return null|WP_Object_Cache If cache is disabled, returns null.
 	 */
 	function __construct() {
+		$this->blog_prefix = is_multisite() ? get_current_blog_id() . ':' : '';
+
 		/**
 		 * @todo This should be moved to the PHP4 style constructor, PHP5
 		 * already calls __destruct()
