* @method set * @description - This method stored the value by key & add frequency if it doesn't exist * @param {string} key * @param {any} value * @param {number} frequency * @returns {LFUCache}
(key, value, frequency = 1)
| 172 | * @returns {LFUCache} |
| 173 | */ |
| 174 | set(key, value, frequency = 1) { |
| 175 | key = String(key) // converted to string |
| 176 | |
| 177 | if (this.#capacity === 0) { |
| 178 | throw new RangeError('LFUCache ERROR: The Capacity is 0') |
| 179 | } |
| 180 | |
| 181 | if (this.cache.has(key)) { |
| 182 | const node = this.cache.get(key) |
| 183 | node.value = value |
| 184 | |
| 185 | this.#frequencyMap.refresh(node) |
| 186 | |
| 187 | return this |
| 188 | } |
| 189 | |
| 190 | // if the cache size is full, then it's delete the Least Frequency Used node |
| 191 | if (this.#capacity === this.cache.size) { |
| 192 | this.#removeCacheNode() |
| 193 | } |
| 194 | |
| 195 | const newNode = new CacheNode(key, value, frequency) |
| 196 | |
| 197 | this.cache.set(key, newNode) |
| 198 | this.#frequencyMap.insert(newNode) |
| 199 | |
| 200 | return this |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @method parse |