_prime_site_caches()
云策文档标注
概述
_prime_site_caches() 函数用于将指定 ID 的站点添加到缓存中,如果它们尚未在缓存中。它通过查询数据库获取未缓存的站点数据,并可选地更新元数据缓存。
关键要点
- 函数接受一个 ID 数组作为必需参数,以及一个可选的布尔参数 $update_meta_cache 来控制是否更新元数据缓存。
- 内部使用 _get_non_cached_ids() 来识别未缓存的站点 ID,然后通过 SQL 查询从数据库获取这些站点的数据。
- 调用 update_site_cache() 来更新站点缓存,并根据 $update_meta_cache 参数决定是否使用 wp_lazyload_site_meta() 懒加载站点元数据。
- 函数自 WordPress 4.6.0 引入,在 6.3.0 版本中改用 wp_lazyload_site_meta() 进行元数据懒加载。
代码示例
function _prime_site_caches( $ids, $update_meta_cache = true ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
update_site_cache( $fresh_sites, false );
}
if ( $update_meta_cache ) {
wp_lazyload_site_meta( $ids );
}
}注意事项
- 此函数主要用于内部缓存优化,开发者通常不需要直接调用,但可以在需要批量预加载站点数据时使用。
- 参数 $ids 必须是数组类型,包含要缓存的站点 ID;$update_meta_cache 默认为 true,表示同时更新元数据缓存。
- 函数依赖于全局 $wpdb 对象进行数据库查询,确保在调用前已正确初始化 WordPress 环境。
原文内容
Adds any sites from the given IDs to the cache that do not already exist in cache.
Description
See also
Parameters
$idsarrayrequired-
ID list.
$update_meta_cachebooloptional-
Whether to update the meta cache.
Default:
true
Source
function _prime_site_caches( $ids, $update_meta_cache = true ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
update_site_cache( $fresh_sites, false );
}
if ( $update_meta_cache ) {
wp_lazyload_site_meta( $ids );
}
}
Changelog
| Version | Description |
|---|---|
| 6.3.0 | Use wp_lazyload_site_meta() for lazy-loading of site meta. |
| 6.1.0 | This function is no longer marked as “private”. |
| 5.1.0 | Introduced the $update_meta_cache parameter. |
| 4.6.0 | Introduced. |