docs/assets/js/helpers/lrucache.js
raw| 1 | // A simple LRU cache implementation backed by a map. |
| 2 | export class LRUCache { |
| 3 | constructor(maxSize) { |
| 4 | this.maxSize = maxSize; |
| 5 | this.cache = new Map(); |
| 6 | } |
| 7 | |
| 8 | get(key) { |
| 9 | return this.cache.get(key); |
| 10 | } |
| 11 | |
| 12 | put(key, value) { |
| 13 | if (this.cache.size >= this.maxSize) { |
| 14 | const firstKey = this.cache.keys().next().value; |
| 15 | this.cache.delete(firstKey); |
| 16 | } |
| 17 | this.cache.set(key, value); |
| 18 | } |
| 19 | } |