fix: avoid caching non-positive TTL entries
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user