鸿蒙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); // 重置为默认容量
  }
}
相关推荐
dog shit2 小时前
web第十次课后作业--Mybatis的增删改查
android·前端·mybatis
科技道人3 小时前
Android15 launcher3
android·launcher3·android15·hotseat
哼唧唧_4 小时前
React Native开发鸿蒙运动健康类应用的项目实践记录
react native·harmonyos·harmony os5·运动健康
CYRUS_STUDIO7 小时前
FART 脱壳某大厂 App + CodeItem 修复 dex + 反编译还原源码
android·安全·逆向
Shujie_L10 小时前
【Android基础回顾】四:ServiceManager
android
Think Spatial 空间思维10 小时前
【实施指南】Android客户端HTTPS双向认证实施指南
android·网络协议·https·ssl
louisgeek11 小时前
Git 使用 SSH 连接
android
二流小码农11 小时前
鸿蒙开发:实现一个标题栏吸顶
android·ios·harmonyos
坚果的博客11 小时前
uniappx插件nutpi-idcard 开发与使用指南(适配鸿蒙)
华为·harmonyos
程序员小刘11 小时前
【HarmonyOS 5】 社交行业详解以及 开发案例
华为·harmonyos