Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
export interface CacheEntry<T> {
value: T;
expiresAt: number;
}

export class TtlCache<T> {
private store = new Map<string, CacheEntry<T>>();
private maxSize: number;
private defaultTtlMs: number;

constructor(maxSize = 1000, defaultTtlMs = 300000) {
this.maxSize = maxSize;
this.defaultTtlMs = defaultTtlMs;
}

get(key: string): T | undefined {
const entry = this.store.get(key);
if (!entry) return undefined;
if (Date.now() > entry.expiresAt) {
this.store.delete(key);
return undefined;
}
return entry.value;
}

set(key: string, value: T, ttlMs?: number): void {
if (this.store.size >= this.maxSize) {
this.evict();
}
this.store.set(key, {
value,
expiresAt: Date.now() + (ttlMs || this.defaultTtlMs),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WARNING] correctness

ttlMs || this.defaultTtlMs uses logical-OR, which is falsy-coalescing. If a caller explicitly passes ttlMs: 0 (intending immediate or near-immediate expiry), the 0 is falsy and falls through to defaultTtlMs, silently caching for the default duration instead.

Suggestion: Use nullish coalescing: ttlMs ?? this.defaultTtlMs. This only falls back when ttlMs is null or undefined, preserving 0 as a valid value.

});
}

has(key: string): boolean {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WARNING] correctness

has() delegates directly to Map.has() without checking expiry. This means has(key) returns true for expired entries that haven't been lazy-deleted yet, while get(key) returns undefined for the same key. This breaks the API contract — callers who guard on has() before calling get() will get unexpected undefined.

Suggestion: Reuse the expiry-check logic from get(): const entry = this.store.get(key); if (!entry) return false; if (Date.now() > entry.expiresAt) { this.store.delete(key); return false; } return true;

return this.store.has(key);
}

delete(key: string): boolean {
return this.store.delete(key);
}

clear(): void {
this.store.clear();
}

get size(): number {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[INFO] correctness

The size getter returns this.store.size, which includes expired entries that haven't been lazily cleaned up yet. Callers may see a size that doesn't reflect the number of actually-valid entries.

Suggestion: Document that size includes expired-but-not-yet-reaped entries, or consider filtering expired entries (though that would be O(n)).

return this.store.size;
}

private evict(): void {
let oldestKey: string = "";
let oldestTime = Infinity;

for (const [key, entry] of this.store) {
if (entry.expiresAt < oldestTime) {
oldestTime = entry.expiresAt;
oldestKey = key;
}
}

if (oldestKey) this.store.delete(oldestKey);
}
}

export function parseCacheControl(header: string): { maxAge: number; noCache: boolean } {
const result = { maxAge: 0, noCache: false };

for (const directive of header.split(",")) {
const trimmed = directive.trim().toLowerCase();
if (trimmed === "no-cache" || trimmed === "no-store") {
result.noCache = true;
}
if (trimmed.startsWith("max-age=")) {
result.maxAge = Number(trimmed.split("=")[1]) * 1000;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WARNING] correctness

Number(trimmed.split("=")[1]) produces NaN for malformed directives like max-age=abc. This NaN (multiplied by 1000) propagates into result.maxAge, and if used downstream as a TTL, Date.now() + NaN = NaN, making the entry effectively inaccessible via get() (since Date.now() > NaN is false).

Suggestion: Validate the parsed value before assignment: const parsed = Number(trimmed.split("=")[1]); if (Number.isFinite(parsed)) result.maxAge = parsed * 1000;

}
}

return result;
}
Loading