pub struct BlockCache { /* private fields */ }Expand description
LRU cache for blocks
This cache uses a Least Recently Used eviction policy to maintain a bounded set of frequently accessed blocks in memory. It’s thread-safe and can be shared across multiple threads.
§Example
use ipfrs_core::{BlockCache, Block};
use bytes::Bytes;
let cache = BlockCache::new(1024 * 1024, Some(100)); // 1MB or 100 blocks max
let block = Block::new(Bytes::from_static(b"cached data")).unwrap();
cache.insert(block.clone());
assert!(cache.get(block.cid()).is_some());Implementations§
Source§impl BlockCache
impl BlockCache
Sourcepub fn new(max_size_bytes: u64, max_blocks: Option<usize>) -> Self
pub fn new(max_size_bytes: u64, max_blocks: Option<usize>) -> Self
Create a new block cache
§Arguments
max_size_bytes- Maximum total size of cached blocks in bytesmax_blocks- Optional maximum number of blocks (None = unlimited)
§Example
use ipfrs_core::BlockCache;
// Cache up to 10MB of blocks
let cache = BlockCache::new(10 * 1024 * 1024, None);
// Cache up to 1MB or 100 blocks, whichever limit is hit first
let cache2 = BlockCache::new(1024 * 1024, Some(100));Sourcepub fn insert(&self, block: Block)
pub fn insert(&self, block: Block)
Insert a block into the cache
If the cache is full, the least recently used block will be evicted.
§Example
use ipfrs_core::{BlockCache, Block};
use bytes::Bytes;
let cache = BlockCache::new(1024 * 1024, None);
let block = Block::new(Bytes::from_static(b"data")).unwrap();
cache.insert(block);Sourcepub fn get(&self, cid: &Cid) -> Option<Block>
pub fn get(&self, cid: &Cid) -> Option<Block>
Get a block from the cache
Returns Some(block) if found, None otherwise. Updates the access
time for LRU tracking.
§Example
use ipfrs_core::{BlockCache, Block};
use bytes::Bytes;
let cache = BlockCache::new(1024 * 1024, None);
let block = Block::new(Bytes::from_static(b"data")).unwrap();
let cid = *block.cid();
cache.insert(block);
if let Some(cached) = cache.get(&cid) {
assert_eq!(cached.len(), 4);
}Sourcepub fn contains(&self, cid: &Cid) -> bool
pub fn contains(&self, cid: &Cid) -> bool
Check if the cache contains a block with the given CID
This does not update LRU access time.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get the current cache statistics
§Example
use ipfrs_core::{BlockCache, Block};
use bytes::Bytes;
let cache = BlockCache::new(1024 * 1024, None);
let block = Block::new(Bytes::from_static(b"data")).unwrap();
cache.insert(block.clone());
cache.get(block.cid()); // Hit
cache.get(block.cid()); // Another hit
let stats = cache.stats();
assert_eq!(stats.hits, 2);
assert_eq!(stats.misses, 0);Sourcepub fn max_blocks(&self) -> Option<usize>
pub fn max_blocks(&self) -> Option<usize>
Get the maximum number of blocks (if configured)
Trait Implementations§
Source§impl Clone for BlockCache
impl Clone for BlockCache
Source§fn clone(&self) -> BlockCache
fn clone(&self) -> BlockCache
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for BlockCache
impl RefUnwindSafe for BlockCache
impl Send for BlockCache
impl Sync for BlockCache
impl Unpin for BlockCache
impl UnwindSafe for BlockCache
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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