LRU缓存器

66 阅读1分钟
let LRUcache = function(capacity){
  this.cache = new Map()
  this.capacity = capacity
}

LRUcahce.prototype.get = function(key){
  if(this.cache.has(key)){
      let value = this.cache.get(key)
      this.cache.delete(key)
      this.cache.set(key,value)
      return value
  }
  return -1
}

LRUCache.prototype.set = function(key,value){
  if(this.cache.has(key)){
      this.cache.delete(value)
  }
  if(this.cache.size>=this.capacity){
      //开启map迭代
      this.cache.delete(this.cache.keys().next().value);
  }
  this.cache.set(key,value)
}