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

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

HarmonyOS
9天前
浏览
收藏 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); 
          }); 
        } 
      } 
    }) 
  } 
}

如果想直接删除缓存目录,则尝试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
微信
回复
9天前
相关问题
HarmonyOS 清除Web缓存
241浏览 • 0回复 待解决
HarmonyOS怎么清除当前应用缓存
233浏览 • 1回复 待解决
HarmonyOS Web如何实现清除缓存
162浏览 • 1回复 待解决
有没有调用日历接口?
6033浏览 • 1回复 待解决
鸿蒙-有没有缓存工具类
5692浏览 • 1回复 待解决
有没有获取appid接口
602浏览 • 1回复 待解决
DevEco有没有清理调试应用缓存功能
3568浏览 • 1回复 待解决
HarmonyOS 有没有方法直接退出APP
166浏览 • 1回复 待解决
有没有接口能获取到组件宽度
471浏览 • 1回复 待解决
如何通过命令号清除应用缓存
1721浏览 • 1回复 待解决
鸿蒙系统有没有调用锁屏接口
6677浏览 • 1回复 待解决