pub struct BlockCache { /* private fields */ }Expand description
An in-memory cache for raw block data with LRU/LFU/TTL eviction and size-based capacity limits.
Implementations§
Source§impl BlockCache
impl BlockCache
Sourcepub fn new(config: CacheConfig) -> Self
pub fn new(config: CacheConfig) -> Self
Creates a new BlockCache with the given CacheConfig.
Sourcepub fn get(&mut self, cid: &str, now_secs: u64) -> Option<&[u8]>
pub fn get(&mut self, cid: &str, now_secs: u64) -> Option<&[u8]>
Looks up a block by CID.
- Hit (fresh): updates
last_accessed_secsandaccess_count, incrementsstats.hits, and returns a slice of the data. - Hit (expired): removes the entry, increments
stats.missesandstats.expired_evictions, and returnsNone. - Miss: increments
stats.missesand returnsNone.
Sourcepub fn insert(&mut self, cid: String, data: Vec<u8>, now_secs: u64)
pub fn insert(&mut self, cid: String, data: Vec<u8>, now_secs: u64)
Inserts a new block into the cache.
If the cache is at or above capacity (entry count or byte budget),
entries are evicted according to CacheConfig::policy until the new
block fits. If the block itself exceeds max_bytes on its own, all
existing entries are evicted and the oversized block is still inserted
(single-block cache).
Sourcepub fn evict_one(&mut self, now_secs: u64) -> bool
pub fn evict_one(&mut self, now_secs: u64) -> bool
Evicts a single entry according to the configured policy.
Returns true if an entry was removed, false if the cache was already
empty.
Sourcepub fn evict_expired(&mut self, now_secs: u64) -> usize
pub fn evict_expired(&mut self, now_secs: u64) -> usize
Removes all expired entries from the cache in one pass.
Returns the number of entries that were removed.
Sourcepub fn remove(&mut self, cid: &str) -> bool
pub fn remove(&mut self, cid: &str) -> bool
Explicitly removes a cached entry by CID.
Returns true if the entry existed and was removed.
Sourcepub fn contains(&self, cid: &str, now_secs: u64) -> bool
pub fn contains(&self, cid: &str, now_secs: u64) -> bool
Returns true if a non-expired entry for cid is currently in the
cache. Unlike get this method does not update any
access metadata and does not remove expired entries.
Sourcepub fn stats(&self) -> &CacheStats
pub fn stats(&self) -> &CacheStats
Returns a reference to the cumulative CacheStats.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BlockCache
impl RefUnwindSafe for BlockCache
impl Send for BlockCache
impl Sync for BlockCache
impl Unpin for BlockCache
impl UnsafeUnpin for BlockCache
impl UnwindSafe for BlockCache
Blanket Implementations§
impl<T> Allocation for T
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