struct BlockLruCache {
map: HashMap<u64, u32>,
nodes: HashMap<u32, LruNode>,
head: Option<u32>,
tail: Option<u32>,
next_id: u32,
max_size: usize,
hits: u64,
misses: u64,
}Expand description
O(1) LRU cache implementation using HashMap + doubly-linked list This implementation uses indices instead of raw pointers to be thread-safe
Fields§
§map: HashMap<u64, u32>Map from block number to node ID
nodes: HashMap<u32, LruNode>Storage for all nodes
head: Option<u32>Head of doubly-linked list (most recently used)
tail: Option<u32>Tail of doubly-linked list (least recently used)
next_id: u32Next available node ID
max_size: usizeMaximum cache size
hits: u64Cache statistics
misses: u64Implementations§
Source§impl BlockLruCache
impl BlockLruCache
fn new(max_size: usize) -> Self
fn get(&mut self, block_num: u64) -> Option<Vec<u8>>
Sourcefn move_to_head(&mut self, node_id: u32)
fn move_to_head(&mut self, node_id: u32)
Move node to head of LRU list (O(1))
Sourcefn remove_from_list(&mut self, node_id: u32)
fn remove_from_list(&mut self, node_id: u32)
Remove node from doubly-linked list (O(1))
Sourcefn add_to_head(&mut self, node_id: u32)
fn add_to_head(&mut self, node_id: u32)
Add node to head of list (O(1))
fn insert(&mut self, block_num: u64, block_data: Vec<u8>)
fn remove(&mut self, block_num: u64)
fn len(&self) -> usize
Sourcefn get_stats(&self) -> (u64, u64, usize)
fn get_stats(&self) -> (u64, u64, usize)
Get cache statistics for debugging and performance analysis
Sourcefn print_stats(&self, cache_name: &str)
fn print_stats(&self, cache_name: &str)
Print cache statistics