Adds a new key-value pair to cache expiring at `now` + the TTL. This means that LRU entries might be evicted if required. If the key is already in the cache, the previous entry is returned. If the size of the entry is greater than the `memory_limit`, the value is not inserted.
(
&mut self,
key: &TableScopedPath,
value: CachedFileList,
now: Instant,
)
| 240 | /// If the key is already in the cache, the previous entry is returned. |
| 241 | /// If the size of the entry is greater than the `memory_limit`, the value is not inserted. |
| 242 | fn put( |
| 243 | &mut self, |
| 244 | key: &TableScopedPath, |
| 245 | value: CachedFileList, |
| 246 | now: Instant, |
| 247 | ) -> Option<CachedFileList> { |
| 248 | let entry = ListFilesEntry::try_new(value, self.ttl, now)?; |
| 249 | let entry_size = entry.size_bytes; |
| 250 | |
| 251 | // no point in trying to add this value to the cache if it cannot fit entirely |
| 252 | if entry_size > self.memory_limit { |
| 253 | return None; |
| 254 | } |
| 255 | |
| 256 | // if the key is already in the cache, the old value is removed |
| 257 | let old_value = self.lru_queue.put(key.clone(), entry); |
| 258 | self.memory_used += entry_size; |
| 259 | |
| 260 | if let Some(entry) = &old_value { |
| 261 | self.memory_used -= entry.size_bytes; |
| 262 | } |
| 263 | |
| 264 | self.evict_entries(); |
| 265 | |
| 266 | old_value.map(|v| v.metas) |
| 267 | } |
| 268 | |
| 269 | /// Evicts entries from the LRU cache until `memory_used` is lower than `memory_limit`. |
| 270 | fn evict_entries(&mut self) { |