_get_non_cached_ids()
云策文档标注
概述
_get_non_cached_ids() 函数用于从一组 ID 中筛选出尚未缓存的 ID。它通过验证和去重处理输入,并利用 wp_cache_get_multiple() 检查缓存状态。
关键要点
- 参数:$object_ids(必需,整数数组)和 $cache_group(必需,缓存组字符串)。
- 返回值:返回未在缓存中找到的 ID 数组。
- 内部处理:使用 _validate_cache_id 过滤、intval 转换和 array_unique 去重。
- 依赖 wp_cache_get_multiple() 进行批量缓存检查。
代码示例
function _get_non_cached_ids( $object_ids, $cache_group ) {
$object_ids = array_filter( $object_ids, '_validate_cache_id' );
$object_ids = array_unique( array_map( 'intval', $object_ids ), SORT_NUMERIC );
if ( empty( $object_ids ) ) {
return array();
}
$non_cached_ids = array();
$cache_values = wp_cache_get_multiple( $object_ids, $cache_group );
foreach ( $cache_values as $id => $value ) {
if ( false === $value ) {
$non_cached_ids[] = (int) $id;
}
}
return $non_cached_ids;
}注意事项
- 该函数在 WordPress 6.1.0 中不再标记为“私有”,自 3.4.0 版本引入。
- 常用于 _prime_term_caches、_prime_post_caches 等函数中,以优化缓存预加载。
原文内容
Retrieves IDs that are not already present in the cache.
Parameters
$object_idsint[]required-
Array of IDs.
$cache_groupstringrequired-
The cache group to check against.
Source
function _get_non_cached_ids( $object_ids, $cache_group ) {
$object_ids = array_filter( $object_ids, '_validate_cache_id' );
$object_ids = array_unique( array_map( 'intval', $object_ids ), SORT_NUMERIC );
if ( empty( $object_ids ) ) {
return array();
}
$non_cached_ids = array();
$cache_values = wp_cache_get_multiple( $object_ids, $cache_group );
foreach ( $cache_values as $id => $value ) {
if ( false === $value ) {
$non_cached_ids[] = (int) $id;
}
}
return $non_cached_ids;
}