HarmonyOS 缓存无法清理干净
使用FAQ中清理缓存的方法,无法彻底清除缓存:
https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-local-file-manager-12-V5
这边创建了一个缓存管理类,按照文档获取和清除app缓存,但是无法彻底清理干净。
import { BusinessError } from '@ohos.base';
import { Logger } from '@ohos/httpclient';
import { fileIo, storageStatistics } from '@kit.CoreFileKit';
//缓存管理工具
export class TBXCacheManager {
  private static instance: TBXCacheManager;
  appCacheSize: number = 0
  private constructor() {
  }
  public static getInstance(): TBXCacheManager {
    if (!TBXCacheManager.instance) {
      TBXCacheManager.instance = new TBXCacheManager()
    }
    return TBXCacheManager.instance;
  }
  /*获取APP缓存打下MB*/
  async getAppCahceSize(): Promise<number> {
    const statistics: storageStatistics.BundleStats = await storageStatistics.getCurrentBundleStats()
    if (statistics) {
      this.appCacheSize = Math.round(statistics.cacheSize/(1024*1024.0) * 10) /10
      console.debug('appCacheSize:',this.appCacheSize)
    }
    return this.appCacheSize
  }
  /*清除APP所有缓存信息*/
  async removeAppAllCache(context: Context) {
    let cacheDir = context.cacheDir;
    console.debug('cacheDic:', cacheDir)
    fileIo.rmdir(cacheDir, (err: BusinessError) => {
      if (err) {
        Logger.debug('remove app cache faild' + err.message)
      } else {
        console.debug('clear success')
        this.getAppCahceSize()
        fileIo.listFile(cacheDir).then((filenames) => {
          console.debug('fileNames:',filenames.length)
          for (let i = 0; i < filenames.length; i++) {
            let dirPath = cacheDir + filenames[i];
            // 判断是否为文件夹
            let isDirectory: boolean = false;
            try {
              isDirectory = fileIo.statSync(dirPath).isDirectory();
            }
            catch (e) {
              console.log(e);
            }
            if (isDirectory) {
              fileIo.rmdirSync(dirPath);
            } else {
              fileIo.unlink(dirPath).then(() => {
                console.info('remove file succeed');
              }).catch((err: Error) => {
                console.info('remove file failed with error message: ' + err.message);
              });
            }
          }
        })
      }
    })
  }
}
        HarmonyOS
      
        赞
        
 收藏 0
 回答 1
 
        待解决
        
相关问题
 HarmonyOS 获取缓存大小,删除缓存,删除不干净的问题 
1199浏览  • 1回复 待解决
HarmonyOS  web 清理缓存 
985浏览  • 1回复 待解决
HarmonyOS  web清理缓存 
1418浏览  • 1回复 待解决
HarmonyOS 清理缓存问题 
936浏览  • 1回复 待解决
HarmonyOS 如何清理 web 缓存 
870浏览  • 1回复 待解决
HarmonyOS  如何清理web的缓存 
667浏览  • 1回复 待解决
HarmonyOS ArkWeb如何清理缓存 
926浏览  • 1回复 待解决
HarmonyOS 关于应用清理缓存 
3166浏览  • 1回复 待解决
HarmonyOS  如何获取app的缓存大小,如何清理缓存? 
2084浏览  • 1回复 待解决
如何获取缓存数据和清理缓存? 
1696浏览  • 1回复 待解决
用file api清理缓存目录 
1790浏览  • 1回复 待解决
HarmonyOS 清理app缓存的方法 
1381浏览  • 1回复 待解决
HarmonyOS 应用缓存清理不彻底 
1058浏览  • 1回复 待解决
HarmonyOS 清理缓存,有没有相关清理缓存一套逻辑和相关代码呢 
1444浏览  • 1回复 待解决
HarmonyOS 官方的检查缓存和清理缓存的API 
1590浏览  • 1回复 待解决
HarmonyOS 如何使用app本地数据缓存以及清理缓存 
2912浏览  • 1回复 待解决
HarmonyOS 如何清理Image的磁盘缓存 
1361浏览  • 1回复 待解决
HarmonyOS 如何获取应用缓存并清理 
1162浏览  • 1回复 待解决
HarmonyOS 通过步骤2获取应用缓存目录,删除目录,通过步骤1获取缓存还是有一些字节内存,无法彻底删除干净 
1105浏览  • 1回复 待解决
DevEco有没有清理调试应用缓存的功能 
5542浏览  • 1回复 待解决
iimsimsiHarmonyOS  获取和清理缓存的API方法 
1185浏览  • 1回复 待解决
怎么获取应用已使用的缓存大小,如何使用api清理缓存 
3103浏览  • 2回复 待解决
HarmonyOS app中清理缓存的功能,包括Web的缓存,一般都需要清理哪些文件夹下的缓存,有没有对应的示例代码? 
1068浏览  • 1回复 待解决
HarmonyOS 有没有相关清理缓存一套逻辑和相关代码呢 
1853浏览  • 1回复 待解决
#鸿蒙通关秘籍#鸿蒙项目中如何分析是否需要清理RenderGroup缓存? 
1514浏览  • 2回复 待解决





















示例是提供了一个删除缓存文件的方法,具体清缓存要到什么程度,由应用自己控制这里是沙箱文件的介绍
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-context-stage-V5#%E8%8E%B7%E5%8F%96%E5%BA%94%E7%94%A8%E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84