Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 3 additions & 66 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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),
});
}

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;

const entry = this.store.get(key);
if (!entry) return false;
if (Date.now() > entry.expiresAt) {
this.store.delete(key);
return false;
}
return true;
}

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=")) {
const parsed = Number(trimmed.split("=")[1]);
if (Number.isFinite(parsed)) result.maxAge = parsed * 1000;
}
}

return result;
}
Loading