HarmonyOS APP有没有清除缓存的接口

APP有没有清除缓存的接口,就是像平常使用的app, 有个一键清理缓存的功能,清理APP的数据。

HarmonyOS
2024-09-09 12:18:14
1282浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

查询缓存用storageStatistics.getCurrentBundleStats()接口。清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存。

详细用法见下面的链接:

查询:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5

清理:

import fs from ‘@ohos.file.fs’; 
let cacheDir = context.cacheDir; 
@Entry 
@Component 
struct Clear_cache { 
  clearCache() { 
    fs.listFile(cacheDir).then((filenames) => { 
      for (let i = 0;i < filenames.length; i++) { 
        let dirPath = ${cacheDir}/${filenames[i]} 
        // 判断是否文件夹 
        let isDirectory 
        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”); 
          }).catch((err) => { 
            console.info("remove file failed with error message: " + err.message + ", error code: " + err.code); 
          }); 
        } 
      } 
    }) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

如果想直接删除缓存目录,则尝试fs.rmdir(getContext(this).cacheDir);请问清除不了的缓存,是否是首选项和Web组件里的缓存?如果是,首选项清除请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-preferences-V5#clear

首选项清除操作中,请根据自己的需要使用delete或者clear,具体如下:

delete:从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过flush将Preferences实例持久化,使用callback异步回调。

clear:清除缓存的Preferences实例中的所有数据,可通过flush将Preferences实例持久化,使用callback异步回调。

web清除缓存请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-cookie-and-data-storage-mgmt-V5

分享
微博
QQ
微信
回复
2024-09-09 16:29:36


相关问题
HarmonyOS 清除app缓存
685浏览 • 1回复 待解决
HarmonyOS 清除APP缓存问题
569浏览 • 1回复 待解决
HarmonyOS 有没有清除通知相关API
971浏览 • 1回复 待解决
HarmonyOS 清除Web缓存
1393浏览 • 0回复 待解决
HarmonyOS 清除缓存功能
1293浏览 • 1回复 待解决
HarmonyOS 关于获取缓存清除缓存
523浏览 • 1回复 待解决
鸿蒙-有没有缓存工具类
6847浏览 • 1回复 待解决
HarmonyOS 如何清除应用缓存
571浏览 • 1回复 待解决
HarmonyOS web清除缓存问题
892浏览 • 1回复 待解决
flutter缓存如何清除
941浏览 • 1回复 待解决
有没有调用日历接口?
7259浏览 • 1回复 待解决
HarmonyOS 如何清除Web缓存
614浏览 • 1回复 待解决
HarmonyOS web缓存清除localStorage
650浏览 • 1回复 待解决
HarmonyOS如何清除应用缓存
645浏览 • 0回复 待解决
HarmonyOS怎么清除当前应用缓存
2542浏览 • 1回复 待解决
有没有获取appid接口
1812浏览 • 1回复 待解决
HarmonyOS Web如何实现清除缓存
1365浏览 • 1回复 待解决
DevEco有没有清理调试应用缓存功能
5023浏览 • 1回复 待解决
HarmonyOS 清除缓存不起作用
600浏览 • 1回复 待解决