mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 12:17:40 +00:00
refactor(plugin-sdk): unify channel dedupe primitives
This commit is contained in:
@@ -2,6 +2,7 @@ import { pruneMapToMaxSize } from "./map-size.js";
|
||||
|
||||
export type DedupeCache = {
|
||||
check: (key: string | undefined | null, now?: number) => boolean;
|
||||
peek: (key: string | undefined | null, now?: number) => boolean;
|
||||
clear: () => void;
|
||||
size: () => number;
|
||||
};
|
||||
@@ -37,20 +38,39 @@ export function createDedupeCache(options: DedupeCacheOptions): DedupeCache {
|
||||
pruneMapToMaxSize(cache, maxSize);
|
||||
};
|
||||
|
||||
const hasUnexpired = (key: string, now: number, touchOnRead: boolean): boolean => {
|
||||
const existing = cache.get(key);
|
||||
if (existing === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (ttlMs > 0 && now - existing >= ttlMs) {
|
||||
cache.delete(key);
|
||||
return false;
|
||||
}
|
||||
if (touchOnRead) {
|
||||
touch(key, now);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
return {
|
||||
check: (key, now = Date.now()) => {
|
||||
if (!key) {
|
||||
return false;
|
||||
}
|
||||
const existing = cache.get(key);
|
||||
if (existing !== undefined && (ttlMs <= 0 || now - existing < ttlMs)) {
|
||||
touch(key, now);
|
||||
if (hasUnexpired(key, now, true)) {
|
||||
return true;
|
||||
}
|
||||
touch(key, now);
|
||||
prune(now);
|
||||
return false;
|
||||
},
|
||||
peek: (key, now = Date.now()) => {
|
||||
if (!key) {
|
||||
return false;
|
||||
}
|
||||
return hasUnexpired(key, now, false);
|
||||
},
|
||||
clear: () => {
|
||||
cache.clear();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user