当前位置: 首页 > news >正文

鸿蒙清楚本地缓存

一.清除的话是需要清除4个文件夹的,如果没有清干净的话就是没有4个全清

·1.获取路径,并调用清除方法
  async getPath() {// 获取原始密级let area = this.context.area;let appArea = this.context.getApplicationContext().area// 暂时替换密级this.context.area = contextConstant.AreaMode.EL2;this.context.getApplicationContext().area = 1let cacheDir01 = ''let cacheDir02 = getContext(this).cacheDirif (this.context.area === contextConstant.AreaMode.EL2) {cacheDir01 = cacheDir02.replace('el2', 'el1')} else if (this.context.area === contextConstant.AreaMode.EL1) {cacheDir01 = cacheDir02.replace('el1', 'el2')}let applicationContext = this.context.getApplicationContext(); // 获取应用上下文let cacheDirEL1 = ''let cacheDirEL2 = applicationContext.cacheDir;if (applicationContext.area === 1) { // 沙箱路径为1代表el2,0代表el1cacheDirEL1 = cacheDirEL2.replace('el2', 'el1')} else if (applicationContext.area === 0) {cacheDirEL1 = cacheDirEL2.replace('el1', 'el2')}console.log('cacheDirEL1', cacheDirEL1)console.log('cacheDirEL2', cacheDirEL2)// 还原密级this.context.area = area;this.context.getApplicationContext().area = appAreaawait fileIo.access(cacheDir01, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDir01)} else {console.info('cacheDir01', 'file not exists');}}});await fileIo.access(cacheDir02, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDir02)} else {console.info('cacheDir02', "file not exists");}}});await fileIo.access(cacheDirEL1, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDirEL1)} else {console.info('cacheDirEL1', "file not exists");}}});await fileIo.access(cacheDirEL2, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDirEL2)} else {console.info('cacheDirEL2', "file not exists");}}});await this.getCache()promptAction.showToast({message: '清除缓存成功',duration: 2000,})}
2.清除方法
 // 清理缓存clearCache(filepath: string) {fileIo.listFile(filepath).then((filenames) => {for (let i = 0; i < filenames.length; i++) {let dirPath = filepath + '/' + filenames[i];console.log('dirPath', dirPath);// 判断是否为文件夹let isDirectory: boolean = false;try {isDirectory = fileIo.statSync(dirPath).isDirectory();} catch (e) {console.error(JSON.stringify(e));}if (isDirectory) {fileIo.rmdirSync(dirPath);} else {fileIo.unlink(dirPath).then(() => {console.info('remove file succeed');}).catch((err: Error) => {console.error('remove file failed with error message: ' + err.message);});}}})}
3.页面调用
  this.getPath()
4. 获取本地缓存

这里的单位转为M了

  getCache() {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, appsize is ${bundleStats.cacheSize}`);this.mb = (bundleStats.cacheSize / (1024 * 1024)).toFixed(2); // 保留两位小数// 重新创建数组来触发UI更新}});}

http://www.dtcms.com/a/353941.html

相关文章:

  • AI大语言模型助力:国自然/省级基金项目撰写(如何高效准备申请材料?如何精准把握评审标准?从申请者和评审者的不同角度全解
  • 【单例模式】
  • CUDA的编译与调试
  • Mac 上录制视频有几种常见方式
  • 基于springboot的校园资料分享平台(源码+论文+PPT答辩)
  • 网络安全监控中心
  • 【笔记】Windows 安装 Triton 的工作记录(之二)
  • IDR的RWA金融逻辑RWA:全球金融革命的底层协议
  • 数学建模——马尔科夫链(Markov Chain Model)
  • 集成学习之 Stacking(堆叠集成)
  • django配置多个app使用同一个static静态文件目录
  • 使用openCV(C ++ / Python)的Alpha混合
  • 【高级机器学习】 2. Loss Functions(损失函数)
  • 一、快速掌握Python 中的文件操作知识体系
  • mysql zip包安装步骤
  • 2025(秋)中国国际健康产业(成都)博览会:探索健康未来辉煌
  • TCP 并发服务器构建
  • 场外期权能做套利吗?
  • 二叉树的工程实践与高频问题(续):从LeetCode真题到系统设计的深度剖析
  • centos7 安装指定版本的fastfds
  • 了解CDC(变更数据捕获)如何革新数据集成方式
  • Linux 系统调优工具与实践指南
  • 个人博客系统系统---测试报告
  • HarmonyOS布局实战:用声明式UI构建自适应电商卡片
  • 【源码分析】@vue/runtime-dom/src/apiCustomElement.ts 解析
  • 重磅升级,Pixso 2.0赋能HarmonyOS应用设计与开发
  • 安卓11 12系统修改定制化_____如何修改固件 实现给指定内置的应用无障碍权限
  • Sybase 安装与备份
  • 【c++】超好玩游戏
  • 一、CSS3 新增选择器(非 “属性”,但为核心基础)