class LRU {
constructor(max){
this.max=max; // 缓存最大数量
this.cache=new Map()
}
get(key){
if(this.cache.has(key)){
const item = this.cache.get(key)
this.cache.delete(key)
this.cache.set(key,item)
return item
}
return -1
}
put(key,value){
if(this.cache.has(key)){
this.cache.delete(key)
}else if(this.cache.size > this.max){
this.cache.delete(this.cache.keys().next().value)
}
this.cache.set(key,value)
}
}
JS LRU算法实现
最新推荐文章于 2022-11-30 11:10:56 发布