BlockCache

Struct BlockCache 

Source
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

Source

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 bytes
  • max_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));
Source

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);
Source

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);
}
Source

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.

Source

pub fn remove(&self, cid: &Cid) -> Option<Block>

Remove a block from the cache

Source

pub fn clear(&self)

Clear all blocks from the cache

Source

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);
Source

pub fn len(&self) -> usize

Get the number of blocks currently in the cache

Source

pub fn is_empty(&self) -> bool

Check if the cache is empty

Source

pub fn size(&self) -> u64

Get the current total size of cached blocks in bytes

Source

pub fn max_size(&self) -> u64

Get the maximum cache size in bytes

Source

pub fn max_blocks(&self) -> Option<usize>

Get the maximum number of blocks (if configured)

Trait Implementations§

Source§

impl Clone for BlockCache

Source§

fn clone(&self) -> BlockCache

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,