fix: avoid caching non-positive TTL entries

This commit is contained in:
Mo Elzubeir
2026-05-29 14:39:04 -05:00
parent d820a39223
commit 4ccf060a3e
3 changed files with 29 additions and 2 deletions
+5 -1
View File
@@ -5,7 +5,7 @@
export interface Cache {
/** Return a cached value, or `undefined` on miss/expiry. */
get(key: string): Promise<unknown | undefined>;
/** Store a value for the given TTL in milliseconds. */
/** Store a value for the given TTL in milliseconds. Non-positive TTL means "do not cache". */
set(key: string, value: unknown, ttlMs: number): Promise<void>;
/** Delete one cache entry. */
delete(key: string): Promise<void>;
@@ -31,6 +31,10 @@ export class MemoryCache implements Cache {
}
async set(key: string, value: unknown, ttlMs: number): Promise<void> {
if (ttlMs <= 0) {
this.map.delete(key);
return;
}
this.map.set(key, { at: Date.now(), value, ttlMs });
}