Skip to content

Commit

Permalink
feat(pkarr): add Cache::capacity() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Feb 17, 2025
1 parent e712fd2 commit 85bd27e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkarr/src/client/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl From<crate::PublicKey> for CacheKey {

/// A trait for a [SignedPacket]s cache for Pkarr [Client][crate::Client].
pub trait Cache: Debug + Send + Sync + DynClone {
/// Returns the maximum capacity of [SignedPacket]s allowed in this cache.
fn capacity(&self) -> usize {
// backward compatibility
0
}
/// Returns the number of [SignedPacket]s in this cache.
fn len(&self) -> usize;
/// Returns true if this cache is empty.
Expand Down Expand Up @@ -73,6 +78,14 @@ impl InMemoryCache {
}

impl Cache for InMemoryCache {
fn capacity(&self) -> usize {
self.inner
.read()
.expect("InMemoryCache RwLock")
.cap()
.into()
}

fn len(&self) -> usize {
self.inner.read().expect("InMemoryCache RwLock").len()
}
Expand Down
4 changes: 4 additions & 0 deletions pkarr/src/extra/lmdb_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ fn update_lru(
}

impl Cache for LmdbCache {
fn capacity(&self) -> usize {
self.capacity
}

fn len(&self) -> usize {
match self.internal_len() {
Ok(result) => result,
Expand Down

0 comments on commit 85bd27e

Please # to comment.