-
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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), | ||
| }); | ||
| } | ||
|
|
||
| has(key: string): boolean { | ||
|
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. [WARNING] correctness
Suggestion: Reuse the expiry-check logic from |
||
| return this.store.has(key); | ||
| } | ||
|
|
||
| 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=")) { | ||
| result.maxAge = Number(trimmed.split("=")[1]) * 1000; | ||
|
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. [WARNING] correctness
Suggestion: Validate the parsed value before assignment: |
||
| } | ||
| } | ||
|
|
||
| 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
ttlMs || this.defaultTtlMsuses logical-OR, which is falsy-coalescing. If a caller explicitly passesttlMs: 0(intending immediate or near-immediate expiry), the0is falsy and falls through todefaultTtlMs, silently caching for the default duration instead.Suggestion: Use nullish coalescing:
ttlMs ?? this.defaultTtlMs. This only falls back whenttlMsisnullorundefined, preserving0as a valid value.