wp_prime_option_caches( string[] $options )
- Since
- 6.4.0
- Source
wp-includes/option.php:270
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
- Scaling
- Scales with input
- Instructions
- 23–78
- Plugin surface
- None
- Called by
- 5
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 103.
Nothing here hands control to plugin code.
5 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 - sqldatabase query
->get_results()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($options_to_prime) | 23–25 | wp_load_alloptions(), wp_cache_get_multiple(), wp_cache_get() |
!empty($options_to_prime) | 57–60 | wp_load_alloptions(), wp_cache_get_multiple(), wp_cache_get(), array_fill(), sprintf(), ->prepare(), ->get_results(), wp_cache_set_multiple() |
!empty($options_to_prime) | 69–73 | wp_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–78 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 103 | 23–78 | 15 | |
| 8.5 | 103 | 23–78 | 15 | |
| 8.4 | 103 | 23–78 | 15 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 106 | 23–81 | 15 | |
| 8.2 | 106 | 23–81 | 15 | |
| 8.1 | 106 | 23–81 | 15 | |
| 7.4 | 106 | 23–81 | 15 |
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
- wp_load_alloptions()Loads and caches all autoloaded options, if available or all options.
- 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.
- wp_cache_set_multiple()Sets multiple values to the cache in one call.
- wp_cache_set()Saves the data to the cache.
Used by · 5
- get_options()Retrieves multiple options.
- get_transient()Retrieves the value of a transient.
- set_transient()Sets/updates the value of a transient.
- wp_prime_network_option_caches()Primes specific network options into the cache with a single database query.
- wp_prime_option_caches_by_group()Primes the cache of all options registered with a specific option group.
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.
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.