函数文档

wp_cache_replace()

💡 云策文档标注

概述

wp_cache_replace() 是 WordPress 中用于替换缓存内容的函数,仅当指定键的缓存已存在时才执行替换操作。它基于 WP_Object_Cache::replace() 方法实现。

关键要点

  • 函数用于替换缓存数据,要求原始缓存必须存在,否则返回 false。
  • 接受参数:$key(缓存键,必需)、$data(新数据,必需)、$group(缓存组,可选,默认为空)、$expire(过期时间,可选,默认为 0 表示永不过期)。
  • 返回布尔值:成功替换返回 true,原始值不存在返回 false。
  • 内部调用 WP_Object_Cache::replace() 方法,确保类型安全。

代码示例

function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
}

注意事项

  • 此函数自 WordPress 2.0.0 版本引入,是核心缓存 API 的一部分。
  • 替换操作依赖于 WP_Object_Cache 实例,确保在缓存环境中使用。

📄 原文内容

Replaces the contents of the cache with new data.

Description

See also

Parameters

$keyint|stringrequired
The key for the cache data that should be replaced.
$datamixedrequired
The new data to store in the cache.
$groupstringoptional
The group for the cache data that should be replaced.
Default empty.
$expireintoptional
When to expire the cache contents, in seconds.
Default 0 (no expiration).

Return

bool True if contents were replaced, false if original value does not exist.

Source

function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
}

Changelog

Version Description
2.0.0 Introduced.