Get a value from the cache
(&self, key: &K)
| 185 | |
| 186 | /// Get a value from the cache |
| 187 | pub fn get(&self, key: &K) -> Option<V> { |
| 188 | self.maybe_cleanup(); |
| 189 | |
| 190 | let mut entries = self.entries.write(); |
| 191 | let mut stats = self.stats.write(); |
| 192 | |
| 193 | if let Some(entry) = entries.get_mut(key) { |
| 194 | if entry.is_expired() { |
| 195 | // Remove expired entry |
| 196 | let size_bytes = entry.size_bytes; |
| 197 | entries.remove(key); |
| 198 | stats.total_entries = entries.len(); |
| 199 | stats.total_size_bytes = stats.total_size_bytes.saturating_sub(size_bytes); |
| 200 | stats.misses += 1; |
| 201 | stats.expired_evictions += 1; |
| 202 | |
| 203 | // Record memory deallocation for expired entry |
| 204 | global_memory_monitor().record_query_deallocation(size_bytes as u64); |
| 205 | |
| 206 | None |
| 207 | } else { |
| 208 | // Update access tracking |
| 209 | if self.config.enable_lru { |
| 210 | entry.access(); |
| 211 | } |
| 212 | stats.hits += 1; |
| 213 | Some(entry.value.clone()) |
| 214 | } |
| 215 | } else { |
| 216 | stats.misses += 1; |
| 217 | None |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Insert a value into the cache with default TTL |
| 222 | pub fn insert(&self, key: K, value: V) { |