Skip to content

Commit

Permalink
stack: return cached file if possible in get_or_update
Browse files Browse the repository at this point in the history
When we aren't replacing a file, prefer to return the cached file
after a call to `put`: if we lost the race and we can find a new
cached file, it's usually better to return what's now persisted on
disk, as though we had found it before attempting to fill the miss.
  • Loading branch information
pkhuong committed Sep 22, 2021
1 parent 30464f1 commit 294bed6
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/stack.rs
Expand Up @@ -373,11 +373,15 @@ impl Cache {

// Grab a read-only return value before publishing the file.
let path = tmp.path();
let ret = File::open(path)?;
let mut ret = File::open(path)?;
if replace {
cache.set(key, path)?;
} else {
cache.put(key, path)?;
// Return the now-cached file, if we can get it.
if let Ok(Some(file)) = cache.get(key) {
ret = file;
}
}

Ok(ret)
Expand Down

0 comments on commit 294bed6

Please sign in to comment.