feat: add in-browser caching for mostly-static API data#22
Merged
avishek0769 merged 4 commits intoJun 2, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a simple in-memory TTL cache utility for the application.
Changes:
- Introduces a module-level
Map-backed cache store with per-entry expiration timestamps. - Adds cache operations: get (with TTL eviction), set, clear, remove by key, and remove by substring match.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+18
to
+20
| export function setInCache<T>(key: string, value: T, ttlMs: number): void { | ||
| store.set(key, { value, expiresAt: Date.now() + ttlMs }); | ||
| } |
Comment on lines
+8
to
+15
| export function getFromCache<T>(key: string): { value: T } | null { | ||
| const entry = store.get(key) as CacheEntry<T> | undefined; | ||
| if (!entry) return null; | ||
| if (Date.now() > entry.expiresAt) { | ||
| store.delete(key); | ||
| return null; | ||
| } | ||
| return { value: entry.value }; |
Comment on lines
+8
to
+15
| export function getFromCache<T>(key: string): { value: T } | null { | ||
| const entry = store.get(key) as CacheEntry<T> | undefined; | ||
| if (!entry) return null; | ||
| if (Date.now() > entry.expiresAt) { | ||
| store.delete(key); | ||
| return null; | ||
| } | ||
| return { value: entry.value }; |
| expiresAt: number; | ||
| } | ||
|
|
||
| const store = new Map<string, CacheEntry<unknown>>(); |
Comment on lines
+30
to
+36
| export function removeMatchingFromCache(pattern: string): void { | ||
| for (const key of store.keys()) { | ||
| if (key.includes(pattern)) { | ||
| store.delete(key); | ||
| } | ||
| } | ||
| } |
- In-memory Map cache with TTL support - Stale-while-revalidate for allowlisted GET endpoints - Cache invalidation on sign-out, API key/chat mutations, message send, and ingestion completion - Cache keys scoped by user ID Closes avishek0769#20
df9965c to
ac2fc1f
Compare
Owner
|
All good. Maybe it will be better to have a global setInterval which sweeps the expired cache after every 5 mins, to avoid too much cache to get stored. |
Replace the lazy size-gated expiration sweep (which only ran when the cache reached 200 entries and only on insert) with a global setInterval that sweeps all expired entries every 5 minutes, regardless of cache size. The timer is properly stopped and restarted on clearCache() to ensure clean session transitions. This addresses review feedback suggesting a periodic cleanup strategy to keep memory usage bounded over long sessions.
Contributor
Author
|
@avishek0769 please take a look at the changes |
Owner
|
Looks fine. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #20