当前位置: 首页 > news >正文

鸿蒙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); // 重置为默认容量}
}
代码如下:
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); // 重置为默认容量}
}

相关文章:

  • Gmssl实战
  • OpenSSL1.1.1d windows安装包资源使用
  • 【C++编程入门】:从零开始掌握基础语法
  • Python常用的第三方模块【openpyxl库】读写Excel文件
  • Vue路由传参的几种方式-案例
  • 系统分析师知识点:访问控制模型OBAC、RBAC、TBAC与ABAC的对比与应用
  • ONLYOFFICE协作空间3.1发布:虚拟数据房间中基于角色的表单填写、房间模板、改进访客管理等
  • 利用WSL2的镜像功能访问Windows下的所有网卡
  • 日志文件太大,如何分卷压缩便于传输
  • 第 2 篇:初探时间序列 - 可视化与基本概念
  • 【网络编程】从零开始彻底了解网络编程(三)
  • IQ信号和实信号的关系与转换的matlab实现
  • 软件工程师中级考试-上午知识点总结(上)
  • Docker概念详解
  • Netdata 监控多台服务器
  • 【大模型ChatGPT +DeepSeeK+python】最新AI赋能Python长时序植被遥感动态分析、物候提取、时空变异归因及RSEI生态评估
  • JavaScript与TypeScript
  • ‌射频功率放大器的核心工作机制与组件设计
  • 从模拟到数字:舵机控制技术的飞跃!
  • Java中 关于编译(Compilation)、类加载(Class Loading) 和 运行(Execution)的详细区别解析
  • 特朗普“炮轰”美联储带崩美股!道指跌超900点,黄金再创新高
  • 一条水脉串起七个特色区块,上海嘉定发布2025年新城行动方案
  • 竹子砍了地却种不上,贵州赤水被指“整改复耕”存形式主义
  • 日本首相石破茂向靖国神社献祭品
  • 京东:自21日起,所有超时20分钟以上的外卖订单全部免单
  • 深化应用型人才培养,这所高校聘任行业企业专家深度参与专业设置