获取缓存大小与清除 Web 缓存 - 鸿蒙 HarmonyOS Next

针对浏览器 Web 组件清除缓存相关,具体实现如下 code 实例所示:

TypeScript 复制代码
/*公共方法类*/
export class PublicUtils {
  /*获取缓存大小*/
  static async getCacheSize(): Promise<number> {
    try {
      let bundleStats = await storageStatistics.getCurrentBundleStats()
      let size = bundleStats.cacheSize / 1024 / 1024
      return Math.round(size)
    } catch (e) {
      console.error(`[PublicUtils] - 获取缓存大小: ErrorCode: ${e.code},  Message: ${e.message}`);
      return 0
    }
  }


  /*清除 Web 浏览器缓存(单个 Web 组件) Rom & Ram*/
  static clearWebCache(webView: web_webview.WebviewController, state: boolean): string {
    if (webView) {
      if (state) { // Rom & Ram: 本地 loc 和 缓存
        try {
          webView.removeCache(true);
          return '浏览器缓存清除成功';
        } catch (error) {
          let e: business_error.BusinessError = error as business_error.BusinessError;
          console.error(`[PublicUtils] - 清除缓存: ErrorCode: ${e.code},  Message: ${e.message}`);
          return '浏览器缓存清除失败:' + e.message;
        }
      } else { // Ram: 仅缓存
        try {
          webView.removeCache(false);
          return '浏览器缓存清除成功';
        } catch (error) {
          let e: business_error.BusinessError = error as business_error.BusinessError;
          console.error(`[PublicUtils] - 清除缓存: ErrorCode: ${e.code},  Message: ${e.message}`);
          return '浏览器缓存清除失败:' + e.message;
        }
      }
    } else {
      return '浏览器缓存清除失败: 无效的组件';
    }
  }
}
TypeScript 复制代码
import { PublicUtils } from '../utils/PublicUtils'

// 获取缓存
PublicUtils.getCacheSize().then((res) => {
  console.log('[获取缓存]: ', res)
  this.detail = res.toString()
})

// 清除缓存
PublicUtils.clearWebCache(this.webView, state);

以上便是此次分享的全部内容,希望能对大家有所帮助!

相关推荐
Flying pigs~~20 小时前
RAG智慧问答项目
数据库·人工智能·缓存·微调·知识库·rag
许彰午21 小时前
CacheSQL(二):主从复制——OpLog 环形缓冲区与故障自动恢复
java·数据库·缓存
jiejiejiejie_1 天前
Flutter for OpenHarmony 心情日记功能实战指南
flutter·华为
Math_teacher_fan1 天前
Flutter 跨平台开发实战:鸿蒙与音乐律动艺术(六)、Lissajous 利萨茹曲线:频率耦合的轨迹艺术
flutter·ui·数学建模·华为·harmonyos·鸿蒙系统
xmdy58661 天前
Flutter+开源鸿蒙实战|智安盾电商溯源平台Day3 溯源查询逻辑+鸿蒙网络请求适配
flutter·开源·harmonyos
maaath1 天前
【maaath】Flutter 跨平台日历日程应用开发实战
flutter·华为·harmonyos
LeesonWong1 天前
架构困境与四层结构化设计
harmonyos
凯瑟琳.奥古斯特1 天前
Redis是什么及核心特性
前端·css·redis·缓存
梦想不只是梦与想1 天前
鸿蒙 应用市场更新功能:版本检测与更新提醒
harmonyos·鸿蒙·版本更新
xmdy58661 天前
Flutter+开源鸿蒙实战|智安盾电商溯源平台Day2 首页+核心入口UI开发(鸿蒙多端适配)
flutter·开源·harmonyos