aboutsummaryrefslogtreecommitdiffhomepage
path: root/pingora-cache
diff options
context:
space:
mode:
authorpluveto <[email protected]>2024-02-29 09:19:24 +0800
committerAndrew Hauck <[email protected]>2024-04-05 11:46:20 -0700
commit59a9f93bd698a77c9f4a38e448dae6486a735323 (patch)
tree58a04527499b8f722b8e5fcc52711490f60df14b /pingora-cache
parent5fdf287c4d6a9ddc8a9caf3447cd27575c13a24c (diff)
downloadpingora-59a9f93bd698a77c9f4a38e448dae6486a735323.tar.gz
pingora-59a9f93bd698a77c9f4a38e448dae6486a735323.zip
tests(cache): add tests for purge method
Replicated-from: https://github.com/cloudflare/pingora/pull/18 Includes-commit: e5a3865e9dd5a381491b0e2ee8d2661f4fb59ffd Includes-commit: f4af9ad3a50f6ce37f1e936460357f9c2cd3d247 Includes-commit: bbfb1eb5ba7194791b1128c83f5d4d8e5719e4f2 Includes-commit: f74cf9f82aac6f9fb47962d0dcdc69add831cf2b
Diffstat (limited to 'pingora-cache')
-rw-r--r--pingora-cache/src/memory.rs55
1 files changed, 52 insertions, 3 deletions
diff --git a/pingora-cache/src/memory.rs b/pingora-cache/src/memory.rs
index 6a66577..afe57ae 100644
--- a/pingora-cache/src/memory.rs
+++ b/pingora-cache/src/memory.rs
@@ -307,12 +307,12 @@ impl Storage for MemCache {
}
async fn purge(&'static self, key: &CompactCacheKey, _trace: &SpanHandle) -> Result<bool> {
- // TODO: purge partial
-
// This usually purges the primary key because, without a lookup, variance key is usually
// empty
let hash = key.combined();
- Ok(self.cached.write().remove(&hash).is_some())
+ let temp_removed = self.temp.write().remove(&hash).is_some();
+ let cache_removed = self.cached.write().remove(&hash).is_some();
+ Ok(temp_removed || cache_removed)
}
async fn update_meta(
@@ -507,4 +507,53 @@ mod test {
let data = hit_handler2.read_body().await.unwrap();
assert!(data.is_none());
}
+
+ #[tokio::test]
+ async fn test_purge_partial() {
+ static MEM_CACHE: Lazy<MemCache> = Lazy::new(MemCache::new);
+ let cache = &MEM_CACHE;
+
+ let key = CacheKey::new("", "a", "1").to_compact();
+ let hash = key.combined();
+ let meta = (
+ "meta_key".as_bytes().to_vec(),
+ "meta_value".as_bytes().to_vec(),
+ );
+
+ let temp_obj = TempObject::new(meta);
+ cache.temp.write().insert(hash.clone(), temp_obj);
+
+ assert!(cache.temp.read().contains_key(&hash));
+
+ let result = cache.purge(&key, &Span::inactive().handle()).await;
+ assert!(result.is_ok());
+
+ assert!(!cache.temp.read().contains_key(&hash));
+ }
+
+ #[tokio::test]
+ async fn test_purge_complete() {
+ static MEM_CACHE: Lazy<MemCache> = Lazy::new(MemCache::new);
+ let cache = &MEM_CACHE;
+
+ let key = CacheKey::new("", "a", "1").to_compact();
+ let hash = key.combined();
+ let meta = (
+ "meta_key".as_bytes().to_vec(),
+ "meta_value".as_bytes().to_vec(),
+ );
+ let body = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
+ let cache_obj = CacheObject {
+ meta,
+ body: Arc::new(body),
+ };
+ cache.cached.write().insert(hash.clone(), cache_obj);
+
+ assert!(cache.cached.read().contains_key(&hash));
+
+ let result = cache.purge(&key, &Span::inactive().handle()).await;
+ assert!(result.is_ok());
+
+ assert!(!cache.cached.read().contains_key(&hash));
+ }
}