Insert an entry into the cache. If the cache is at capacity, the least recently used entry is evicted first.
(&mut self, key: K, value: V)
| 75 | /// If the cache is at capacity, the least recently used entry |
| 76 | /// is evicted first. |
| 77 | pub(crate) fn insert(&mut self, key: K, value: V) { |
| 78 | // Evict if at capacity |
| 79 | if self.entries.len() >= self.capacity && !self.entries.contains_key(&key) { |
| 80 | self.evict_lru(); |
| 81 | } |
| 82 | |
| 83 | self.access_counter += 1; |
| 84 | self.entries.insert( |
| 85 | key, |
| 86 | CacheEntry { |
| 87 | value, |
| 88 | last_access: self.access_counter, |
| 89 | }, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /// Remove an entry from the cache. |
| 94 | /// |