* Evicts the oldest cache entry, if we've exceeded capacity.
()
| 63 | * Evicts the oldest cache entry, if we've exceeded capacity. |
| 64 | */ |
| 65 | evict_() { |
| 66 | if (this.size_ <= this.capacity_) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | const cache = this.cache_; |
| 71 | let oldest = this.access_ + 1; |
| 72 | let oldestKey; |
| 73 | for (const key in cache) { |
| 74 | const {access} = cache[key]; |
| 75 | if (access < oldest) { |
| 76 | oldest = access; |
| 77 | oldestKey = key; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (oldestKey !== undefined) { |
| 82 | delete cache[oldestKey]; |
| 83 | this.size_--; |
| 84 | } |
| 85 | } |
| 86 | } |