当前位置: 首页 > 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); // 重置为默认容量}
}

http://www.xdnf.cn/news/56917.html

相关文章:

  • 【仿Mudou库one thread per loop式并发服务器实现】HTTP协议模块实现
  • 系统分析师知识点:访问控制模型OBAC、RBAC、TBAC与ABAC的对比与应用
  • Unreal 如何实现一个Vehicle汽车沿着一条指定Spline路径自动驾驶
  • SpringBoot和微服务学习记录Day3
  • PCB 射频天线设计和版图创建技巧
  • Redis 的单线程模型对微服务意味着什么?需要注意哪些潜在瓶颈?
  • 系统架构设计(二):基于架构的软件设计方法ABSD
  • 前端Javascript模块化 CommonJS与ES Module区别
  • 1-1 什么是数据结构
  • DevOps功能详解
  • 人工智能在慢病管理中的具体应用全集:从技术落地到场景创新
  • 华为OD机试真题——数据分类(2025A卷:100分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
  • 爱普生TG-5006CG成为提升5G RedCap时钟同步精度的理想选择
  • 4.1腾讯校招简历优化与自我介绍攻略:公式化表达+结构化呈现
  • 【AI提示词】数据分析专家
  • from tensorflow.keras.models import Model中Model报红;以及动态链接库(DLL)初始化例程失败
  • n8n 中文系列教程_05.如何在本机部署/安装 n8n(详细图文教程)
  • jvm-描述符与特征签名的区别
  • 华为设备命令部分精简分类汇总示例
  • 【Unity iOS打包】报错解决记录
  • OpenCV训练题
  • 初识Redis · C++客户端set和zset
  • 【阿里云大模型高级工程师ACP习题集】2.1 用大模型构建新人答疑机器人
  • 阿里云入门手册
  • Java 将对象转为 Map 的几种方法
  • MySQL安装
  • 栈和队列--数据结构初阶(2)(C/C++)
  • MATLAB 训练CNN模型 yolo v4
  • CSS预处理工具有哪些?分享主流产品
  • 【身份证扫描件识别表格】如何识别大量身份证扫描件将内容导出保存到Excel表格,一次性处理多张身份证图片导出Excel表格,基于WPF和腾讯云的实现方案