HarmonyOS 应用缓存清理不彻底

https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-local-file-manager-12-V5

通过以上链接清理缓存后,缓存数据不够彻底,依然还会存在部分缓存

HarmonyOS
2024-12-25 16:32:10
650浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

请参考示例如下:

import { fileIo, storageStatistics } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import common from '@ohos.app.ability.common';
import contextConstant from '@ohos.app.ability.contextConstant';

@Entry
@Component
struct ClearCache {
  private context = getContext(this) as common.UIAbilityContext;

  // 获取应用数据空间大小
  getCache() {
    storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
      if (error) {
        console.error('getCurrentBundleStats failed with error:' + JSON.stringify(error));
      } else {
        console.info('getCurrentBundleStats successfully:' + JSON.stringify(bundleStats));
        console.info('appsize :' + bundleStats.appSize);
        console.info('cacheSize :' + bundleStats.cacheSize);
        console.info('dataSize :' + bundleStats.dataSize);
      }
    });
  }

  // 清理缓存
  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);
          });
        }
      }

    })
  }

  // 获取缓存路径
  getPath() {
    // 获取原始密级
    let area = this.context.area;
    console.log('area', area)
    let appArea = this.context.getApplicationContext().area
    console.log('appArea', appArea)

    // 暂时替换密级
    this.context.area = contextConstant.AreaMode.EL2;
    this.context.getApplicationContext().area = 1

    let cacheDir01 = ''
    let cacheDir02 = getContext(this).cacheDir
    if (this.context.area === contextConstant.AreaMode.EL2) {
      cacheDir01 = cacheDir02.replace('el2', 'el1')
    } else if (this.context.area === contextConstant.AreaMode.EL1){
      cacheDir01 = cacheDir02.replace('el1', 'el2')
    }
    console.log('cacheDir01', cacheDir01)
    console.log('cacheDir02', cacheDir02)

    let applicationContext = this.context.getApplicationContext(); // 获取应用上下文
    let cacheDirEL1 = ''
    let cacheDirEL2 = applicationContext.cacheDir;
    if (applicationContext.area === 1) { // 沙箱路径为1代表el2,0代表el1
      cacheDirEL1 = 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 = appArea
    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');
        }
      }
    });
    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");
        }
      }
    });
    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");
        }
      }
    });
    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");
        }
      }
    });
  }

  build() {
    Column() {
      Button('获取系统缓存大小')
        .onClick(() => {
          this.getCache();
        })
      Button('点击清理缓存')
        .onClick(() => {
          this.getPath()
        })
    }
  }
}
  • 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.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
分享
微博
QQ
微信
回复
2024-12-25 17:52:58


相关问题
HarmonyOS 关于应用清理缓存
1152浏览 • 1回复 待解决
HarmonyOS 如何获取应用缓存清理
611浏览 • 1回复 待解决
HarmonyOS web 清理缓存
593浏览 • 1回复 待解决
HarmonyOS 清理缓存问题
655浏览 • 1回复 待解决
HarmonyOS web清理缓存
898浏览 • 1回复 待解决
HarmonyOS ArkWeb如何清理缓存
522浏览 • 1回复 待解决
如何获取缓存数据和清理缓存
1246浏览 • 1回复 待解决
HarmonyOS 如何清理 web 缓存
563浏览 • 1回复 待解决
HarmonyOS 缓存无法清理干净
857浏览 • 1回复 待解决
HarmonyOS 清理app缓存的方法
701浏览 • 1回复 待解决
DevEco有没有清理调试应用缓存的功能
5031浏览 • 1回复 待解决
HarmonyOS 如何清理web的缓存
438浏览 • 1回复 待解决
HarmonyOS 如何清理Image的磁盘缓存
841浏览 • 1回复 待解决
用file api清理缓存目录
1378浏览 • 1回复 待解决
HarmonyOS 如何彻底退出应用
914浏览 • 1回复 待解决
如何获取应用信息以及彻底退出APP
1418浏览 • 1回复 待解决
如何获取应用信息以及彻底退出APP
1162浏览 • 1回复 待解决