diff --git a/nomos-services/data-availability/Cargo.toml b/nomos-services/data-availability/Cargo.toml index a4816c73f..2a60bbc41 100644 --- a/nomos-services/data-availability/Cargo.toml +++ b/nomos-services/data-availability/Cargo.toml @@ -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" diff --git a/nomos-services/data-availability/src/backend/memory_cache.rs b/nomos-services/data-availability/src/backend/memory_cache.rs new file mode 100644 index 000000000..8471f06ee --- /dev/null +++ b/nomos-services/data-availability/src/backend/memory_cache.rs @@ -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(Cache); + +impl BlobCache +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 + Send + '_> { + Box::new(self.0.iter().map(|t| t.1)) + } +}