鸿蒙NEXT开发LRUCache缓存工具类(单例模式)(ArkTs)

复制代码
import { util } from '@kit.ArkTS';

/**
 * LRUCache缓存工具类(单例模式)
 * @author 鸿蒙布道师
 * @since 2025/04/21
 */
export class LRUCacheUtil {
  private static instance: LRUCacheUtil;
  private lruCache: util.LRUCache<string, any>;

  /**
   * 私有构造函数,防止外部实例化
   */
  private constructor() {
    this.lruCache = new util.LRUCache(64); // 默认容量为64
  }

  /**
   * 获取LRUCacheUtil的单例
   * @returns LRUCacheUtil 单例对象
   */
  public static getInstance(): LRUCacheUtil {
    if (!LRUCacheUtil.instance) {
      LRUCacheUtil.instance = new LRUCacheUtil();
    }
    return LRUCacheUtil.instance;
  }

  /**
   * 判断是否包含指定key的缓存
   * @param key 缓存键
   * @returns 是否存在该key
   */
  public has(key: string): boolean {
    return this.lruCache.contains(key);
  }

  /**
   * 获取指定key的缓存值
   * @param key 缓存键
   * @returns 缓存值,若不存在则返回undefined
   */
  public get<T = any>(key: string): T | undefined {
    return this.lruCache.get(key) as T;
  }

  /**
   * 添加或更新缓存
   * @param key 缓存键
   * @param value 缓存值
   */
  public put(key: string, value: any): void {
    this.lruCache.put(key, value);
  }

  /**
   * 删除指定key的缓存
   * @param key 缓存键
   */
  public remove(key: string): void {
    this.lruCache.remove(key);
  }

  /**
   * 判断缓存是否为空
   * @returns 是否为空
   */
  public isEmpty(): boolean {
    return this.lruCache.isEmpty();
  }

  /**
   * 获取当前缓存的容量
   * @returns 当前容量
   */
  public getCapacity(): number {
    return this.lruCache.getCapacity();
  }

  /**
   * 更新缓存的容量
   * @param newCapacity 新的容量
   */
  public updateCapacity(newCapacity: number): void {
    if (newCapacity <= 0) {
      throw new Error("Capacity must be greater than 0.");
    }
    this.lruCache.updateCapacity(newCapacity);
  }

  /**
   * 清空缓存并重置容量为默认值
   */
  public clear(): void {
    this.lruCache.clear();
    this.lruCache.updateCapacity(64); // 重置为默认容量
  }
}
代码如下:
TypeScript 复制代码
import { util } from '@kit.ArkTS';

/**
 * LRUCache缓存工具类(单例模式)
 * @author 鸿蒙布道师
 * @since 2025/04/21
 */
export class LRUCacheUtil {
  private static instance: LRUCacheUtil;
  private lruCache: util.LRUCache<string, any>;

  /**
   * 私有构造函数,防止外部实例化
   */
  private constructor() {
    this.lruCache = new util.LRUCache(64); // 默认容量为64
  }

  /**
   * 获取LRUCacheUtil的单例
   * @returns LRUCacheUtil 单例对象
   */
  public static getInstance(): LRUCacheUtil {
    if (!LRUCacheUtil.instance) {
      LRUCacheUtil.instance = new LRUCacheUtil();
    }
    return LRUCacheUtil.instance;
  }

  /**
   * 判断是否包含指定key的缓存
   * @param key 缓存键
   * @returns 是否存在该key
   */
  public has(key: string): boolean {
    return this.lruCache.contains(key);
  }

  /**
   * 获取指定key的缓存值
   * @param key 缓存键
   * @returns 缓存值,若不存在则返回undefined
   */
  public get<T = any>(key: string): T | undefined {
    return this.lruCache.get(key) as T;
  }

  /**
   * 添加或更新缓存
   * @param key 缓存键
   * @param value 缓存值
   */
  public put(key: string, value: any): void {
    this.lruCache.put(key, value);
  }

  /**
   * 删除指定key的缓存
   * @param key 缓存键
   */
  public remove(key: string): void {
    this.lruCache.remove(key);
  }

  /**
   * 判断缓存是否为空
   * @returns 是否为空
   */
  public isEmpty(): boolean {
    return this.lruCache.isEmpty();
  }

  /**
   * 获取当前缓存的容量
   * @returns 当前容量
   */
  public getCapacity(): number {
    return this.lruCache.getCapacity();
  }

  /**
   * 更新缓存的容量
   * @param newCapacity 新的容量
   */
  public updateCapacity(newCapacity: number): void {
    if (newCapacity <= 0) {
      throw new Error("Capacity must be greater than 0.");
    }
    this.lruCache.updateCapacity(newCapacity);
  }

  /**
   * 清空缓存并重置容量为默认值
   */
  public clear(): void {
    this.lruCache.clear();
    this.lruCache.updateCapacity(64); // 重置为默认容量
  }
}
相关推荐
爬虫程序猿7 小时前
利用爬虫按关键字搜索淘宝商品实战指南
android·爬虫
顾北川_野7 小时前
Android ttyS2无法打开该如何配置 + ttyS0和ttyS1可以
android·fpga开发
前端世界8 小时前
鸿蒙UI开发全解:JS与Java双引擎实战指南
javascript·ui·harmonyos
wzj_what_why_how10 小时前
Android网络层架构:统一错误处理的问题分析到解决方案与设计实现
android·架构
千里马学框架10 小时前
User手机上如何抓取界面的布局uiautomatorviewer
android·智能手机·aosp·uiautomator·布局抓取·user版本
阿巴~阿巴~11 小时前
操作系统核心技术剖析:从Android驱动模型到鸿蒙微内核的国产化实践
android·华为·harmonyos
Engandend11 小时前
Flutter与iOS混合开发交互
flutter·ios·程序员
iMerryou11 小时前
鸿蒙的动态渐变背景实现
harmonyos
hsx66611 小时前
使用 MaterialShapeDrawable 自定义各种形状的 View
android
用户20187928316712 小时前
滑动城堡的奇妙管家 ——ViewPager故事
android