-
Notifications
You must be signed in to change notification settings - Fork 706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch cache to custom without write-lock for reads. #5576
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2020-2022 the Kubeapps contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::{collections::HashMap, hash::Hash, sync::RwLock}; | ||
|
||
/// An unlimited cache that can be RwLocked across threads. | ||
/// | ||
/// Importantly, checking the cache does not require a write-lock | ||
/// (unlike the [`Cached` trait's `cache_get`](https://github.com/jaemk/cached/blob/f5911dc3fbc03e1db9f87192eb854fac2ee6ac98/src/lib.rs#L203)) | ||
#[derive(Default)] | ||
struct LockableCache<K, V>(RwLock<HashMap<K, V>>); | ||
|
||
impl<K, V> LockableCache<K, V> { | ||
fn get(&self, k: &K) -> Option<V> | ||
where | ||
K: Eq + Hash, | ||
V: Eq + Hash + Clone, | ||
{ | ||
(*self.0.read().unwrap()) | ||
.get(k) | ||
.and_then(|v| Some((*v).clone())) | ||
} | ||
|
||
// insert is called in PrunableCache via a write lock guard, but compiler | ||
// doesn't see this, apparently. | ||
#[allow(dead_code)] | ||
fn insert(&self, k: K, v: V) | ||
where | ||
K: Eq + Hash, | ||
V: Eq + Hash, | ||
{ | ||
self.0.write().unwrap().insert(k, v); | ||
} | ||
|
||
#[cfg(test)] | ||
fn len(&self) -> usize { | ||
(*self.0.read().unwrap()).len() | ||
} | ||
} | ||
|
||
/// A cache that additionally prunes itself whenever it holds the write-lock. | ||
pub struct PruningCache<K, V> { | ||
cache: LockableCache<K, V>, | ||
prune_fn: fn(&(K, V)) -> bool, | ||
} | ||
|
||
impl<K, V> PruningCache<K, V> { | ||
pub fn new(f: fn(&(K, V)) -> bool) -> PruningCache<K, V> | ||
where | ||
K: Default, | ||
V: Default, | ||
{ | ||
PruningCache { | ||
cache: LockableCache::default(), | ||
prune_fn: f, | ||
} | ||
} | ||
|
||
pub fn get(&self, k: &K) -> Option<V> | ||
where | ||
K: Eq + Hash + Clone, | ||
V: Eq + Hash + Clone, | ||
{ | ||
// Only return the value from the cache if it should not have been | ||
// pruned. | ||
self.cache | ||
.get(k) | ||
.and_then(|v| (self.prune_fn)(&(k.clone(), v.clone())).then_some(v)) | ||
} | ||
|
||
// Prunes the cache while holding the write-lock during an insert. | ||
pub fn insert(&self, k: K, v: V) | ||
where | ||
K: Eq + Hash + Clone, | ||
V: Eq + Hash + Clone, | ||
{ | ||
let mut write_guard = self.cache.0.write().unwrap(); | ||
write_guard.insert(k, v); | ||
// Replace the cache with one where items are pruned. | ||
let cache = std::mem::take(&mut *write_guard); | ||
*write_guard = cache.into_iter().filter(self.prune_fn).collect(); | ||
} | ||
|
||
#[cfg(test)] | ||
pub fn len(&self) -> usize { | ||
self.cache.len() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_lockable_cache_get() { | ||
let c = LockableCache::default(); | ||
|
||
c.insert(1, 5); | ||
c.insert(2, 3); | ||
|
||
assert_eq!(c.get(&1), Some(5)); | ||
assert_eq!(c.get(&2), Some(3)); | ||
assert_eq!(c.len(), 2); | ||
} | ||
|
||
#[test] | ||
fn test_lockable_cache_overwrite() { | ||
let c = LockableCache::default(); | ||
|
||
c.insert(1, 5); | ||
assert_eq!(c.get(&1), Some(5)); | ||
|
||
c.insert(1, 6); | ||
assert_eq!(c.get(&1), Some(6)); | ||
assert_eq!(c.len(), 1); | ||
} | ||
|
||
#[test] | ||
fn test_pruning_cache_get() { | ||
// Create a cache that prunes all odd numbers (keeps evens only). | ||
let c = PruningCache::new(|(_k, v)| *v % 2 == 0); | ||
|
||
c.insert(1, 1); | ||
c.insert(2, 2); | ||
c.insert(3, 3); | ||
c.insert(4, 4); | ||
|
||
assert_eq!(c.get(&1), None); | ||
assert_eq!(c.get(&2), Some(2)); | ||
assert_eq!(c.get(&3), None); | ||
assert_eq!(c.get(&4), Some(4)); | ||
assert_eq!(c.len(), 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This struct is a NewType pattern which is a zero-cost abstraction (ie. no penalty at run-time) that defines a new type as a thin wrapper - a 1-tuple of another type, so that we can add our caching functions (
get
,insert
) to this type without needing an extra struct. So in this case, a LockableCache is really just a read-write lock wrapping a hash, but one which behaves like a cache.