diff options
author | Zaidoon Abd Al Hadi <[email protected]> | 2024-10-29 02:24:57 -0400 |
---|---|---|
committer | Yuchen Wu <[email protected]> | 2024-11-01 10:32:08 -0700 |
commit | ef1c9f121e9937720661d0aeecf65542f25c581f (patch) | |
tree | c9b18e81a11e1343fa60aea094481e6e3dcd2f7d | |
parent | 9a159869f2ec281d11c0a131d47a37a5bd0eba51 (diff) | |
download | pingora-ef1c9f121e9937720661d0aeecf65542f25c581f.tar.gz pingora-ef1c9f121e9937720661d0aeecf65542f25c581f.zip |
add support for removing items from pingora-memory-cache
-rw-r--r-- | .bleep | 2 | ||||
-rw-r--r-- | pingora-memory-cache/src/lib.rs | 31 |
2 files changed, 32 insertions, 1 deletions
@@ -1 +1 @@ -d715e2d7a34ba00f8872339d4596b2e1e68de304
\ No newline at end of file +bbcce2850360af2c4b60120842701a1dd86b35cc
\ No newline at end of file diff --git a/pingora-memory-cache/src/lib.rs b/pingora-memory-cache/src/lib.rs index b6e78bd..22f354d 100644 --- a/pingora-memory-cache/src/lib.rs +++ b/pingora-memory-cache/src/lib.rs @@ -130,6 +130,12 @@ impl<K: Hash, T: Clone + Send + Sync + 'static> MemoryCache<K, T> { self.store.put(hashed_key, node, 1); } + /// Remove a key from the cache if it exists. + pub fn remove(&self, key: &K) { + let hashed_key = self.hasher.hash_one(key); + self.store.remove(&hashed_key); + } + pub(crate) fn force_put(&self, key: &K, value: T, ttl: Option<Duration>) { if let Some(t) = ttl { if t.is_zero() { @@ -202,6 +208,31 @@ mod tests { } #[test] + fn test_put_get_remove() { + let cache: MemoryCache<i32, i32> = MemoryCache::new(10); + let (res, hit) = cache.get(&1); + assert_eq!(res, None); + assert_eq!(hit, CacheStatus::Miss); + cache.put(&1, 2, None); + cache.put(&3, 4, None); + cache.put(&5, 6, None); + let (res, hit) = cache.get(&1); + assert_eq!(res.unwrap(), 2); + assert_eq!(hit, CacheStatus::Hit); + cache.remove(&1); + cache.remove(&3); + let (res, hit) = cache.get(&1); + assert_eq!(res, None); + assert_eq!(hit, CacheStatus::Miss); + let (res, hit) = cache.get(&3); + assert_eq!(res, None); + assert_eq!(hit, CacheStatus::Miss); + let (res, hit) = cache.get(&5); + assert_eq!(res.unwrap(), 6); + assert_eq!(hit, CacheStatus::Hit); + } + + #[test] fn test_get_expired() { let cache: MemoryCache<i32, i32> = MemoryCache::new(10); let (res, hit) = cache.get(&1); |