diff --git a/CHANGELOG.md b/CHANGELOG.md index 8945602..0232cfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ ## Changed ## Removed +## [0.49.2] +## Added +## Changed +- While handling cache refreshes in `DiskCache::cache_get`, treat deserialization failures as non-existent values +## Removed + ## [0.49.1] ## Added ## Changed diff --git a/Cargo.toml b/Cargo.toml index 3808022..0d34c1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cached" -version = "0.49.1" +version = "0.49.2" authors = ["James Kominick "] description = "Generic cache implementations and simplified function memoization" repository = "https://github.com/jaemk/cached" diff --git a/src/stores/disk.rs b/src/stores/disk.rs index a9caf48..33a85e7 100644 --- a/src/stores/disk.rs +++ b/src/stores/disk.rs @@ -185,16 +185,18 @@ where let seconds = self.seconds; let refresh = self.refresh; let update = |old: Option<&[u8]>| -> Option> { - if old.is_none() { - return None; - } - let old = old.unwrap(); + let old = old?; if seconds.is_none() { return Some(old.to_vec()); } let seconds = seconds.unwrap(); - let mut cached = rmp_serde::from_slice::>(old) - .expect("error deserializing cached disk value"); + let mut cached = match rmp_serde::from_slice::>(old) { + Ok(cached) => cached, + Err(_) => { + // unable to deserialize, treat it as not existing + return None; + } + }; if SystemTime::now() .duration_since(cached.created_at) .unwrap_or(Duration::from_secs(0)) @@ -211,7 +213,7 @@ where } }; - if let Some(data) = self.connection.update_and_fetch(&key, update)? { + if let Some(data) = self.connection.update_and_fetch(key, update)? { let cached = rmp_serde::from_slice::>(&data)?; Ok(Some(cached.value)) } else {