perf: bound SWR cache, clean stale storage, cap annotations

Add LRU cache provider (200 entries) to prevent unbounded SWR memory
growth. Clean up stale PKCE localStorage keys on app init. Cap chart
annotations to 20 visible reference lines with overflow indicator.
This commit is contained in:
Usman Baig
2026-03-10 21:19:33 +01:00
parent 502f4952fc
commit 205cdf314c
7 changed files with 99 additions and 8 deletions

42
lib/swr/cache-provider.ts Normal file
View File

@@ -0,0 +1,42 @@
// * Bounded LRU cache provider for SWR
// * Prevents unbounded memory growth during long sessions across many sites
const MAX_CACHE_ENTRIES = 200
export function boundedCacheProvider() {
const map = new Map()
const accessOrder: string[] = []
const touch = (key: string) => {
const idx = accessOrder.indexOf(key)
if (idx > -1) accessOrder.splice(idx, 1)
accessOrder.push(key)
}
const evict = () => {
while (map.size > MAX_CACHE_ENTRIES && accessOrder.length > 0) {
const oldest = accessOrder.shift()!
map.delete(oldest)
}
}
return {
get(key: string) {
if (map.has(key)) touch(key)
return map.get(key)
},
set(key: string, value: any) {
map.set(key, value)
touch(key)
evict()
},
delete(key: string) {
map.delete(key)
const idx = accessOrder.indexOf(key)
if (idx > -1) accessOrder.splice(idx, 1)
},
keys() {
return map.keys()
},
}
}