LRU 页面置换算法
javascript
class MyLRU {
constructor(n) {
this.size = n // 初始化内存条
this.cacheMap = new Map() // 新插入的数据排在后面,旧数据放在前面
}
put(domain, info) {
this.cacheMap.has(domain) && this.cacheMap.delete(domain)
this.cacheMap.size >= this.size && this.cacheMap.delete(this.cacheMap.keys().next().value)
this.cacheMap.set(domain, info)
}
get(domain) {
if (!this.cacheMap.has(domain)) {
return false
}
const info = this.cacheMap.get(domain)
this.put(domain, info)
return info
}
}