wppaste
WordPress

wp_prime_network_option_caches( int|null $network_id, string[] $options )

Since
6.6.0
Source
wp-includes/option.php:694
Primes specific network options into the cache with a single database query.

Description

Only network options that do not already exist in cache will be loaded.

If site is not multisite, then call wp_prime_option_caches().

Compatibility

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

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$network_idint|null
ID of the network. Can be null to default to the current network ID.
$optionsstring[]
An array of option names to be loaded.

Performance profile

How much work a call to wp_prime_network_option_caches() 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
Heavy

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
7–102

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What it touches

  • cacheobject cachewp_cache_get_multiple()called directly
  • serializeserialisationmaybe_unserialize()called directly
  • sqldatabase query->get_results()called directly

Further down the call graph this can also reach hook. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 11 distinct outcomes

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

WhenInstructionsCalls it makes
wp_installing()7wp_installing()
!wp_installing() && is_multisite() && !$network_id13wp_installing(), is_multisite()
!wp_installing() && !is_multisite()13wp_installing(), is_multisite(), wp_prime_option_caches()
!wp_installing() && is_multisite() && empty($options_to_prime)39–44wp_installing(), is_multisite(), array_values(), wp_cache_get_multiple(), wp_cache_get()
!wp_installing() && is_multisite() && empty($options_to_prime)42–47wp_installing(), is_multisite(), get_current_network_id(), array_values(), wp_cache_get_multiple(), wp_cache_get()
!wp_installing() && is_multisite() && !empty($options_to_prime)78–84wp_installing(), is_multisite(), array_values(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple()
!wp_installing() && is_multisite() && !empty($options_to_prime)81–87wp_installing(), is_multisite(), get_current_network_id(), array_values(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple()
!wp_installing() && is_multisite() && !empty($options_to_prime)87–94wp_installing(), is_multisite(), array_values(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple(), array_diff()
!wp_installing() && is_multisite() && !empty($options_to_prime)90–97wp_installing(), is_multisite(), get_current_network_id(), array_values(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple(), array_diff()
!wp_installing() && is_multisite() && !empty($options_to_prime)92–99wp_installing(), is_multisite(), array_values(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple(), array_diff(), wp_cache_set()
!wp_installing() && is_multisite() && !empty($options_to_prime)95–102wp_installing(), is_multisite(), get_current_network_id(), array_values(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple(), array_diff(), wp_cache_set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1477–10221
8.51477–10221
8.41477–102215 fewer instructions than PHP 8.3
8.31527–10721
8.21527–10721
8.11527–10721
7.41527–10721

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 · 9

Used by · 2

Source code

function wp_prime_network_option_caches( $network_id, array $options ) {	global $wpdb; 	if ( wp_installing() ) {		return;	} 	if ( ! is_multisite() ) {		wp_prime_option_caches( $options );		return;	} 	if ( $network_id && ! is_numeric( $network_id ) ) {		return;	} 	$network_id = (int) $network_id; 	// Fallback to the current network if a network ID is not specified.	if ( ! $network_id ) {		$network_id = get_current_network_id();	} 	$cache_keys = array();	foreach ( $options as $option ) {		$cache_keys[ $option ] = "{$network_id}:{$option}";	} 	$cache_group    = 'site-options';	$cached_options = wp_cache_get_multiple( array_values( $cache_keys ), $cache_group ); 	$notoptions_key = "$network_id:notoptions";	$notoptions     = wp_cache_get( $notoptions_key, $cache_group ); 	if ( ! is_array( $notoptions ) ) {		$notoptions = array();	} 	// Filter options that are not in the cache.	$options_to_prime = array();	foreach ( $cache_keys as $option => $cache_key ) {		if (			( ! isset( $cached_options[ $cache_key ] ) || false === $cached_options[ $cache_key ] )			&& ! isset( $notoptions[ $option ] )		) {			$options_to_prime[] = $option;		}	} 	// Bail early if there are no options to be loaded.	if ( empty( $options_to_prime ) ) {		return;	} 	$query_args   = $options_to_prime;	$query_args[] = $network_id;	$results      = $wpdb->get_results(		$wpdb->prepare(			sprintf(				"SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN (%s) AND site_id = %s",				implode( ',', array_fill( 0, count( $options_to_prime ), '%s' ) ),				'%d'			),			$query_args		)	); 	$data          = array();	$options_found = array();	foreach ( $results as $result ) {		$key                = $result->meta_key;		$cache_key          = $cache_keys[ $key ];		$data[ $cache_key ] = maybe_unserialize( $result->meta_value );		$options_found[]    = $key;	}	wp_cache_set_multiple( $data, $cache_group );	// If all options were found, no need to update `notoptions` cache.	if ( count( $options_found ) === count( $options_to_prime ) ) {		return;	}

Changelog

Introduced in 6.6.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

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

6.8.8
Parameter $network_id retyped from int to int|null.verified against source
6.6.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/option.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.