函数文档

wp_suspend_cache_addition()

💡 云策文档标注

概述

wp_suspend_cache_addition() 函数用于临时暂停向缓存中添加数据,同时仍允许从缓存中检索数据。这适用于如数据导入等场景,以避免大量无用数据被添加到缓存中。暂停状态最多持续一个页面加载周期。

关键要点

  • 函数用于控制缓存添加的暂停和恢复,不影响缓存检索。
  • 参数 $suspend 为布尔值:true 暂停添加,false 恢复添加;默认 null 不改变当前设置。
  • 返回当前暂停设置的布尔值。
  • 暂停状态是静态的,基于页面加载,需手动调用以提前恢复。

代码示例

function wp_suspend_cache_addition( $suspend = null ) {
	static $_suspend = false;

	if ( is_bool( $suspend ) ) {
		$_suspend = $suspend;
	}

	return $_suspend;
}

注意事项

  • 暂停状态最多持续一个页面加载,如需提前恢复,需再次调用函数。
  • 常用于避免在批量操作(如导入)时缓存被无用数据填充。

📄 原文内容

Temporarily suspends cache additions.

Description

Stops more data being added to the cache, but still allows cache retrieval.
This is useful for actions, such as imports, when a lot of data would otherwise be almost uselessly added to the cache.

Suspension lasts for a single page load at most. Remember to call this function again if you wish to re-enable cache adds earlier.

Parameters

$suspendbooloptional
Suspends additions if true, re-enables them if false.
Defaults to not changing the current setting.

Default:null

Return

bool The current suspend setting.

Source

function wp_suspend_cache_addition( $suspend = null ) {
	static $_suspend = false;

	if ( is_bool( $suspend ) ) {
		$_suspend = $suspend;
	}

	return $_suspend;
}

Changelog

Version Description
3.3.0 Introduced.