chore: Dedupe sent-message cache storage (#22127)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 8401257b27
Co-authored-by: TaKO8Ki <41065217+TaKO8Ki@users.noreply.github.com>
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Reviewed-by: @obviyus
This commit is contained in:
Takayuki Maeda
2026-02-21 16:04:59 +09:00
committed by GitHub
parent 35fd322114
commit 40f1a6c0d2
2 changed files with 4 additions and 6 deletions

View File

@@ -6,7 +6,6 @@
const TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
type CacheEntry = {
messageIds: Set<number>;
timestamps: Map<number, number>;
};
@@ -20,7 +19,6 @@ function cleanupExpired(entry: CacheEntry): void {
const now = Date.now();
for (const [msgId, timestamp] of entry.timestamps) {
if (now - timestamp > TTL_MS) {
entry.messageIds.delete(msgId);
entry.timestamps.delete(msgId);
}
}
@@ -33,13 +31,12 @@ export function recordSentMessage(chatId: number | string, messageId: number): v
const key = getChatKey(chatId);
let entry = sentMessages.get(key);
if (!entry) {
entry = { messageIds: new Set(), timestamps: new Map() };
entry = { timestamps: new Map() };
sentMessages.set(key, entry);
}
entry.messageIds.add(messageId);
entry.timestamps.set(messageId, Date.now());
// Periodic cleanup
if (entry.messageIds.size > 100) {
if (entry.timestamps.size > 100) {
cleanupExpired(entry);
}
}
@@ -55,7 +52,7 @@ export function wasSentByBot(chatId: number | string, messageId: number): boolea
}
// Clean up expired entries on read
cleanupExpired(entry);
return entry.messageIds.has(messageId);
return entry.timestamps.has(messageId);
}
/**