_prime_comment_caches()
云策文档标注
概述
_prime_comment_caches() 函数用于将指定评论ID数组中尚未缓存的评论添加到缓存中,并可选择更新评论元数据缓存。它通过查询数据库获取未缓存的评论数据,并调用 update_comment_cache() 进行缓存更新。
关键要点
- 函数接受两个参数:$comment_ids(评论ID数组,必需)和 $update_meta_cache(是否更新元数据缓存,可选,默认为 true)。
- 使用 _get_non_cached_ids() 检查哪些评论ID尚未缓存,避免重复查询。
- 如果 $update_meta_cache 为 true,会调用 wp_lazyload_comment_meta() 懒加载评论元数据。
- 相关函数包括 update_comment_cache()、wp_lazyload_comment_meta() 和 _get_non_cached_ids()。
- 在 WordPress 6.3.0 中改用 wp_lazyload_comment_meta() 进行元数据懒加载,6.1.0 版本后不再标记为“私有”。
代码示例
function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
update_comment_cache( $fresh_comments, false );
}
if ( $update_meta_cache ) {
wp_lazyload_comment_meta( $comment_ids );
}
}
原文内容
Adds any comments from the given IDs to the cache that do not already exist in cache.
Description
See also
Parameters
$comment_idsint[]required-
Array of comment IDs.
$update_meta_cachebooloptional-
Whether to update the meta cache.
Default:
true
Source
function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
update_comment_cache( $fresh_comments, false );
}
if ( $update_meta_cache ) {
wp_lazyload_comment_meta( $comment_ids );
}
}
Changelog
| Version | Description |
|---|---|
| 6.3.0 | Use wp_lazyload_comment_meta() for lazy-loading of comment meta. |
| 6.1.0 | This function is no longer marked as “private”. |
| 4.4.0 | Introduced. |