refactor: dedupe extension runtime caches
parent
f095bbd7b0
commit
89d65521fe
@ -1,44 +1,23 @@
|
||||
const TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
||||
|
||||
type CacheEntry = {
|
||||
timestamps: Map<string, number>;
|
||||
};
|
||||
|
||||
const sentMessages = new Map<string, CacheEntry>();
|
||||
import { createScopedExpiringIdCache } from "openclaw/plugin-sdk/text-runtime";
|
||||
|
||||
function cleanupExpired(entry: CacheEntry): void {
|
||||
const now = Date.now();
|
||||
for (const [msgId, timestamp] of entry.timestamps) {
|
||||
if (now - timestamp > TTL_MS) {
|
||||
entry.timestamps.delete(msgId);
|
||||
}
|
||||
}
|
||||
}
|
||||
const TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
||||
const sentMessageCache = createScopedExpiringIdCache<string, string>({
|
||||
store: new Map<string, Map<string, number>>(),
|
||||
ttlMs: TTL_MS,
|
||||
cleanupThreshold: 200,
|
||||
});
|
||||
|
||||
export function recordMSTeamsSentMessage(conversationId: string, messageId: string): void {
|
||||
if (!conversationId || !messageId) {
|
||||
return;
|
||||
}
|
||||
let entry = sentMessages.get(conversationId);
|
||||
if (!entry) {
|
||||
entry = { timestamps: new Map() };
|
||||
sentMessages.set(conversationId, entry);
|
||||
}
|
||||
entry.timestamps.set(messageId, Date.now());
|
||||
if (entry.timestamps.size > 200) {
|
||||
cleanupExpired(entry);
|
||||
}
|
||||
sentMessageCache.record(conversationId, messageId);
|
||||
}
|
||||
|
||||
export function wasMSTeamsMessageSent(conversationId: string, messageId: string): boolean {
|
||||
const entry = sentMessages.get(conversationId);
|
||||
if (!entry) {
|
||||
return false;
|
||||
}
|
||||
cleanupExpired(entry);
|
||||
return entry.timestamps.has(messageId);
|
||||
return sentMessageCache.has(conversationId, messageId);
|
||||
}
|
||||
|
||||
export function clearMSTeamsSentMessageCache(): void {
|
||||
sentMessages.clear();
|
||||
sentMessageCache.clear();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue