wp_cache_get_salted()
云策文档标注
概述
wp_cache_get_salted() 是 WordPress 6.9.0 引入的缓存函数,用于在缓存数据有效且未更改时检索数据。它通过结合缓存键、组和盐值来验证缓存的有效性。
关键要点
- 函数接受三个必需参数:$cache_key(缓存键)、$group(缓存组)和 $salt(盐值,可以是字符串或数组)。
- 返回缓存数据(如果有效)或 false(如果缓存不存在或已过时)。
- 内部实现基于 wp_cache_get() 获取缓存,并检查缓存数组中的 'salt' 和 'data' 键是否匹配提供的盐值。
- 该函数被多个核心查询函数(如 WP_Query、WP_Term_Query 等)使用,以优化数据检索性能。
代码示例
function wp_cache_get_salted( $cache_key, $group, $salt ) {
$salt = is_array( $salt ) ? implode( ':', $salt ) : $salt;
$cache = wp_cache_get( $cache_key, $group );
if ( ! is_array( $cache ) ) {
return false;
}
if ( ! isset( $cache['salt'] ) || ! isset( $cache['data'] ) || $salt !== $cache['salt'] ) {
return false;
}
return $cache['data'];
}
原文内容
Retrieves cached data if valid and unchanged.
Parameters
$cache_keystringrequired-
The cache key used for storage and retrieval.
$groupstringrequired-
The cache group used for organizing data.
$saltstring|string[]required-
The timestamp (or multiple timestamps if an array) indicating when the cache group(s) were last updated.
Source
function wp_cache_get_salted( $cache_key, $group, $salt ) {
$salt = is_array( $salt ) ? implode( ':', $salt ) : $salt;
$cache = wp_cache_get( $cache_key, $group );
if ( ! is_array( $cache ) ) {
return false;
}
if ( ! isset( $cache['salt'] ) || ! isset( $cache['data'] ) || $salt !== $cache['salt'] ) {
return false;
}
return $cache['data'];
}
Changelog
| Version | Description |
|---|---|
| 6.9.0 | Introduced. |