sui_dockerfile_wrapper/
cache.rs1use std::collections::BTreeMap;
17use std::sync::Mutex;
18
19use async_trait::async_trait;
20use sui_cache::storage::StorageBackend;
21use sui_cache::CacheError;
22
23#[derive(Default)]
27pub struct MockCacheBackend {
28 narinfos: Mutex<BTreeMap<String, String>>,
29}
30
31impl MockCacheBackend {
32 #[must_use]
33 pub fn new() -> Self {
34 Self::default()
35 }
36
37 #[must_use]
45 pub fn with_entry(self, hash: &str, image_ref: &str) -> Self {
46 self.narinfos
47 .lock()
48 .expect("mock mutex poisoned")
49 .insert(hash.to_string(), image_ref.to_string());
50 self
51 }
52}
53
54#[async_trait]
55impl StorageBackend for MockCacheBackend {
56 async fn get_narinfo(&self, hash: &str) -> Result<Option<String>, CacheError> {
57 Ok(self.narinfos.lock().expect("mock mutex poisoned").get(hash).cloned())
58 }
59
60 async fn put_narinfo(&self, hash: &str, content: &str) -> Result<(), CacheError> {
61 self.narinfos
62 .lock()
63 .expect("mock mutex poisoned")
64 .insert(hash.to_string(), content.to_string());
65 Ok(())
66 }
67
68 async fn get_nar(&self, _path: &str) -> Result<Option<Vec<u8>>, CacheError> {
69 Ok(None)
70 }
71
72 async fn put_nar(&self, _path: &str, _data: &[u8]) -> Result<(), CacheError> {
73 Ok(())
74 }
75
76 async fn delete(&self, hash: &str) -> Result<(), CacheError> {
77 self.narinfos.lock().expect("mock mutex poisoned").remove(hash);
78 Ok(())
79 }
80
81 async fn list_narinfos(&self) -> Result<Vec<String>, CacheError> {
82 Ok(self.narinfos.lock().expect("mock mutex poisoned").keys().cloned().collect())
83 }
84}