使用接口
接口 | 接口能力 |
---|---|
storageStatistics.getCurrentBundleStats | 获取当前应用的存储空间大小 (包含缓存文件,安装文件等) |
statvfs.getFreeSize | 查询设备剩余可用空间 |
statvfs.getTotalSize | 获取设备总空间 (排除系统占用空间) |
场景一:计算应用缓存大小,并进行清理
1.可以通过storageStatistics.getCurrentBundleStats接口获取缓存大小。
应用的缓存文件因为级别和加密类型不同,会保存在以下目录中。例如:app级别,加密类型为el1的缓存会保存到/data/storage/el1/base/cache目录下。
/data/storage/el1/base/cache
/data/storage/el1/base/haps/entry/cache
/data/storage/el2/base/cache
/data/storage/el2/base/haps/entry/cache
这四个目录可以通过以下接口获取,其中el1与el2因为分区不同,需要切换加密等级才能获取到:
let moduleContext: common.Context;
moduleContext = this.context.createModuleContext('entry');
console.log('moduleContext + el2: '+moduleContext.cacheDir);
console.log('UIAbilityContext + el2: '+this.context.cacheDir);
moduleContext.area = contextConstant.AreaMode.EL1;
console.log('moduleContext + el1: '+moduleContext.cacheDir);
this.context.area = contextConstant.AreaMode.EL1;
console.log('UIAbilityContext + el1: '+this.context.cacheDir);
storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
if (err) {
console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Invoke getCurrentBundleStats succeeded, cacheSize is ${bundleStats.cacheSize}`);
}
});
2.清除缓存,缓存会保存在上述四个文件夹中,可以递归删除
webview缓存(/data/storage/el2/base/cache/web/Cache)包含于在上述文件夹中,也可以调用[WebviewController.removeCache]单独清理web缓存。
//通过Context.cacheDir获取所有缓存路径
let cacheDir : string[] =[