Update LRUCache.ts

Add a get function to the cache
This commit is contained in:
po-lan 2024-07-05 12:09:59 +08:00 committed by GitHub
parent 61cfa0e86d
commit eb84e2f8c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<T> {
// 移除LRU节点
private removeLRUNode(node: cacheNode<T>) {
logDebug(
'removeLRUNode',
"removeLRUNode",
node.groupId,
node.userId,
node.value,
@ -140,6 +140,26 @@ class LRU<T> {
}
}
}
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;