update_site_cache()
云策文档标注
概述
update_site_cache() 函数用于更新站点缓存,支持批量处理站点对象并可选更新站点元数据缓存。
关键要点
- 函数接受一个站点对象数组作为必需参数,以及一个可选的布尔参数 $update_meta_cache 来控制是否更新站点元数据缓存,默认值为 true。
- 内部实现通过遍历站点对象,提取站点 ID 并构建缓存数据,使用 wp_cache_add_multiple() 将数据添加到 'sites' 和 'blog-details' 缓存组。
- 如果 $update_meta_cache 为 true,则调用 update_sitemeta_cache() 函数更新站点元数据缓存。
代码示例
function update_site_cache( $sites, $update_meta_cache = true ) {
if ( ! $sites ) {
return;
}
$site_ids = array();
$site_data = array();
$blog_details_data = array();
foreach ( $sites as $site ) {
$site_ids[] = $site->blog_id;
$site_data[ $site->blog_id ] = $site;
$blog_details_data[ $site->blog_id . 'short' ] = $site;
}
wp_cache_add_multiple( $site_data, 'sites' );
wp_cache_add_multiple( $blog_details_data, 'blog-details' );
if ( $update_meta_cache ) {
update_sitemeta_cache( $site_ids );
}
}注意事项
- 函数在 WordPress 4.6.0 版本引入,并在 5.1.0 版本添加了 $update_meta_cache 参数。
- 相关函数包括 wp_cache_add_multiple() 和 update_sitemeta_cache(),用于缓存操作和元数据更新。
- 该函数被 prime_site_caches() 和 WP_MS_Sites_List_Table::prepare_items() 等函数调用,用于站点缓存管理和列表显示准备。
原文内容
Updates sites in cache.
Parameters
$sitesarrayrequired-
Array of site objects.
$update_meta_cachebooloptional-
Whether to update site meta cache.
Default:
true
Source
function update_site_cache( $sites, $update_meta_cache = true ) {
if ( ! $sites ) {
return;
}
$site_ids = array();
$site_data = array();
$blog_details_data = array();
foreach ( $sites as $site ) {
$site_ids[] = $site->blog_id;
$site_data[ $site->blog_id ] = $site;
$blog_details_data[ $site->blog_id . 'short' ] = $site;
}
wp_cache_add_multiple( $site_data, 'sites' );
wp_cache_add_multiple( $blog_details_data, 'blog-details' );
if ( $update_meta_cache ) {
update_sitemeta_cache( $site_ids );
}
}