wp_using_ext_object_cache()
云策文档标注
概述
wp_using_ext_object_cache() 函数用于切换 $_wp_using_ext_object_cache 全局变量的状态,避免直接操作全局变量。它通过参数控制外部对象缓存的使用设置,并返回当前状态。
关键要点
- 函数 wp_using_ext_object_cache($using = null) 接受一个可选布尔参数 $using,用于设置外部对象缓存是否被使用,默认值为 null。
- 当 $using 不为 null 时,函数更新 $_wp_using_ext_object_cache 全局变量为指定值;否则,返回当前设置。
- 返回值为当前 'using' 设置,类型为 bool 或 null,因为全局变量可能为 null。
- 此函数在 WordPress 3.7.0 版本中引入,常用于管理对象缓存相关操作。
代码示例
function wp_using_ext_object_cache( $using = null ) {
global $_wp_using_ext_object_cache;
$current_using = $_wp_using_ext_object_cache;
if ( null !== $using ) {
$_wp_using_ext_object_cache = $using;
}
return $current_using;
}注意事项
- 全局变量 $_wp_using_ext_object_cache 可能为 null,因此返回值类型包括 bool 和 null,开发者需注意处理可能的 null 值。
- 此函数被多个核心功能使用,如 WP_Site_Health、WP_Query 和 transient 相关函数,确保在开发插件或主题时兼容其行为。
原文内容
Toggles $_wp_using_ext_object_cache on and off without directly touching global.
Parameters
$usingbooloptional-
Whether external object cache is being used.
Default:
null
Source
function wp_using_ext_object_cache( $using = null ) {
global $_wp_using_ext_object_cache;
$current_using = $_wp_using_ext_object_cache;
if ( null !== $using ) {
$_wp_using_ext_object_cache = $using;
}
return $current_using;
}
Changelog
| Version | Description |
|---|---|
| 3.7.0 | Introduced. |
Skip to note 2 content
Ronald Huereca
Global `global $_wp_using_ext_object_cache;` can return null, so the return type is actually `bool` and `null`.