Skip to content

Commit

Permalink
Add remove method for unsync cache
Browse files Browse the repository at this point in the history
  • Loading branch information
JokerXiL authored and tatsuya6502 committed Jan 12, 2025
1 parent 74f6b2e commit 6cb1d78
Showing 1 changed file with 24 additions and 0 deletions.
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

0 comments on commit 6cb1d78

Please sign in to comment.