鸿蒙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); // 重置为默认容量
  }
}
相关推荐
小李飞飞砖10 分钟前
Android 插件化实现原理详解
android
m0_5973453118 分钟前
【Android】安卓四大组件之广播接收器(Broadcast Receiver):从基础到进阶
android·java·boradcast·安卓四大组件
whysqwhw41 分钟前
OkHttp PublicSuffix包的后缀列表处理
android
yeziyfx1 小时前
kotlin中集合的用法
android·开发语言·kotlin
Daniel_Coder2 小时前
iOS Widget 开发-7:TimelineProvider 机制全解析:构建未来时间线
ios·swift·widget
EngZegNgi3 小时前
安卓应用启动崩溃的问题排查记录
android·crash·启动崩溃
Daniel_Coder3 小时前
iOS Widget 开发-3:Widget 的种类与尺寸(主屏、锁屏、灵动岛)
ios·swift·widget
火柴就是我3 小时前
每日见闻之Container Decoration
android·flutter
生如夏花℡3 小时前
HarmonyOS学习记录4
学习·华为·harmonyos
天枢破军3 小时前
【AOSP】解决repo拉取提示无法连接android.googlesource.com
android