MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / set

Method set

Cache/LFUCache.js:174–201  ·  view source on GitHub ↗

* @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)

Source from the content-addressed store, hash-verified

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

Callers 2

parseMethod · 0.95
insertMethod · 0.45

Calls 5

#removeCacheNodeMethod · 0.95
refreshMethod · 0.80
hasMethod · 0.45
getMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected