wppaste
WordPress

wp_cache_switch_to_blog_fallback()

Since
7.0.0
Source
wp-includes/ms-blogs.php:611
Fallback logic for switching cache context when an object cache drop-in lacks a switch_to_blog() method.

Description

Reinitializes the cache and restores global/non-persistent groups.

Used by the wp_cache_switch_to_blog() compatibility function, abstracted only to allow for unit testing outside of the drop-in plugin inclusion circus.

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to wp_cache_switch_to_blog_fallback() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Only touches the object cache, via wp_cache_switch_to_blog_fallback().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
16–72

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 74.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_switch_to_blog_fallback()this function does it

What one call costs · 20 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_cache_switch_to_blog_fallback() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!function_exists()16–21wp_cache_init(), function_exists(), function_exists()
always22–29wp_cache_init(), function_exists(), function_exists(), wp_cache_add_non_persistent_groups()
always22–29wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists()
function_exists()28–37wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array() && !function_exists()31–39wp_is_numeric_array(), wp_cache_init(), function_exists(), function_exists()
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array() && !function_exists()35–43wp_is_numeric_array(), array_keys(), wp_cache_init(), function_exists(), function_exists()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array()37–47wp_is_numeric_array(), wp_cache_init(), function_exists(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array()37–47wp_is_numeric_array(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists()
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array()41–51wp_is_numeric_array(), array_keys(), wp_cache_init(), function_exists(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array()41–51wp_is_numeric_array(), array_keys(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array() && function_exists()43–55wp_is_numeric_array(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array() && !function_exists()47–52wp_is_numeric_array(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), function_exists()
8 further outcomes, up to 72 instructions
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array() && function_exists()47–59wp_is_numeric_array(), array_keys(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array() && !function_exists()51–56wp_is_numeric_array(), array_keys(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), function_exists()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array()53–60wp_is_numeric_array(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array()53–60wp_is_numeric_array(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists()
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array()57–64wp_is_numeric_array(), array_keys(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array()57–64wp_is_numeric_array(), array_keys(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists()
is_object($wp_object_cache) && isset($wp_object_cache) && wp_is_numeric_array() && function_exists()59–68wp_is_numeric_array(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists(), wp_cache_add_non_persistent_groups()
is_object($wp_object_cache) && isset($wp_object_cache) && !wp_is_numeric_array() && function_exists()63–72wp_is_numeric_array(), array_keys(), array_keys(), array_diff(), array_values(), wp_cache_init(), function_exists(), wp_cache_add_global_groups(), function_exists(), wp_cache_add_non_persistent_groups()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 74 instructions, 16–72 executed per call, 15 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

Used by · 1

  • wp_cache_switch_to_blog()Used when switch_to_blog() and restore_current_blog() are called, but only when a persistent object cache drop-in plugin has omitted the wp_cache_switch_to_blog() function that was introduced in 3.5.0.

Source code

function wp_cache_switch_to_blog_fallback() {	global $wp_object_cache; 	$global_groups         = false;	$non_persistent_groups = false; 	if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) && is_array( $wp_object_cache->global_groups ) ) { 		// Get the global groups as they are.		$group_names = $wp_object_cache->global_groups; 		// Get global group keys if non-numeric array.		if ( ! wp_is_numeric_array( $group_names ) ) {			$group_names = array_keys( $group_names );		} 		$global_groups = $group_names; 		/*		 * Non-persistent groups: Check for no_mc_groups first (memcached drop-in).		 * Fall back to cache structure (default cache).		 */		if ( isset( $wp_object_cache->no_mc_groups ) && is_array( $wp_object_cache->no_mc_groups ) && ! empty( $wp_object_cache->no_mc_groups ) ) {			$non_persistent_groups = $wp_object_cache->no_mc_groups;		} elseif ( isset( $wp_object_cache->cache ) && is_array( $wp_object_cache->cache ) ) {			$all_groups            = array_keys( $wp_object_cache->cache );			$non_persistent_groups = array_values( array_diff( $all_groups, $global_groups ) );		}	} 	wp_cache_init(); 	if ( function_exists( 'wp_cache_add_global_groups' ) ) {		if ( ! is_array( $global_groups ) || empty( $global_groups ) ) {			$global_groups = array(				'blog-details',				'blog-id-cache',				'blog-lookup',				'blog_meta',				'global-posts',				'image_editor',				'networks',				'network-queries',				'sites',				'site-details',				'site-options',				'site-queries',				'site-transient',				'theme_files',				'translation_files',				'rss',				'users',				'user-queries',				'user_meta',				'useremail',				'userlogins',				'userslugs',			);		} 		wp_cache_add_global_groups( $global_groups );	} 	if ( function_exists( 'wp_cache_add_non_persistent_groups' ) ) {		if ( ! is_array( $non_persistent_groups ) || empty( $non_persistent_groups ) ) {			$non_persistent_groups = array(				'counts',				'plugins',				'theme_json',			);		} 		wp_cache_add_non_persistent_groups( $non_persistent_groups );	}}

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/ms-blogs.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.