js-practice/src/array/lru-cache.js
2020-05-25 22:27:39 +08:00

22 lines
454 B
JavaScript

export default class LRUCache {
constructor (capacity) {
this.max = capacity; this.map = new Map()
}
get (key) {
const value = this.map.get(key)
if (value !== undefined) {
this.map.delete(key)
this.map.set(key, value)
return value
}
return -1
}
put (key, value) {
this.map.delete(key)
this.map.set(key, value)
if (this.map.size > this.max) this.map.delete(this.map.keys().next().value)
}
}