pub struct LruPeerDiscoveryCache { /* private fields */ }Expand description
LRU cache for recently discovered peer addresses with tick-based TTL.
Entries are stored in a Vec ordered by last access time — the front
(index 0) is the least recently used and the back is the most recently
used. When capacity is exceeded, the LRU entry (front) is evicted.
Implementations§
Source§impl LruPeerDiscoveryCache
impl LruPeerDiscoveryCache
Sourcepub fn new(config: DiscoveryCacheConfig) -> Self
pub fn new(config: DiscoveryCacheConfig) -> Self
Create a new cache with the given configuration.
Sourcepub fn put(
&mut self,
peer_id: &str,
addresses: Vec<String>,
source: &str,
ttl: Option<u64>,
)
pub fn put( &mut self, peer_id: &str, addresses: Vec<String>, source: &str, ttl: Option<u64>, )
Insert or update a peer entry.
If the peer already exists its addresses, source, and TTL are updated and the entry is moved to the back (MRU position). If the cache is at capacity, the LRU entry (front) is evicted first.
Sourcepub fn get(&mut self, peer_id: &str) -> Option<&CacheEntry>
pub fn get(&mut self, peer_id: &str) -> Option<&CacheEntry>
Retrieve a peer entry, moving it to the MRU position.
Returns None if the peer is not present or has expired (expired entries
are removed on access). Increments access_count and updates hit/miss
counters.
Sourcepub fn remove(&mut self, peer_id: &str) -> bool
pub fn remove(&mut self, peer_id: &str) -> bool
Remove a peer entry. Returns true if the entry existed.
Sourcepub fn contains(&self, peer_id: &str) -> bool
pub fn contains(&self, peer_id: &str) -> bool
Check whether a peer is in the cache without updating LRU order or access counters.
Sourcepub fn tick_cleanup(&mut self)
pub fn tick_cleanup(&mut self)
Advance the internal tick counter by one and remove all expired entries.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Number of entries currently in the cache.
Sourcepub fn hit_rate(&self) -> f64
pub fn hit_rate(&self) -> f64
Hit rate as hits / (hits + misses), or 0.0 when no lookups have been
performed.
Sourcepub fn entries_by_source(&self, source: &str) -> Vec<&CacheEntry>
pub fn entries_by_source(&self, source: &str) -> Vec<&CacheEntry>
Return references to all entries that match the given discovery source.
Sourcepub fn stats(&self) -> LruDiscoveryCacheStats
pub fn stats(&self) -> LruDiscoveryCacheStats
Snapshot of current cache statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for LruPeerDiscoveryCache
impl RefUnwindSafe for LruPeerDiscoveryCache
impl Send for LruPeerDiscoveryCache
impl Sync for LruPeerDiscoveryCache
impl Unpin for LruPeerDiscoveryCache
impl UnsafeUnpin for LruPeerDiscoveryCache
impl UnwindSafe for LruPeerDiscoveryCache
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more