HarmonyOS 应用缓存清理不彻底

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

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

HarmonyOS
22h前
浏览
收藏 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()
        })
    }
  }
}
分享
微博
QQ
微信
回复
21h前
相关问题
HarmonyOS 关于应用清理缓存
75浏览 • 1回复 待解决
HarmonyOS 如何获取应用缓存清理
48浏览 • 1回复 待解决
HarmonyOS web清理缓存
3浏览 • 0回复 待解决
HarmonyOS web 清理缓存
7浏览 • 0回复 待解决
HarmonyOS ArkWeb如何清理缓存
67浏览 • 1回复 待解决
如何获取缓存数据和清理缓存
408浏览 • 1回复 待解决
HarmonyOS 缓存无法清理干净
39浏览 • 1回复 待解决
HarmonyOS 如何清理 web 缓存
90浏览 • 1回复 待解决
DevEco有没有清理调试应用缓存的功能
4021浏览 • 1回复 待解决
HarmonyOS 清理app缓存的方法
42浏览 • 1回复 待解决
HarmonyOS 如何彻底退出应用
83浏览 • 1回复 待解决
HarmonyOS 如何清理web的缓存
3浏览 • 0回复 待解决
HarmonyOS 如何清理Image的磁盘缓存
0浏览 • 0回复 待解决
用file api清理缓存目录
843浏览 • 1回复 待解决
如何获取应用信息以及彻底退出APP
517浏览 • 1回复 待解决
如何获取应用信息以及彻底退出APP
410浏览 • 1回复 待解决