函数文档

wp_prime_network_option_caches()

💡 云策文档标注

概述

wp_prime_network_option_caches() 函数用于通过单次数据库查询,将指定的网络选项预加载到缓存中,以提高性能。它仅加载缓存中不存在的选项,并在非多站点环境下回退到 wp_prime_option_caches()。

关键要点

  • 函数通过单次数据库查询预加载网络选项到缓存,优化性能
  • 仅处理缓存中不存在的选项,避免冗余操作
  • 非多站点环境下自动调用 wp_prime_option_calls()
  • 支持指定网络 ID,默认为当前网络
  • 使用 wp_cache_get_multiple() 和 wp_cache_set_multiple() 进行批量缓存操作
  • 维护 notoptions 缓存以记录未找到的选项

代码示例

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

    $options_not_found = array_diff( $options_to_prime, $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_key, $notoptions, $cache_group );
    }
}

注意事项

  • 函数在 WordPress 安装模式下(wp_installing() 返回 true)会直接返回,不执行任何操作
  • 如果 $network_id 非数字且非 null,函数会提前返回
  • 缓存键格式为 "{$network_id}:{$option}",组名为 'site-options'
  • 使用 wpdb::prepare() 确保 SQL 查询的安全性
  • 未找到的选项会记录在 notoptions 缓存中,避免重复查询

📄 原文内容

Primes specific network options into the cache with a single database query.

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() .

Parameters

$network_idint|nullrequired
ID of the network. Can be null to default to the current network ID.
$optionsstring[]required
An array of option names to be loaded.

Source

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

	$options_not_found = array_diff( $options_to_prime, $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_key, $notoptions, $cache_group );
	}
}

Changelog

Version Description
6.6.0 Introduced.