|
1 | 1 | class LRUCache {
|
2 | 2 | // LRU Cache to store a given capacity of data
|
| 3 | + #capacity |
| 4 | + |
| 5 | + /** |
| 6 | + * @param {number} capacity - the capacity of LRUCache |
| 7 | + * @returns {LRUCache} - sealed |
| 8 | + */ |
3 | 9 | constructor (capacity) {
|
4 |
| - this.cache = new Map() |
5 |
| - this.capacity = capacity |
| 10 | + if (!Number.isInteger(capacity) || capacity < 0) { |
| 11 | + throw new TypeError('Invalid capacity') |
| 12 | + } |
| 13 | + |
| 14 | + this.#capacity = ~~capacity |
| 15 | + this.misses = 0 |
6 | 16 | this.hits = 0
|
7 |
| - this.miss = 0 |
| 17 | + this.cache = new Map() |
| 18 | + |
| 19 | + return Object.seal(this) |
| 20 | + } |
| 21 | + |
| 22 | + get info () { |
| 23 | + return Object.freeze({ |
| 24 | + misses: this.misses, |
| 25 | + hits: this.hits, |
| 26 | + capacity: this.capacity, |
| 27 | + size: this.size |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + get size () { |
| 32 | + return this.cache.size |
8 | 33 | }
|
9 | 34 |
|
10 |
| - cacheInfo () { |
11 |
| - // Return the details for the cache instance [hits, misses, capacity, current_size] |
12 |
| - return `CacheInfo(hits=${this.hits}, misses=${this.miss}, capacity=${this.capacity}, current size=${this.cache.size})` |
| 35 | + get capacity () { |
| 36 | + return this.#capacity |
13 | 37 | }
|
14 | 38 |
|
| 39 | + set capacity (newCapacity) { |
| 40 | + if (newCapacity < 0) { |
| 41 | + throw new RangeError('Capacity should be greater than 0') |
| 42 | + } |
| 43 | + |
| 44 | + if (newCapacity < this.capacity) { |
| 45 | + let diff = this.capacity - newCapacity |
| 46 | + |
| 47 | + while (diff--) { |
| 48 | + this.#removeLeastRecentlyUsed() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + this.#capacity = newCapacity |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * delete oldest key existing in map by the help of iterator |
| 57 | + */ |
| 58 | + #removeLeastRecentlyUsed () { |
| 59 | + this.cache.delete(this.cache.keys().next().value) |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param {string} key |
| 64 | + * @returns {*} |
| 65 | + */ |
| 66 | + has (key) { |
| 67 | + key = String(key) |
| 68 | + |
| 69 | + return this.cache.has(key) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param {string} key |
| 74 | + * @param {*} value |
| 75 | + */ |
15 | 76 | set (key, value) {
|
| 77 | + key = String(key) |
16 | 78 | // Sets the value for the input key and if the key exists it updates the existing key
|
17 |
| - if (this.cache.size === this.capacity) { |
18 |
| - // delete oldest key existing in map |
19 |
| - this.cache.delete(this.cache.keys().next().value) |
| 79 | + if (this.size === this.capacity) { |
| 80 | + this.#removeLeastRecentlyUsed() |
20 | 81 | }
|
| 82 | + |
21 | 83 | this.cache.set(key, value)
|
22 | 84 | }
|
23 | 85 |
|
| 86 | + /** |
| 87 | + * @param {string} key |
| 88 | + * @returns {*} |
| 89 | + */ |
24 | 90 | get (key) {
|
| 91 | + key = String(key) |
25 | 92 | // Returns the value for the input key. Returns null if key is not present in cache
|
26 | 93 | if (this.cache.has(key)) {
|
27 | 94 | const value = this.cache.get(key)
|
| 95 | + |
28 | 96 | // refresh the cache to update the order of key
|
29 | 97 | this.cache.delete(key)
|
30 | 98 | this.cache.set(key, value)
|
31 |
| - this.hits += 1 |
| 99 | + |
| 100 | + this.hits++ |
32 | 101 | return value
|
33 |
| - } else { |
34 |
| - this.miss += 1 |
35 |
| - return null |
36 | 102 | }
|
| 103 | + |
| 104 | + this.misses++ |
| 105 | + return null |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param {JSON} json |
| 110 | + * @returns {LRUCache} |
| 111 | + */ |
| 112 | + parse (json) { |
| 113 | + const { misses, hits, cache } = JSON.parse(json) |
| 114 | + |
| 115 | + this.misses += misses ?? 0 |
| 116 | + this.hits += hits ?? 0 |
| 117 | + |
| 118 | + for (const key in cache) { |
| 119 | + this.set(key, cache[key]) |
| 120 | + } |
| 121 | + |
| 122 | + return this |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param {number} indent |
| 127 | + * @returns {JSON} - string |
| 128 | + */ |
| 129 | + toString (indent) { |
| 130 | + const replacer = (_, value) => { |
| 131 | + if (value instanceof Set) { |
| 132 | + return [...value] |
| 133 | + } |
| 134 | + |
| 135 | + if (value instanceof Map) { |
| 136 | + return Object.fromEntries(value) |
| 137 | + } |
| 138 | + |
| 139 | + return value |
| 140 | + } |
| 141 | + |
| 142 | + return JSON.stringify(this, replacer, indent) |
37 | 143 | }
|
38 | 144 | }
|
39 | 145 |
|
40 |
| -export { LRUCache } |
| 146 | +export default LRUCache |
0 commit comments