Files
JavaScript/Cache/LRUCache.js
Aditya Kumar 8b1a4b90f6 merge: fix #844 (#846)
* fix #844

* formatted code according to standard.js
2021-11-25 12:33:10 +05:30

41 lines
1.1 KiB
JavaScript

class LRUCache {
// LRU Cache to store a given capacity of data
constructor (capacity) {
this.cache = new Map()
this.capacity = capacity
this.hits = 0
this.miss = 0
}
cacheInfo () {
// Return the details for the cache instance [hits, misses, capacity, current_size]
return `CacheInfo(hits=${this.hits}, misses=${this.miss}, capacity=${this.capacity}, current size=${this.cache.size})`
}
set (key, value) {
// Sets the value for the input key and if the key exists it updates the existing key
if (this.cache.size === this.capacity) {
// delete oldest key existing in map
this.cache.delete(this.cache.keys().next().value)
}
this.cache.set(key, value)
}
get (key) {
// Returns the value for the input key. Returns null if key is not present in cache
if (this.cache.has(key)) {
const value = this.cache.get(key)
// refresh the cache to update the order of key
this.cache.delete(key)
this.cache.set(key, value)
this.hits += 1
return value
} else {
this.miss += 1
return null
}
}
}
export { LRUCache }