wp_prime_option_caches()WP 6.4.0

Primes the specified options in the cache so subsequent get_option() calls do not issue a separate database query for each option.

The function checks which options are already cached and loads only the missing options with one database query. Autoloaded options are skipped because they are already in the shared alloptions cache. Names of nonexistent options are added to the notoptions cache.

No Hooks.

Returns

null.

  • void - returns nothing.

Usage

wp_prime_option_caches( $options );
$options(string[]) (required)
Names of the options to load into the cache.

Examples

0

#1 Preloading multiple options

Options are loaded in a single request, after which each call to get_option() gets the value from the cache.

$option_names = [
	'my_plugin_settings',
	'my_plugin_version',
	'my_plugin_license',
];

wp_prime_option_caches( $option_names );

$settings = get_option( 'my_plugin_settings' );
$version  = get_option( 'my_plugin_version' );
$license  = get_option( 'my_plugin_license' );

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 6.4.0 Introduced.

wp_prime_option_caches() code WP 7.0.4

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' );
	}
}