Don't panic if the duration is negative in try_get_or_insert (#3413)

This commit is contained in:
Evan Almloff authored and GitHub committed 2024-12-20 12:02:07 -08:00
1 parent ecf8d16f3e
commit 21fdecd219
1 file changed
+4 -1
+4 -1
View File
@@ -55,12 +55,15 @@ impl InMemoryCache {
let age = elapsed.num_seconds(); let age = elapsed.num_seconds();
// The cache entry is out of date, so we need to remove it. // The cache entry is out of date, so we need to remove it.
if let Some(invalidate_after) = self.invalidate_after { if let Some(invalidate_after) = self.invalidate_after {
if elapsed.to_std().unwrap() > invalidate_after { // If we can't convert to a std duration, the duration is negative and hasn't elapsed yet.
if let Ok(std_elapsed) = elapsed.to_std() {
if std_elapsed > invalidate_after {
tracing::trace!("memory cache out of date"); tracing::trace!("memory cache out of date");
memory_cache.pop(route); memory_cache.pop(route);
return Ok(None); return Ok(None);
} }
} }
}
// We need to reborrow because we may have invalidated the lifetime if the route was removed. // We need to reborrow because we may have invalidated the lifetime if the route was removed.
// We know it wasn't because we returned... but rust doesn't understand that. // We know it wasn't because we returned... but rust doesn't understand that.