pub struct ContentAddressableCache { /* private fields */ }Expand description
High-performance content-addressable cache.
Keyed by FNV-1a-derived CIDs; supports LRU, LFU, TTL-first, size-weighted, and tag-targeted eviction policies. Thread-safety must be provided by the caller.
Implementations§
Source§impl ContentAddressableCache
impl ContentAddressableCache
Sourcepub fn new(config: CacheConfig) -> Self
pub fn new(config: CacheConfig) -> Self
Create a new cache with the given configuration.
Sourcepub fn insert(
&mut self,
data: Vec<u8>,
ttl_us: Option<u64>,
tags: Vec<String>,
) -> Result<String, CacheError>
pub fn insert( &mut self, data: Vec<u8>, ttl_us: Option<u64>, tags: Vec<String>, ) -> Result<String, CacheError>
Insert data into the cache.
Computes the CID automatically; applies ttl_us (or the configured
default) and the given tags. Returns the derived CID on success.
Sourcepub fn insert_with_cid(
&mut self,
cid: String,
data: Vec<u8>,
ttl_us: Option<u64>,
tags: Vec<String>,
) -> Result<(), CacheError>
pub fn insert_with_cid( &mut self, cid: String, data: Vec<u8>, ttl_us: Option<u64>, tags: Vec<String>, ) -> Result<(), CacheError>
Insert data under a caller-supplied cid.
Verifies that the CID matches the data; returns
CacheError::CidMismatch otherwise.
Sourcepub fn get(&mut self, cid: &str) -> Result<&[u8], CacheError>
pub fn get(&mut self, cid: &str) -> Result<&[u8], CacheError>
Retrieve the data payload for cid.
Updates LRU position and increments access_count. Returns
CacheError::TtlExpired (and removes the entry) if the entry has
expired.
Sourcepub fn get_entry(&self, cid: &str) -> Result<&CacheEntry, CacheError>
pub fn get_entry(&self, cid: &str) -> Result<&CacheEntry, CacheError>
Retrieve the full CacheEntry for cid (read-only, does NOT update
LRU or access counts).
Sourcepub fn contains(&self, cid: &str) -> bool
pub fn contains(&self, cid: &str) -> bool
Return true when an entry for cid exists and has not expired.
Does not update LRU position.
Sourcepub fn remove(&mut self, cid: &str) -> Result<CacheEntry, CacheError>
pub fn remove(&mut self, cid: &str) -> Result<CacheEntry, CacheError>
Remove and return the entry for cid.
Sourcepub fn remove_by_tag(&mut self, tag: &str) -> Vec<String>
pub fn remove_by_tag(&mut self, tag: &str) -> Vec<String>
Remove all entries bearing tag and return their CIDs.
Sourcepub fn expire_ttl(&mut self) -> Vec<String>
pub fn expire_ttl(&mut self) -> Vec<String>
Scan the cache and remove all TTL-expired entries.
Returns the CIDs that were removed.
Sourcepub fn evict_to_fit(&mut self, needed_bytes: usize) -> Vec<String>
pub fn evict_to_fit(&mut self, needed_bytes: usize) -> Vec<String>
Evict entries per policy until needed_bytes additional bytes can be
accommodated without exceeding max_bytes.
Returns the CIDs evicted.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Return a snapshot of current cache statistics.
Auto Trait Implementations§
impl Freeze for ContentAddressableCache
impl RefUnwindSafe for ContentAddressableCache
impl Send for ContentAddressableCache
impl Sync for ContentAddressableCache
impl Unpin for ContentAddressableCache
impl UnsafeUnpin for ContentAddressableCache
impl UnwindSafe for ContentAddressableCache
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