ITPub博客

首页 > IT基础架构 > 服务器/存储 > 如何解决应用缓存未完全清除的问题?

如何解决应用缓存未完全清除的问题?

服务器/存储 作者:zhmw 时间:2026-09-16 10:11:20 0 删除 编辑
HarmonyOS

如何解决应用缓存未完全清除的问题?

阅读原文:如何解决应用缓存未完全清除的问题?

需求描述

使用 context.cacheDir 获取并清除应用缓存时,缓存并未完全移除。调用 storageStatistics.getCurrentBundleStats 检查缓存大小后,结果仍显示缓存大小不为零。

背景知识

应用缓存是指应用运行过程中产生的临时数据,如图片、视频和文档。这些数据有助于提高响应速度并提供离线能力,但过多的缓存会占用大量存储空间。

@ohos.file.storageStatistics 模块提供了查询存储空间使用情况的功能,包括:

  • 内部和外部存储查询

  • 应用特定数据的分类和使用统计

在鸿蒙系统中,应用缓存文件分布在四个目录中:

/
data
/
storage
/
el1
/
base
/
cache
/
data
/
storage
/
el1
/
base
/
haps
/
entry
/
cache
/
data
/
storage
/
el2
/
base
/
cache
/
data
/
storage
/
el2
/
base
/
haps
/
entry
/
cache
image.png

仅使用 context.cacheDir 会清除一个目录(/data/storage/el2/base/haps/entry/cache),而其他位置会残留缓存。

实现步骤

  • 获取所有四个缓存目录的路径(EL1 和 EL2 级别)。

  • 将缓存目录路径存储到数组中以便迭代。

  • 遍历数组并删除每个路径中的所有文件和子目录,以确保完全清除缓存。

代码片段 / 配置

步骤 1:获取所有缓存目录路径

private
paths
:
Array
<
string
>
=
[];
private
moduleContext
:
common
.
Context
|
undefined
=
undefined
;
private
context
:
Context
|
undefined
=
undefined
;
async
aboutToAppear
():
Promise
<
void
>
{
this
.
context
=
this
.
getUIContext
().
getHostContext
()
!
;
this
.
moduleContext
=
await
application
.
createModuleContext
(
this
.
context
,
'
entry
'
);
console
.
info
(
`moduleContext + el2:
${
this
.
moduleContext
.
cacheDir
}
`
);
// /data/storage/el2/base/cache
console
.
info
(
`UIAbilityContext + el2:
${
this
.
context
.
cacheDir
}
`
);
// /data/storage/el2/base/haps/entry/cache
this
.
paths
.
push
(
this
.
moduleContext
.
cacheDir
);
this
.
paths
.
push
(
this
.
context
.
cacheDir
);
this
.
moduleContext
.
area
=
contextConstant
.
AreaMode
.
EL1
;
console
.
info
(
`moduleContext + el1:
${
this
.
moduleContext
.
cacheDir
}
`
);
// /data/storage/el1/base/cache
this
.
context
.
area
=
contextConstant
.
AreaMode
.
EL1
;
console
.
info
(
`UIAbilityContext + el1:
${
this
.
context
.
cacheDir
}
`
);
// /data/storage/el1/base/haps/entry/cache
this
.
paths
.
push
(
this
.
moduleContext
.
cacheDir
);
this
.
paths
.
push
(
this
.
context
.
cacheDir
);
}

步骤 2:删除所有缓存目录中的文件

Button
(
'
Clear Cache
'
).
onClick
(()
=>
{
for
(
let
i
=
0
;
i
<
this
.
paths
.
length
;
i
++
)
{
let
path
=
this
.
paths
[
i
];
fileIo
.
listFile
(
path
).
then
((
filenames
)
=>
{
for
(
let
i
=
0
;
i
<
filenames
.
length
;
i
++
)
{
let
dirPath
=
path
+
'
/
'
+
filenames
[
i
];
console
.
info
(
dirPath
);
let
isDirectory
:
boolean
=
false
;
try
{
isDirectory
=
fileIo
.
statSync
(
dirPath
).
isDirectory
();
}
catch
(
err
)
{
console
.
error
(
`Failed to check directory:
${
err
.
message
}
`
);
}
if
(
isDirectory
)
{
fileIo
.
rmdirSync
(
dirPath
);
}
else
{
fileIo
.
unlink
(
dirPath
).
then
(()
=>
{
console
.
info
(
'
File removed successfully
'
);
}).
catch
((
err
:
Error
)
=>
{
console
.
error
(
`Failed to remove file:
${
err
.
message
}
`
);
});
}
}
});
}
});

测试结果

执行上述代码后,所有缓存目录均被成功清除。调用 storageStatistics.getCurrentBundleStats 返回的 cacheSize 值为 0,确认缓存已完全删除。

限制或注意事项

  • 自 API(应用程序编程接口) 版本 19 Release 起支持。

  • 需要 HarmonyOS 5.1.1 Release SDK(软件开发工具包) 或更高版本。

  • 必须使用 DevEco Studio 5.1.1 Release 或更高版本进行编译和执行。

  • 应用应根据自身需求决定缓存清理的级别。

相关文档或链接

https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-file-storage-statistics#storagestatisticsgetcurrentbundlestats9

https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-file-storage-statistics

https://developer.huawei.com/consumer/en/doc/harmonyos-faqs/faqs-local-file-manager-12

作者:Emine Inan


转载声明:本文为转载整理,原文版权归原作者及原站点所有。

原文来源:DEV HarmonyOS Feed

原创地址:https://dev.to/harmonyos/how-to-resolve-the-issue-of-application-cache-not-being-completely-cleared-1ngo

原文作者:HarmonyOS;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2018-12-04

  • 博文量
    295
  • 访问量
    9529