27 lines
792 B
TypeScript
27 lines
792 B
TypeScript
type CacheEntry = { data: any; expiresAt: number }
|
|
const shared = globalThis as typeof globalThis & { __materialFacetsCache?: Map<string, CacheEntry> }
|
|
const cache = shared.__materialFacetsCache ||= new Map<string, CacheEntry>()
|
|
const TTL_MS = 5 * 60 * 1000
|
|
|
|
export function getMaterialFacetsCache(key: string): any | null {
|
|
const entry = cache.get(key)
|
|
if (!entry) return null
|
|
if (Date.now() > entry.expiresAt) {
|
|
cache.delete(key)
|
|
return null
|
|
}
|
|
return entry.data
|
|
}
|
|
|
|
export function setMaterialFacetsCache(key: string, data: any) {
|
|
if (cache.size > 200) {
|
|
const oldest = cache.keys().next().value
|
|
if (oldest) cache.delete(oldest)
|
|
}
|
|
cache.set(key, { data, expiresAt: Date.now() + TTL_MS })
|
|
}
|
|
|
|
export function invalidateMaterialFacetsCache() {
|
|
cache.clear()
|
|
}
|