class Cache {
constructor (limit = 5) {
this.limit = limit
this.map = {}
this.keys = []
}
set (key,value) {
let map = this.map
let keys = this.keys
if (!Object.prototype.hasOwnProperty.call(map,key)) {
if (keys.length === this.limit) {
// 先进先出的策略进行淘汰
delete map[keys.shift()]
}
keys.push(key)
}
map[key] = value
}
get (key) {
return this.map[key]
}
}
FIFO-Cache
最新推荐文章于 2022-09-02 01:14:43 发布