@bigmi/core
Version:
TypeScript library for Bitcoin apps.
30 lines • 763 B
JavaScript
/**
* Map with a LRU (Least recently used) policy.
*
* @link https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
*/
export class LruMap extends Map {
constructor(size) {
super();
this.maxSize = size;
}
get(key) {
const value = super.get(key);
if (super.has(key) && value !== undefined) {
this.delete(key);
super.set(key, value);
}
return value;
}
set(key, value) {
super.set(key, value);
if (this.maxSize && this.size > this.maxSize) {
const firstKey = this.keys().next().value;
if (firstKey) {
this.delete(firstKey);
}
}
return this;
}
}
//# sourceMappingURL=lru.js.map