Module block_cache

Module block_cache 

Source
Expand description

LRU cache for blocks

This module provides an efficient LRU (Least Recently Used) cache for blocks, enabling fast repeated access to frequently used blocks without hitting storage.

§Features

  • Thread-safe - Can be safely shared across threads
  • LRU eviction - Automatically evicts least recently used blocks when full
  • Size limits - Configurable maximum cache size (in bytes and/or block count)
  • Statistics tracking - Monitor cache hits, misses, and evictions
  • Zero-copy - Blocks are reference-counted, so cloning is cheap

§Example

use ipfrs_core::{BlockCache, Block};
use bytes::Bytes;

// Create a cache with 10MB limit
let cache = BlockCache::new(10 * 1024 * 1024, None);

// Insert a block
let block = Block::new(Bytes::from_static(b"Hello, cache!")).unwrap();
cache.insert(block.clone());

// Retrieve the block
if let Some(cached_block) = cache.get(block.cid()) {
    println!("Cache hit!");
}

// Check statistics
let stats = cache.stats();
println!("Hits: {}, Misses: {}", stats.hits, stats.misses);

Structs§

BlockCache
LRU cache for blocks
CacheStats
Statistics for block cache operations