pub struct ContentRoutingCache {
pub config: CacheConfig,
pub total_provider_lookups: u64,
pub total_hint_lookups: u64,
pub cache_hits: u64,
pub cache_misses: u64,
/* private fields */
}Expand description
Multi-tier cache for DHT content routing.
Three independent tiers:
- Provider tier – stores
CrcProviderRecords (multiple per CID). - Hint tier – stores one
RoutingHintper CID. - Negative tier – stores one
NegativeCacheEntryper CID.
Fields§
§config: CacheConfigCache configuration.
total_provider_lookups: u64Cumulative provider-tier lookup count.
total_hint_lookups: u64Cumulative hint-tier lookup count.
cache_hits: u64Cumulative cache hits (provider + hint tiers combined).
cache_misses: u64Cumulative cache misses (provider + hint tiers combined).
Implementations§
Source§impl ContentRoutingCache
impl ContentRoutingCache
Sourcepub fn new(config: CacheConfig) -> Self
pub fn new(config: CacheConfig) -> Self
Create a new cache with the given configuration.
Sourcepub fn add_provider(&mut self, record: CrcProviderRecord)
pub fn add_provider(&mut self, record: CrcProviderRecord)
Insert a provider record.
If the per-CID limit (MAX_PROVIDERS_PER_CID) would be exceeded the
oldest record (smallest last_provided) is evicted first. If the
global provider-CID limit would be exceeded the CID whose most-recent
provider record is oldest is removed entirely.
Sourcepub fn get_providers(&mut self, cid: &str, now: u64) -> Vec<&CrcProviderRecord>
pub fn get_providers(&mut self, cid: &str, now: u64) -> Vec<&CrcProviderRecord>
Return all non-expired provider records for cid.
Expired records are removed in place. Updates lookup and hit/miss counters.
Sourcepub fn remove_provider(&mut self, cid: &str, peer_id: &str) -> bool
pub fn remove_provider(&mut self, cid: &str, peer_id: &str) -> bool
Remove a specific provider record identified by (cid, peer_id).
Returns true if a record was removed.
Sourcepub fn add_hint(&mut self, hint: RoutingHint)
pub fn add_hint(&mut self, hint: RoutingHint)
Insert a routing hint.
If the hint-tier capacity would be exceeded the oldest hint (smallest
cached_at) is evicted first.
Sourcepub fn get_hint(&mut self, cid: &str, now: u64) -> Option<&RoutingHint>
pub fn get_hint(&mut self, cid: &str, now: u64) -> Option<&RoutingHint>
Return the routing hint for cid if it exists and has not expired.
If the hint exists but is expired it is removed and None is returned.
Updates lookup and hit/miss counters.
Sourcepub fn remove_hint(&mut self, cid: &str) -> bool
pub fn remove_hint(&mut self, cid: &str) -> bool
Remove the hint for cid. Returns true if a hint was present.
Sourcepub fn add_negative(&mut self, entry: NegativeCacheEntry)
pub fn add_negative(&mut self, entry: NegativeCacheEntry)
Insert a negative cache entry.
If the negative-tier capacity would be exceeded the oldest entry
(smallest not_found_at) is evicted first.
Sourcepub fn is_negative(&mut self, cid: &str, now: u64) -> bool
pub fn is_negative(&mut self, cid: &str, now: u64) -> bool
Returns true if there is a live (non-expired) negative entry for cid.
If the entry is expired it is removed.
Sourcepub fn remove_negative(&mut self, cid: &str) -> bool
pub fn remove_negative(&mut self, cid: &str) -> bool
Remove the negative entry for cid. Returns true if one existed.
Sourcepub fn evict_expired(&mut self, now: u64) -> usize
pub fn evict_expired(&mut self, now: u64) -> usize
Sweep all three tiers and remove expired entries.
Returns the total number of items removed.
Sourcepub fn has_content(&self, cid: &str) -> bool
pub fn has_content(&self, cid: &str) -> bool
Returns true if the provider tier holds at least one record for cid.
Note: this does not check expiry; call evict_expired or
get_providers to prune first if freshness matters.
Sourcepub fn cache_stats(&self) -> CacheStats
pub fn cache_stats(&self) -> CacheStats
Return a statistics snapshot.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ContentRoutingCache
impl RefUnwindSafe for ContentRoutingCache
impl Send for ContentRoutingCache
impl Sync for ContentRoutingCache
impl Unpin for ContentRoutingCache
impl UnsafeUnpin for ContentRoutingCache
impl UnwindSafe for ContentRoutingCache
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