wp_prime_option_caches

函数
wp_prime_option_caches ( $options )
参数
  • (array) $options An array of option names to be loaded.
    Required:
定义位置
相关方法
wp_prime_option_caches_by_group_prime_post_caches_prime_site_caches_prime_term_caches_prime_comment_caches
引入
6.4.0
弃用
-

通过一次数据库查询,将特定选项置入缓存。

只有缓存中不存在的选项才会被加载。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_prime_option_caches( $options ) {
$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;
}
global $wpdb;
$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' );
}
}
function wp_prime_option_caches( $options ) { $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; } global $wpdb; $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' ); } }
function wp_prime_option_caches( $options ) {
	$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;
	}

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

常见问题

FAQs
查看更多 >