clean_dirsize_cache()
云策文档标注
概述
clean_dirsize_cache() 函数用于清理由 recurse_dirsize() 使用的目录大小缓存。它从 dirsize_cache 瞬态中移除指定路径及其所有父目录的缓存条目。
关键要点
- 函数接受一个非空字符串路径参数,用于指定要清理的目录或文件路径。
- 如果路径无效或为空,会触发错误并返回,不执行清理操作。
- 清理过程包括移除指定路径及其所有父目录在 dirsize_cache 瞬态中的条目。
- 缓存过期时间根据是否使用外部对象缓存设置:外部缓存时为 0,否则为 10 年。
代码示例
function clean_dirsize_cache( $path ) {
if ( ! is_string( $path ) || empty( $path ) ) {
wp_trigger_error(
'',
sprintf(
__( '%1$s only accepts a non-empty path string, received %2$s.' ),
'clean_dirsize_cache()',
'' . gettype( $path ) . ''
)
);
return;
}
$directory_cache = get_transient( 'dirsize_cache' );
if ( empty( $directory_cache ) ) {
return;
}
$expiration = ( wp_using_ext_object_cache() ) ? 0 : 10 * YEAR_IN_SECONDS;
if (
! str_contains( $path, '/' ) &&
! str_contains( $path, '' )
) {
unset( $directory_cache[ $path ] );
set_transient( 'dirsize_cache', $directory_cache, $expiration );
return;
}
$last_path = null;
$path = untrailingslashit( $path );
unset( $directory_cache[ $path ] );
while (
$last_path !== $path &&
DIRECTORY_SEPARATOR !== $path &&
'.' !== $path &&
'..' !== $path
) {
$last_path = $path;
$path = dirname( $path );
unset( $directory_cache[ $path ] );
}
set_transient( 'dirsize_cache', $directory_cache, $expiration );
}注意事项
- 该函数主要用于处理上传、删除附件等操作时的缓存清理,确保目录大小信息准确。
- 在 WordPress 5.9.0 版本中增加了输入验证,无效输入会触发错误通知。
- 函数内部使用了多个 WordPress 核心函数,如 get_transient()、set_transient() 和 untrailingslashit()。
原文内容
Cleans directory size cache used by recurse_dirsize() .
Description
Removes the current directory and all parent directories from the dirsize_cache transient.
Parameters
$pathstringrequired-
Full path of a directory or file.
Source
function clean_dirsize_cache( $path ) {
if ( ! is_string( $path ) || empty( $path ) ) {
wp_trigger_error(
'',
sprintf(
/* translators: 1: Function name, 2: A variable type, like "boolean" or "integer". */
__( '%1$s only accepts a non-empty path string, received %2$s.' ),
'<code>clean_dirsize_cache()</code>',
'<code>' . gettype( $path ) . '</code>'
)
);
return;
}
$directory_cache = get_transient( 'dirsize_cache' );
if ( empty( $directory_cache ) ) {
return;
}
$expiration = ( wp_using_ext_object_cache() ) ? 0 : 10 * YEAR_IN_SECONDS;
if (
! str_contains( $path, '/' ) &&
! str_contains( $path, '\' )
) {
unset( $directory_cache[ $path ] );
set_transient( 'dirsize_cache', $directory_cache, $expiration );
return;
}
$last_path = null;
$path = untrailingslashit( $path );
unset( $directory_cache[ $path ] );
while (
$last_path !== $path &&
DIRECTORY_SEPARATOR !== $path &&
'.' !== $path &&
'..' !== $path
) {
$last_path = $path;
$path = dirname( $path );
unset( $directory_cache[ $path ] );
}
set_transient( 'dirsize_cache', $directory_cache, $expiration );
}
Skip to note 2 content
cln.lgr
https://wordpress.org/support/topic/iis-wordpress-5-6-multi-sites-file-upload-hang/