-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add TTL cache for API response caching #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 { | ||
| 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [INFO] correctness The Suggestion: Document that |
||
| 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; | ||
| } | ||
There was a problem hiding this comment.
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 toMap.has()without checking expiry. This meanshas(key)returnstruefor expired entries that haven't been lazy-deleted yet, whileget(key)returnsundefinedfor the same key. This breaks the API contract — callers who guard onhas()before callingget()will get unexpectedundefined.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;