From eb84e2f8c90c971a0a924420f29715a32ebd4824 Mon Sep 17 00:00:00 2001 From: po-lan <42771836+po-lan@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:09:59 +0800 Subject: [PATCH] Update LRUCache.ts Add a get function to the cache --- src/common/utils/LRUCache.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/common/utils/LRUCache.ts b/src/common/utils/LRUCache.ts index 4240f298..cf459e87 100644 --- a/src/common/utils/LRUCache.ts +++ b/src/common/utils/LRUCache.ts @@ -1,4 +1,4 @@ -import { logError, logDebug } from '@/common/utils/log'; +import { logError, logDebug } from "@/common/utils/log"; type group_id = number; type user_id = number; @@ -44,7 +44,7 @@ class LRU { // 移除LRU节点 private removeLRUNode(node: cacheNode) { logDebug( - 'removeLRUNode', + "removeLRUNode", node.groupId, node.userId, node.value, @@ -140,6 +140,26 @@ class LRU { } } } + public get(groupId: group_id): { userId: user_id; value: T }[]; + public get(groupId: group_id, userId: user_id): null | { userId: user_id; value: T }; + public get(groupId: group_id, userId?: user_id): any { + const groupObject = this.cache[groupId]; + if(!groupObject) return userId === undefined ? [] : null; + + if (userId === undefined) { + return Object.entries(groupObject).map(([userId, { value }]) => ({ + userId: Number(userId), + value, + })); + } + + if (groupObject[userId]) { + return { userId, value: groupObject[userId].value }; + } + + return null; + + } } export default LRU;