UNPKG

raiden-ts

Version:

Raiden Light Client Typescript/Javascript SDK

36 lines 940 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LruCache = void 0; /** * Simple Map-based LRU cache * * @param max - Maximum size of cache */ 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; } } exports.LruCache = LruCache; //# sourceMappingURL=lru.js.map