Skip to content
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

Add remove method to return Option<V> for unsync cache #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/unsync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ where
}
}

/// Discards any cached value for the key, returning the cached value.
///
/// The key may be any borrowed form of the cache's key type, but `Hash` and `Eq`
/// on the borrowed form _must_ match those for the key type.
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where
Rc<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.evict_expired_if_needed();
self.evict_lru_entries();

if let Some(mut entry) = self.cache.remove(key) {
let weight = entry.policy_weight();
self.deques.unlink_ao(&mut entry);
crate::unsync::deques::Deques::unlink_wo(&mut self.deques.write_order, &mut entry);
self.saturating_sub_from_total_weight(weight as u64);
Some(entry.value)
}
else {
None
}
}

/// Discards all cached values.
///
/// Like the `invalidate` method, this method does not clear the historic
Expand Down
Loading