鸿蒙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); // 重置为默认容量
  }
}
相关推荐
0wioiw033 分钟前
Kotlin基础(①)
android·开发语言·kotlin
西瓜本瓜@44 分钟前
在 Android 中实现通话录音
android·java·开发语言·学习·github·android-studio
浩浩测试一下2 小时前
信息收集之hack用的网络空间搜索引擎
android·网络·安全·web安全·搜索引擎·网络安全·安全架构
搞瓶可乐2 小时前
鸿蒙ArkUI实战之TextArea组件、RichEditor组件、RichText组件、Search组件的使用
华为·harmonyos·arkui·搜索框·富文本组件·富文本输入框·鸿蒙原生api
孤寂码农_defector2 小时前
鸿蒙系统的 “成长烦恼“:生态突围与技术迭代的双重挑战
macos·华为·objective-c·cocoa·harmonyos
齐格Insight2 小时前
Capacitor 框架下解决One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be ...
android
芦半山2 小时前
后AOSP时代还能贡献代码吗?
android·源码
V少年2 小时前
深入浅出Java编译过程
android
ii_best3 小时前
选择 iOS 按键精灵无根有根越狱辅助工具的理由
ios
墨雪遗痕3 小时前
使用 inobounce 解决 iOS 皮筋效果导致的无法下拉刷新
ios