Files
med-notes/.pnpm-store/v10/files/db/2f8545d79222f5239ac210c07b5e299e92c9f6ede1046e1ac1ef0b3085d9da955aaf7ba38eea694e9e70d6a785a70b7723ce56118873fcd8955abe4bc08042
2025-05-09 05:30:08 +02:00

41 lines
788 B
Plaintext

class LRUCache {
constructor () {
this.max = 1000
this.map = new Map()
}
get (key) {
const value = this.map.get(key)
if (value === undefined) {
return undefined
} else {
// Remove the key from the map and add it to the end
this.map.delete(key)
this.map.set(key, value)
return value
}
}
delete (key) {
return this.map.delete(key)
}
set (key, value) {
const deleted = this.delete(key)
if (!deleted && value !== undefined) {
// If cache is full, delete the least recently used item
if (this.map.size >= this.max) {
const firstKey = this.map.keys().next().value
this.delete(firstKey)
}
this.map.set(key, value)
}
return this
}
}
module.exports = LRUCache