Skip to content

Commit

Permalink
Implement mocka backend
Browse files Browse the repository at this point in the history
  • Loading branch information
danielSanchezQ committed Sep 7, 2023
1 parent 7995abb commit ffd460a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nomos-services/data-availability/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
async-trait = "0.1"
futures = "0.3"
moka = { version = "0.11", features = ["future"] }
nomos-core = { path = "../../nomos-core" }
nomos-network = { path = "../network" }
overwatch-rs = { git = "https://github.com/logos-co/Overwatch", branch = "main" }
tracing = "0.1"
Expand Down
41 changes: 41 additions & 0 deletions nomos-services/data-availability/src/backend/memory_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use moka::future::{Cache, CacheBuilder};
use nomos_core::blob::Blob;
use std::time::Duration;

pub struct BlobCacheSettings {
max_capacity: usize,
evicting_period: Duration,
}

pub struct BlobCache<H, B>(Cache<H, B>);

impl<B> BlobCache<B::Hash, B>
where
B: Clone + Blob + Send + Sync + 'static,
B::Hash: Send + Sync + 'static,
{
pub fn new(settings: BlobCacheSettings) -> Self {
let BlobCacheSettings {
max_capacity,
evicting_period,
} = settings;
let cache = CacheBuilder::new(max_capacity as u64)
.time_to_live(evicting_period)
// can we leverage this to evict really old blobs?
.time_to_idle(evicting_period)
.build();
Self(cache)
}

pub async fn add(&self, blob: B) {
self.0.insert(blob.hash(), blob).await
}

pub async fn remove(&self, hash: &B::Hash) {
self.0.remove(hash).await;
}

pub async fn pending_blobs(&self) -> Box<dyn Iterator<Item = B> + Send + '_> {
Box::new(self.0.iter().map(|t| t.1))
}
}

0 comments on commit ffd460a

Please sign in to comment.