| 71 | } |
| 72 | |
| 73 | pub fn insert(&self, key: K, value: V) { |
| 74 | let mut cache = self.cache.write().unwrap(); |
| 75 | |
| 76 | // Simple eviction: remove oldest entry if at capacity |
| 77 | if cache.len() >= self.capacity && !cache.contains_key(&key) |
| 78 | && let Some((oldest_key, _)) = cache.iter() |
| 79 | .min_by_key(|(_, entry)| entry.last_accessed) { |
| 80 | let oldest_key = oldest_key.clone(); |
| 81 | cache.remove(&oldest_key); |
| 82 | } |
| 83 | |
| 84 | cache.insert(key, CacheEntry { |
| 85 | value, |
| 86 | last_accessed: Instant::now(), |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | pub fn invalidate(&self, key: &K) { |
| 91 | self.cache.write().unwrap().remove(key); |