wp_prime_network_option_caches( int|null $network_id, string[] $options )
- Since
- 6.6.0
- Source
wp-includes/option.php:694
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
- Scaling
- Scales with input
- Instructions
- 7–102
- Plugin surface
- None
- Called by
- 2
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 147.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get_multiple()called directly - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
wp_installing() | 7 | wp_installing() |
!wp_installing() && is_multisite() && !$network_id | 13 | wp_installing(), is_multisite() |
!wp_installing() && !is_multisite() | 13 | wp_installing(), is_multisite(), wp_prime_option_caches() |
!wp_installing() && is_multisite() && empty($options_to_prime) | 39–44 | wp_installing(), is_multisite(), array_values(), wp_cache_get_multiple(), wp_cache_get() |
!wp_installing() && is_multisite() && empty($options_to_prime) | 42–47 | wp_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–84 | wp_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–87 | wp_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–94 | wp_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–97 | wp_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–99 | wp_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–102 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 147 | 7–102 | 21 | |
| 8.5 | 147 | 7–102 | 21 | |
| 8.4 | 147 | 7–102 | 21 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 152 | 7–107 | 21 | |
| 8.2 | 152 | 7–107 | 21 | |
| 8.1 | 152 | 7–107 | 21 | |
| 7.4 | 152 | 7–107 | 21 |
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
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- is_multisite()Determines whether Multisite is enabled.
- wp_prime_option_caches()Primes specific options into the cache with a single database query.
- get_current_network_id()Retrieves the current network ID.
- wp_cache_get_multiple()Retrieves multiple values from the cache in one call.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- maybe_unserialize()Unserializes data only if it was serialized.
- wp_cache_set_multiple()Sets multiple values to the cache in one call.
- wp_cache_set()Saves the data to the cache.
Used by · 2
- wp_load_core_site_options()Loads and primes caches of certain often requested network options if is_multisite().
- wp_prime_site_option_caches()Primes specific network options for the current network into the cache with a single database query.
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.
Signature, return type and hooks compared across 5 parsed releases.
$network_id retyped from int to int|null.verified against sourceAbout 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.