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

发布于:2025-04-22 ⋅ 阅读:(10) ⋅ 点赞:(0)
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); // 重置为默认容量
  }
}
代码如下:
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); // 重置为默认容量
  }
}


网站公告

今日签到

点亮在社区的每一天
去签到