raiden-ts
Version:
Raiden Light Client Typescript/Javascript SDK
32 lines • 814 B
JavaScript
/**
* Simple Map-based LRU cache
*
* @param max - Maximum size of cache
*/
export class LruCache extends Map {
constructor(max) {
super();
this.max = max;
}
get(key) {
let value;
if (this.has(key)) {
// peek the entry, re-insert for LRU strategy
value = super.get(key);
this.delete(key);
super.set(key, value);
}
return value;
}
set(key, value) {
this.get(key); // bump key on set if exists
super.set(key, value);
while (this.size > this.max) {
// least-recently used cache eviction strategy
const keyToDelete = this.keys().next().value;
this.delete(keyToDelete);
}
return this;
}
}
//# sourceMappingURL=lru.js.map