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); }