获取缓存大小与清除 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);

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

相关推荐
弓.长.1 分钟前
基础入门 React Native 鸿蒙跨平台开发:LayoutAnimation 布局动画
react native·react.js·harmonyos
廋到被风吹走1 分钟前
【分布式缓存】分布式缓存架构全解析:从 Redis Cluster 到多级缓存策略
分布式·缓存·架构
地球没有花8 分钟前
tw引发的对redis的深入了解
数据库·redis·缓存·go
世人万千丶9 分钟前
鸿蒙跨端框架 Flutter 学习:GetX 全家桶:从状态管理到路由导航的极简艺术
学习·flutter·ui·华为·harmonyos·鸿蒙
填满你的记忆10 分钟前
【从零开始——Redis 进化日志|Day6】缓存的三剑客:穿透、击穿、雪崩,到底怎么防?(附生产级代码实战)
java·数据库·redis·缓存·面试
弓.长.12 分钟前
基础入门 React Native 鸿蒙跨平台开发:Linking 链接处理 鸿蒙实战
react native·react.js·harmonyos
夜雨声烦丿13 分钟前
Flutter 框架跨平台鸿蒙开发 - 电影票房查询 - 完整开发教程
flutter·华为·harmonyos
弓.长.13 分钟前
基础入门 React Native 鸿蒙跨平台开发:网络请求实战
网络·react native·harmonyos
小白阿龙1 小时前
鸿蒙+flutter 跨平台开发——从零打造手持弹幕App实战
flutter·华为·harmonyos·鸿蒙
编程乐学12 小时前
鸿蒙非原创--DevEcoStudio开发的奶茶点餐APP
华为·harmonyos·deveco studio·鸿蒙开发·奶茶点餐·鸿蒙大作业