_prime_network_caches()
云策文档标注
概述
_prime_network_caches() 函数用于将指定网络 ID 中未缓存的部分添加到缓存中,以提高网络数据查询效率。它通过检查缓存状态并查询数据库来更新缓存。
关键要点
- 函数接受一个网络 ID 数组作为参数,仅处理未缓存的 ID。
- 使用 _get_non_cached_ids() 来识别未缓存的网络 ID。
- 通过 SQL 查询从数据库获取网络数据,并调用 update_network_cache() 更新缓存。
- 此函数在 WordPress 4.6.0 中引入,并在 6.1.0 中不再标记为“私有”。
代码示例
function _prime_network_caches( $network_ids ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
update_network_cache( $fresh_networks );
}
}注意事项
- 函数内部使用全局 $wpdb 对象进行数据库查询,需确保数据库连接正常。
- SQL 查询未使用预编译语句,但通过 intval 处理 ID 以增强安全性。
- 此函数通常由 WP_Network_Query::get_networks() 调用,开发者应优先使用高级 API。
原文内容
Adds any networks from the given IDs to the cache that do not already exist in cache.
Description
See also
Parameters
$network_idsarrayrequired-
Array of network IDs.
Source
function _prime_network_caches( $network_ids ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
update_network_cache( $fresh_networks );
}
}