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 1/3] 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; From a9f3e7fc54ba0b8e608181dca80c1f0a4560e00d Mon Sep 17 00:00:00 2001 From: po-lan <42771836+po-lan@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:12:40 +0800 Subject: [PATCH 2/3] Update db.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过读取缓存修复刚说话缺无法获取发言时间的问题 --- src/common/utils/db.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/common/utils/db.ts b/src/common/utils/db.ts index 96bd7a84..e7860150 100644 --- a/src/common/utils/db.ts +++ b/src/common/utils/db.ts @@ -408,10 +408,12 @@ class DBUtil extends DBUtilBase { logDebug('读取发言时间', groupId); return new Promise((resolve, reject) => { this.db!.all(`SELECT * FROM "${groupId}" `, (err, rows: IRember[]) => { + const cache = this.LURCache.get(groupId).map(e=>({user_id:e.userId, last_sent_time:e.value})); if (err) { logError('查询发言时间失败', groupId); - return resolve([]); + return resolve(cache.map(e=>({...e, join_time:0}))); } + Object.assign(rows, cache) logDebug('查询发言时间成功', groupId, rows); resolve(rows); }); From 81134ea2d43619e976b69a5c77df1f4627840b19 Mon Sep 17 00:00:00 2001 From: po-lan <42771836+po-lan@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:13:24 +0800 Subject: [PATCH 3/3] Update LRUCache.ts --- src/common/utils/LRUCache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/utils/LRUCache.ts b/src/common/utils/LRUCache.ts index cf459e87..3a8333fd 100644 --- a/src/common/utils/LRUCache.ts +++ b/src/common/utils/LRUCache.ts @@ -31,7 +31,7 @@ class LRU { private tail: cacheNode | null = null; private onFuncs: ((node: cacheNode) => void)[] = []; - constructor(maxAge: number = 2e4, maxSize: number = 5e3) { + constructor(maxAge: number = 6e4, maxSize: number = 5e3) { this.maxAge = maxAge; this.maxSize = maxSize; this.cache = Object.create(null);