wppaste
WordPress

wp_prime_option_caches( string[] $options )

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

Description

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

Compatibility

WordPress
since 6.4.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

$optionsstring[]
An array of option names to be loaded.

Performance profile

How much work a call to wp_prime_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
23–78

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 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
  • sqldatabase query->get_results()called directly
  • hookthird-party callbacksapply_filters()one call below wp_prime_option_caches()

What one call costs · 4 distinct outcomes

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

WhenInstructionsCalls it makes
empty($options_to_prime)23–25wp_load_alloptions(), wp_cache_get_multiple(), wp_cache_get()
!empty($options_to_prime)57–60wp_load_alloptions(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple()
!empty($options_to_prime)69–73wp_load_alloptions(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple(), array_keys(), array_diff()
!empty($options_to_prime)74–78wp_load_alloptions(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple(), array_keys(), array_diff(), wp_cache_set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10323–7815
8.510323–7815
8.410323–78153 fewer instructions than PHP 8.3
8.310623–8115
8.210623–8115
8.110623–8115
7.410623–8115

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

Used by · 5

Source code

function wp_prime_option_caches( $options ) {	global $wpdb; 	$alloptions     = wp_load_alloptions();	$cached_options = wp_cache_get_multiple( $options, 'options' );	$notoptions     = wp_cache_get( 'notoptions', 'options' );	if ( ! is_array( $notoptions ) ) {		$notoptions = array();	} 	// Filter options that are not in the cache.	$options_to_prime = array();	foreach ( $options as $option ) {		if (			( ! isset( $cached_options[ $option ] ) || false === $cached_options[ $option ] )			&& ! isset( $alloptions[ $option ] )			&& ! isset( $notoptions[ $option ] )		) {			$options_to_prime[] = $option;		}	} 	// Bail early if there are no options to be loaded.	if ( empty( $options_to_prime ) ) {		return;	} 	$results = $wpdb->get_results(		$wpdb->prepare(			sprintf(				"SELECT option_name, option_value FROM $wpdb->options WHERE option_name IN (%s)",				implode( ',', array_fill( 0, count( $options_to_prime ), '%s' ) )			),			$options_to_prime		)	); 	$options_found = array();	foreach ( $results as $result ) {		/*		 * The cache is primed with the raw value (i.e. not maybe_unserialized).		 *		 * `get_option()` will handle unserializing the value as needed.		 */		$options_found[ $result->option_name ] = $result->option_value;	}	wp_cache_set_multiple( $options_found, 'options' ); 	// If all options were found, no need to update `notoptions` cache.	if ( count( $options_found ) === count( $options_to_prime ) ) {		return;	} 	$options_not_found = array_diff( $options_to_prime, array_keys( $options_found ) ); 	// Add the options that were not found to the cache.	$update_notoptions = false;	foreach ( $options_not_found as $option_name ) {		if ( ! isset( $notoptions[ $option_name ] ) ) {			$notoptions[ $option_name ] = true;			$update_notoptions          = true;		}	} 	// Only update the cache if it was modified.	if ( $update_notoptions ) {		wp_cache_set( 'notoptions', $notoptions, 'options' );	}}

Changelog

Introduced in 6.4.0. Unchanged from 6.7.7 through 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.

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.