make it LRU

This commit is contained in:
Simon Knott 2024-09-02 14:23:33 +02:00
parent 57a50d0250
commit bb79be9225
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -30,8 +30,14 @@ const cache = new Map<SnapshotRenderer, string>();
const CACHE_SIZE = 300000000; // 300mb const CACHE_SIZE = 300000000; // 300mb
function cacheAndReturn(key: SnapshotRenderer, compute: () => string): string { function cacheAndReturn(key: SnapshotRenderer, compute: () => string): string {
if (cache.has(key)) if (cache.has(key)) {
return cache.get(key)!; const value = cache.get(key)!;
// reinserting makes this the least recently used entry
cache.delete(key);
cache.set(key, value);
return value;
}
const result = compute(); const result = compute();