-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7995abb
commit ffd460a
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
nomos-services/data-availability/src/backend/memory_cache.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |