(key int, value int)
| 47 | |
| 48 | |
| 49 | func (this *LRUCache) Put(key int, value int) { |
| 50 | if _, ok := this.cache[key]; ok { |
| 51 | this.Remove(this.cache[key]) |
| 52 | } |
| 53 | this.cache[key] = &Node{key: key, val: value} |
| 54 | this.Insert(this.cache[key]) |
| 55 | |
| 56 | if len(this.cache) > this.capacity { |
| 57 | // remove from the list and delete the LRU from hashmap |
| 58 | lru := this.left.next |
| 59 | this.Remove(lru) |
| 60 | delete(this.cache, lru.key) |
| 61 | } |
| 62 | } |