Save all generated chunks instead of only chunks that were changed
This commit is contained in:
+12
-2
@@ -3,9 +3,12 @@ export class LruMap<K, V> {
|
||||
#map = new Map<K, V>();
|
||||
#newest: K | undefined;
|
||||
readonly max_size: number;
|
||||
// called with what's forgotten for being the least recently used
|
||||
#on_evict?: (key: K, value: V) => void;
|
||||
|
||||
constructor(max_size: number) {
|
||||
constructor(max_size: number, on_evict?: (key: K, value: V) => void) {
|
||||
this.max_size = max_size;
|
||||
this.#on_evict = on_evict;
|
||||
}
|
||||
|
||||
get(key: K): V | undefined {
|
||||
@@ -26,7 +29,9 @@ export class LruMap<K, V> {
|
||||
this.#map.delete(key);
|
||||
this.#map.set(key, value);
|
||||
if (this.#map.size > this.max_size) {
|
||||
this.#map.delete(this.#map.keys().next().value!);
|
||||
const [oldest, value] = this.#map.entries().next().value!;
|
||||
this.#map.delete(oldest);
|
||||
this.#on_evict?.(oldest, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +40,11 @@ export class LruMap<K, V> {
|
||||
return this.#map.has(key);
|
||||
}
|
||||
|
||||
// get without counting as a use
|
||||
peek(key: K): V | undefined {
|
||||
return this.#map.get(key);
|
||||
}
|
||||
|
||||
delete(key: K) {
|
||||
this.#map.delete(key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user