HarmonyOS 清除缓存不起作用

我们的项目大多是js前端功能,有一些进入后的选择功能,选择后我想后期重置修改,

let applicationContext = context.getApplicationContext();
    let cacheDir = applicationContext.cacheDir;
    fs.rmdirSync(cacheDir)
  • 1.
  • 2.
  • 3.

使用这个清除不行

后面我加了其他一些清楚

let preferencesDir = applicationContext.preferencesDir;
    let tempDir = applicationContext.tempDir;
    let filesDir = applicationContext.filesDir;

    fs.rmdirSync(preferencesDir)
    fs.rmdirSync(tempDir)
    fs.rmdirSync(filesDir)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

也都不好使。

后来我找了个HDC相关的命令

hdc shell bm clean xxx.xxx.xxx -c 也是不行的 。

HarmonyOS
2024-12-27 15:51:06
826浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

查询缓存用storageStatistics.getCurrentBundleStats()接口,当前没有api可以直接清除缓存,可以使用fs.rmdirSync清除应用缓存,提供一个清除缓存的demo,可以参考一下:

import { BusinessError } from '@ohos.base';
import { storageStatistics } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';
import { common } from '@kit.AbilityKit';

@Entry
@Component
struct CacheSizePage {
  @State message: string = 'CacheSize';
  private index:number = 0;
  private fileBuf:Uint32Array = new Uint32Array(1024)
  @State statStr:string = ''
  aboutToAppear(): void {
    for (let i:number = 0; i< 1024; i++) {
      this.fileBuf[i] = i;
    }
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
        Button("Click to add 1K Cache file")
          .fontSize(20)
          .onClick(()=>{
            let context = getContext(this) as common.UIAbilityContext;
            let file = fs.openSync(context.cacheDir + `/${this.index}.txt`, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
            let writeLen = fs.writeSync(file.fd, this.fileBuf.buffer);
            console.info(`tag: add file ${this.index}`);
            this.index++;
          })
        Button("Click to clear cache")
          .fontSize(20)
          .onClick(()=>{
            let context = getContext(this) as common.UIAbilityContext;
            fs.rmdirSync(context.cacheDir);
            console.info('tag: rm cache dir');
            this.index = 0;
          })
        Button("Click to get Stat")
          .onClick(()=>{
            storageStatistics.getCurrentBundleStats().then((BundleStats: storageStatistics.BundleStats) => {
              console.info("tag: getCurrentBundleStats successfully:" + JSON.stringify(BundleStats));
              this.statStr = JSON.stringify(BundleStats)
            }).catch((err: BusinessError) => {
              console.error("tag: getCurrentBundleStats failed with error:"+ JSON.stringify(err));
            });
          })
        Text(this.statStr).fontSize(30).fontColor(Color.Red)
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-27 18:51:37


相关问题
HarmonyOS zIndex不起作用
693浏览 • 1回复 待解决
Preferences不起作用
4851浏览 • 1回复 待解决
HarmonyOS异步await不起作用
982浏览 • 1回复 待解决
HarmonyOS router.back不起作用
663浏览 • 1回复 待解决
HarmonyOS column设置圆角不起作用
1480浏览 • 1回复 待解决
HarmonyOS bindSheet设置preferType不起作用
426浏览 • 1回复 待解决
HarmonyOS TextAlign.JUSTIFY 不起作用
968浏览 • 1回复 待解决
hilog的private参数不起作用
6499浏览 • 1回复 待解决
HarmonyOS data_preferences remove不起作用
949浏览 • 1回复 待解决
设置状态栏颜色不起作用怎么回事?
2971浏览 • 1回复 待解决
,配置自动签名不起作用
1502浏览 • 1回复 待解决
HarmonyOS 清除Web缓存
1393浏览 • 0回复 待解决
HarmonyOS 清除app缓存
689浏览 • 1回复 待解决