函数文档

wp_cache_get_multiple_salted()

💡 云策文档标注

概述

wp_cache_get_multiple_salted() 函数用于从缓存中检索多个项目,仅考虑有效且未更改的项目。它通过比较时间戳(salt)来验证缓存项的新鲜度,返回关联数组,其中无效或过期的项标记为 false。

关键要点

  • 函数参数包括 $cache_keys(缓存键数组)、$group(缓存组)和 $salt(时间戳或时间戳数组)。
  • 返回值为关联数组,包含缓存值;若项目未找到或过期,则对应值为 false。
  • 内部调用 wp_cache_get_multiple() 获取缓存,然后检查每个值的 salt 和 data 字段是否匹配。
  • 此函数在 WordPress 6.9.0 版本中引入。

代码示例

function wp_cache_get_multiple_salted( $cache_keys, $group, $salt ) {
	$salt  = is_array( $salt ) ? implode( ':', $salt ) : $salt;
	$cache = wp_cache_get_multiple( $cache_keys, $group );

	foreach ( $cache as $key => $value ) {
		if ( ! is_array( $value ) ) {
			$cache[ $key ] = false;
			continue;
		}
		if ( ! isset( $value['salt'], $value['data'] ) || $salt !== $value['salt'] ) {
			$cache[ $key ] = false;
			continue;
		}
		$cache[ $key ] = $value['data'];
	}

	return $cache;
}

注意事项

  • 确保 $salt 参数正确传递,可以是单个时间戳或时间戳数组,用于验证缓存组的上次更新时间。
  • 缓存项必须包含 'salt' 和 'data' 字段,否则将被视为无效并返回 false。
  • 此函数依赖于 wp_cache_get_multiple(),适用于批量检索缓存场景。

📄 原文内容

Retrieves multiple items from the cache, only considering valid and unchanged items.

Parameters

$cache_keysarrayrequired
Array of cache keys to retrieve.
$groupstringrequired
The group of the cache to check.
$saltstring|string[]required
The timestamp (or multiple timestamps if an array) indicating when the cache group(s) were last updated.

Return

array An associative array containing cache values. Values are false if they are not found or outdated.

Source

function wp_cache_get_multiple_salted( $cache_keys, $group, $salt ) {
	$salt  = is_array( $salt ) ? implode( ':', $salt ) : $salt;
	$cache = wp_cache_get_multiple( $cache_keys, $group );

	foreach ( $cache as $key => $value ) {
		if ( ! is_array( $value ) ) {
			$cache[ $key ] = false;
			continue;
		}
		if ( ! isset( $value['salt'], $value['data'] ) || $salt !== $value['salt'] ) {
			$cache[ $key ] = false;
			continue;
		}
		$cache[ $key ] = $value['data'];
	}

	return $cache;
}

Changelog

Version Description
6.9.0 Introduced.