HarmonyOS鸿蒙Next中检查缓存和清理缓存怎么做呢?包括清理首选项和web缓存

HarmonyOS鸿蒙Next中检查缓存和清理缓存怎么做呢?包括清理首选项和web缓存 有没有一键清除APP缓存的api呢?包括用户的首选项数据和web缓存数据,应该怎么清理?

4 回复

没有直接的API清理应用数据,但可以参考以下应用空间统计API:

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

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

可以参考以下案例,本地测试删除缓存后,getCurrentBundleStats返回正常。

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import storageStatistics from '@ohos.file.storageStatistics';
import fs from '@ohos.file.fs';
let context = this as common.UIAbilityContext;
let filesDir = context.cacheDir;
@Entry
@Component
struct Index {

  @State message: string = 'Hello World';
  // let context = getContext(this) as common.UIAbilityContext;
  aboutToAppear(): void {
    //fs清除缓存
    fs.rmdirSync(filesDir);
    //获取空间
    storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
      if (error) {
        console.error("getCurrentBundleStats failed with error:" + JSON.stringify(error));
      } else {
        // do something
        console.info("8899getCurrentBundleStats successfully:" + JSON.stringify(bundleStats));
      }
    });
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')

  }
}

清理文件demo如下:

import fs from '@ohos.file.fs';
let cacheDir = context.cacheDir;
@Entry
@Component
struct Clear_cache {
  clearCache() {
    // let cacheDir = getContext(this).cacheDir
    // fs.rmdirSync(cacheDir)
    // console.log(“delete !!!”)

    fs.listFiles(cacheDir).then((filenames) => {
      for (let i = 0;i < filenames.length; i++) {
        let dirPath = `${cacheDir}/${filenames[i]}`
        // 判断是否文件夹
        let isDirectory
        try {
          isDirectory = fs.statSync(dirPath).isDirectory()
        }
        catch (e) {
          console.log(e)
        }

        if (isDirectory) {
          fs.rmdirSync(dirPath)
        } else {
          fs.unlink(dirPath).then(() => {
            console.info('remove file succeed');
          }).catch((err) => {
            console.info("remove file failed with error message: " + err.message + ", error code: " + err.code);
          });
        }
      }

    })
  }
}

清除不了的数据可能包括首选项和web 首选项和Web组件里的缓存,

首选项清除参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-preferences-V5#clear

首选项清除操作中,根据自己的需要使用delete或者clear,

具体如下

delete:从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过flush将Preferences实例持久化,

使用callback异步回调

clear:清除缓存的Preferences实例中的所有数据,可通过flush将Preferences实例持久化,

使用callback异步回调

web清除缓存参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-cookie-and-data-storage-mgmt-V5

更多关于HarmonyOS鸿蒙Next中检查缓存和清理缓存怎么做呢?包括清理首选项和web缓存的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


既然能set get 存取数据,也有可能有类似removeAll

在HarmonyOS鸿蒙Next中,检查缓存和清理缓存可以通过以下步骤实现:

  1. 检查缓存

    • 打开“设置”应用。
    • 选择“应用管理”或“应用信息”。
    • 选择你要检查的应用,进入应用详情页。
    • 在应用详情页中,可以看到“存储”选项,点击后可以查看应用的缓存大小。
  2. 清理缓存

    • 在应用详情页的“存储”选项中,点击“清除缓存”按钮即可清理该应用的缓存。
  3. 清理首选项

    • 在应用详情页的“存储”选项中,点击“清除数据”按钮,这将清除应用的首选项和缓存。
    • 注意:清除数据会重置应用的所有设置和数据,使用时需谨慎。
  4. 清理Web缓存

    • 如果应用是浏览器或包含WebView组件,可以在应用内找到“设置”或“隐私”选项,选择“清除浏览数据”或“清除缓存”来清理Web缓存。
    • 或者,在系统设置中找到该应用,按照上述清理缓存的方法进行清理。

这些操作可以帮助你管理和优化设备存储空间。

在HarmonyOS鸿蒙Next中,检查与清理缓存可通过以下步骤实现:

  1. 清理应用缓存:进入“设置” > “应用管理”,选择目标应用,点击“存储”选项,选择“清除缓存”。
  2. 清理首选项:在“应用管理”中选择应用,点击“存储”后选择“清除数据”,这将同时清除首选项和缓存。
  3. 清理Web缓存:打开“浏览器”应用,进入“设置” > “隐私与安全”,选择“清除浏览数据”,勾选“缓存文件”后确认清除。

这些操作能有效释放存储空间,提升设备性能。

回到顶部