From 607dd686207320a59942887369a6512e6df74f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 13 Jan 2025 20:35:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=A0=87=E5=87=86=E5=8C=96?= =?UTF-8?q?=E4=B8=8E=E6=8F=90=E9=AB=98=E7=BC=93=E5=AD=98=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/file-uuid.ts | 51 +++++++++++++++++++++++++++++++----- src/common/message-unique.ts | 2 +- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/common/file-uuid.ts b/src/common/file-uuid.ts index 717cae71..822ca873 100644 --- a/src/common/file-uuid.ts +++ b/src/common/file-uuid.ts @@ -1,5 +1,4 @@ import { Peer } from '@/core'; -import { LRUCache } from './lru-cache'; import { randomUUID } from 'crypto'; interface FileUUIDData { @@ -11,11 +10,49 @@ interface FileUUIDData { fileUUID?: string; } -class FileUUIDManager { - private cache: LRUCache; +class TimeBasedCache { + private cache: Map; + private ttl: number; - constructor(capacity: number) { - this.cache = new LRUCache(capacity); + constructor(ttl: number) { + this.cache = new Map(); + this.ttl = ttl; + } + + public put(key: K, value: V): void { + const timestamp = Date.now(); + this.cache.set(key, { value, timestamp }); + this.cleanup(); + } + + public get(key: K): V | undefined { + const entry = this.cache.get(key); + if (entry) { + const currentTime = Date.now(); + if (currentTime - entry.timestamp < this.ttl) { + return entry.value; + } else { + this.cache.delete(key); + } + } + return undefined; + } + + private cleanup(): void { + const currentTime = Date.now(); + for (const [key, entry] of this.cache.entries()) { + if (currentTime - entry.timestamp >= this.ttl) { + this.cache.delete(key); + } + } + } +} + +class FileUUIDManager { + private cache: TimeBasedCache; + + constructor(ttl: number) { + this.cache = new TimeBasedCache(ttl); } public encode(data: FileUUIDData, endString: string = "", customUUID?: string): string { @@ -32,8 +69,8 @@ class FileUUIDManager { export class FileNapCatOneBotUUIDWrap { private manager: FileUUIDManager; - constructor(capacity: number = 100) { - this.manager = new FileUUIDManager(capacity); + constructor(ttl: number = 86400000) { + this.manager = new FileUUIDManager(ttl); } public encodeModelId(peer: Peer, modelId: string, fileId: string, fileUUID: string = "", endString: string = "", customUUID?: string): string { diff --git a/src/common/message-unique.ts b/src/common/message-unique.ts index d6ca43fc..cc4187e7 100644 --- a/src/common/message-unique.ts +++ b/src/common/message-unique.ts @@ -78,7 +78,7 @@ class MessageUniqueWrapper { private readonly msgDataMap: LimitedHashTable; private readonly msgIdMap: LimitedHashTable; - constructor(maxMap: number = 1000) { + constructor(maxMap: number = 5000) { this.msgIdMap = new LimitedHashTable(maxMap); this.msgDataMap = new LimitedHashTable(maxMap); }