函数文档

wp_cache_set_salted()

💡 云策文档标注

概述

wp_cache_set_salted() 函数用于将带有盐值的数据存储到缓存中,通过结合数据与盐值来增强缓存的有效性管理。它内部调用 wp_cache_set() 实现存储,适用于需要基于时间戳或其他标识符更新缓存的场景。

关键要点

  • 函数签名:wp_cache_set_salted( $cache_key, $data, $group, $salt, $expire = 0 )
  • 参数说明:$cache_key(缓存键)、$data(缓存数据)、$group(缓存组)、$salt(盐值,可为字符串或数组,表示时间戳)、$expire(过期时间,秒,默认0表示不过期)
  • 返回值:成功返回 true,失败返回 false
  • 内部实现:将 $salt 转换为字符串(数组时用冒号分隔),并与 $data 组合为数组后调用 wp_cache_set()
  • 相关用途:被多个核心函数如 WP_Query::get_posts()、WP_User_Query::query() 等调用,用于缓存查询结果
  • 版本历史:自 WordPress 6.9.0 引入

代码示例

function wp_cache_set_salted( $cache_key, $data, $group, $salt, $expire = 0 ) {
	$salt = is_array( $salt ) ? implode( ':', $salt ) : $salt;
	return wp_cache_set(
		$cache_key,
		array(
			'data' => $data,
			'salt' => $salt,
		),
		$group,
		$expire
	);
}

📄 原文内容

Stores salted data in the cache.

Parameters

$cache_keystringrequired
The cache key under which to store the data.
$datamixedrequired
The data to be cached.
$groupstringrequired
The cache group to which the data belongs.
$saltstring|string[]required
The timestamp (or multiple timestamps if an array) indicating when the cache group(s) were last updated.
$expireintoptional
When to expire the cache contents, in seconds.
Default 0 (no expiration).

Return

bool True on success, false on failure.

Source

function wp_cache_set_salted( $cache_key, $data, $group, $salt, $expire = 0 ) {
	$salt = is_array( $salt ) ? implode( ':', $salt ) : $salt;
	return wp_cache_set(
		$cache_key,
		array(
			'data' => $data,
			'salt' => $salt,
		),
		$group,
		$expire
	);
}

Changelog

Version Description
6.9.0 Introduced.