查询缓存用storageStatistics.getCurrentBundleStats()接口,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5\#storagestatisticsgetcurrentbundlestats9清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存清理缓存demo:import fs from '@ohos.file.fs'; @Entry @Component struct Clear_cache { clearCache() { let cacheDir = getContext(this).cacheDir fs.listFile(cacheDir).then((filenames) => { for (let i = 0;i < filenames.length; i++) { let dirPath = `${cacheDir}/${filenames[i]}` // 判断是否文件夹 let isDirectory = false try { isDirectory = fs.statSync(dirPath).isDirectory() } catch (e) { console.log(e) } if (isDirectory) { fs.rmdirSync(dirPath) } else { fs.unlink(dirPath).then(() => { console.info('remove file succeed'); }) } } }) } }cacheSize包含以下几个目录,仅清理应用沙箱路径下的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获取这4个路径的方式如下:let moduleContext: common.Context; moduleContext = context.createModuleContext('entry'); ///data/storage/el2/base/cache let cachePathOne = moduleContext.cacheDir; ///data/storage/el2/base/haps/entry/cache let cachePathTwo = context.cacheDir; //切换加密等级 moduleContext.area = contextConstant.AreaMode.EL1; ///data/storage/el1/base/cache let cachePathThree = moduleContext.cacheDir; context.area = contextConstant.AreaMode.EL1; ///data/storage/el1/base/haps/entry/cache let cachePathFour = context.cacheDir; //切换回默认加密等级 moduleContext.area = contextConstant.AreaMode.EL2;
查询缓存用storageStatistics.getCurrentBundleStats()接口,参考链接:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5\#storagestatisticsgetcurrentbundlestats9
清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存
清理缓存demo:
cacheSize包含以下几个目录,仅清理应用沙箱路径下的cache还显示有大小是正常规格。
获取这4个路径的方式如下: