Open DamomHd opened 3 years ago
function LRUCache (length){ this.cacheMap = new Map(); this.length = length; } LRUCache.prototype.get(key){ if(this.cacheMap.has(key)){ // 命中 取值 修改顺序 let temp = this.cacheMap.get(key); this.cacheMap.delete(key) this.cacheMap.set(key,temp) return temp } return -1 } LRUCache.prototype.put(key,val){ if(this.cacheMap.has(key)){ this.cacheMap.delete(key) this.cacheMap.set(key,val) } else{ // 堆超出最大数 if(this.cacheMap.size >= this.length){ let firstKey = this.cacheMap.keys().next.value; this.cacheMap.delete(firstKey) this.cacheMap.set(key,val) } else{ this.cacheMap.set(key,val) } } }