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:
42
lib/swr/cache-provider.ts
Normal file
42
lib/swr/cache-provider.ts
Normal 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()
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user