Inserts a key-value pair into the cache.
(&mut self, key: Key, value: Value)
| 35 | |
| 36 | /// Inserts a key-value pair into the cache. |
| 37 | pub fn insert(&mut self, key: Key, value: Value) { |
| 38 | // Remove an element if the cache is full. |
| 39 | let key_to_remove = if self.map.len() >= self.max_size { |
| 40 | self.map.keys().next().cloned() |
| 41 | } else { |
| 42 | None |
| 43 | }; |
| 44 | |
| 45 | if let Some(key) = key_to_remove { |
| 46 | self.map.remove(&key); |
| 47 | } |
| 48 | |
| 49 | self.map.insert(key, value); |
| 50 | } |
| 51 | |
| 52 | /// Returns a reference to the value at the key if it exists. |
| 53 | pub fn get(&self, key: &Key) -> Option<&Value> { |