Skip to content

Commit

Permalink
0.49.2
Browse files Browse the repository at this point in the history
treat DiskCache::cache_get deserialization failures as non-existent
values
  • Loading branch information
jaemk committed Feb 24, 2024
1 parent 5a0583b commit 738225b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cached"
version = "0.49.1"
version = "0.49.2"
authors = ["James Kominick <[email protected]>"]
description = "Generic cache implementations and simplified function memoization"
repository = "https://github.com/jaemk/cached"
Expand Down
16 changes: 9 additions & 7 deletions src/stores/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ where
let seconds = self.seconds;
let refresh = self.refresh;
let update = |old: Option<&[u8]>| -> Option<Vec<u8>> {
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::<CachedDiskValue<V>>(old)
.expect("error deserializing cached disk value");
let mut cached = match rmp_serde::from_slice::<CachedDiskValue<V>>(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))
Expand All @@ -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::<CachedDiskValue<V>>(&data)?;
Ok(Some(cached.value))
} else {
Expand Down

0 comments on commit 738225b

Please sign in to comment.