HarmonyOS 如何使用app本地数据缓存以及清理缓存

如何使用app本地数据缓存以及清理缓存,是否有相关文档或者现成demo

HarmonyOS
2025-01-09 18:28:44
1.2w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

仅使用可以参考APPStorage localStorage. set get方法

查询缓存用storageStatistics.getCurrentBundleStats()接口,可以获取缓存空间大小。

清除文件缓存:

查询缓存用storageStatistics.getCurrentBundleStats()接口,清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存。

参考文档:

应用空间统计:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5

文件管理:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-fileio-V5

demo如下:

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

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  //获取缓存
  getCacheSize() {
    storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
      if (error) {
        console.error("getCurrentBundleStats failed with error:" + JSON.stringify(error));
      } else {
        let cacheSize = parseFloat((bundleStats.cacheSize / (1024*1024)).toFixed(1))
        console.info('cacheSize:' + cacheSize);
      }
    })
  }

  //清理缓存
  clearCache() {
    const context: Context = getContext(this);
    let cacheDir = context.cacheDir
    fs.listFile(cacheDir).then((filenames) => {
      for (let i = 0;i < filenames.length; i++) {
        let dirPath = cacheDir+filenames[i]
        try {
          // 判断是否文件夹
          let isDirectory = fs.statSync(dirPath).isDirectory()
          if (isDirectory) {
            fs.rmdirSync(dirPath)
          } else {
            fs.unlink(dirPath).then(() => {
              console.info('remove file succeed');
            }).catch((err: BusinessError) => {
              console.info("remove file failed with error message: " + err.message + ", error code: " + err.code);
            });
          }
        }catch (e) {
          console.log(e)
        }
      }
    })
  }

  build() {
    Column() {
      Text(this.message)
        .id('JSONParseModelPageHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('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.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
分享
微博
QQ
微信
回复
2025-01-09 20:45:23


相关问题
如何获取缓存数据清理缓存
947浏览 • 1回复 待解决
HarmonyOS 清理app缓存的方法
342浏览 • 1回复 待解决
HarmonyOS ArkWeb如何清理缓存
305浏览 • 1回复 待解决
HarmonyOS 如何清理 web 缓存
338浏览 • 1回复 待解决
HarmonyOS 清理缓存问题
385浏览 • 1回复 待解决
HarmonyOS web清理缓存
513浏览 • 1回复 待解决
HarmonyOS web 清理缓存
355浏览 • 1回复 待解决
HarmonyOS 如何清理web的缓存
239浏览 • 1回复 待解决
HarmonyOS 关于应用清理缓存
745浏览 • 1回复 待解决
HarmonyOS 缓存无法清理干净
575浏览 • 1回复 待解决
HarmonyOS 如何获取应用缓存清理
345浏览 • 1回复 待解决
HarmonyOS 如何清理Image的磁盘缓存
539浏览 • 1回复 待解决
HarmonyOS 应用缓存清理不彻底
369浏览 • 1回复 待解决
用file api清理缓存目录
1130浏览 • 1回复 待解决
HarmonyOS 本地缓存问题
717浏览 • 1回复 待解决
HarmonyOS 本地缓存问题?
758浏览 • 0回复 待解决