From 7cad7fd44afd61a09df18b3c0b7bb4eb63e14faf Mon Sep 17 00:00:00 2001 From: JokerXiL <58382820+JokerXiL@users.noreply.github.com> Date: Thu, 12 Sep 2024 16:24:24 +0700 Subject: [PATCH] Add remove method for unsync cache --- src/unsync/cache.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/unsync/cache.rs b/src/unsync/cache.rs index 00f88b2..1743a91 100644 --- a/src/unsync/cache.rs +++ b/src/unsync/cache.rs @@ -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(&mut self, key: &Q) -> Option + where + Rc: Borrow, + 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