BlockLruCache

Struct BlockLruCache 

Source
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: u32

Next available node ID

§max_size: usize

Maximum cache size

§hits: u64

Cache statistics

§misses: u64

Implementations§

Source§

impl BlockLruCache

Source

fn new(max_size: usize) -> Self

Source

fn get(&mut self, block_num: u64) -> Option<Vec<u8>>

Source

fn move_to_head(&mut self, node_id: u32)

Move node to head of LRU list (O(1))

Source

fn remove_from_list(&mut self, node_id: u32)

Remove node from doubly-linked list (O(1))

Source

fn add_to_head(&mut self, node_id: u32)

Add node to head of list (O(1))

Source

fn insert(&mut self, block_num: u64, block_data: Vec<u8>)

Source

fn remove(&mut self, block_num: u64)

Source

fn len(&self) -> usize

Source

fn get_stats(&self) -> (u64, u64, usize)

Get cache statistics for debugging and performance analysis

Source

fn print_stats(&self, cache_name: &str)

Print cache statistics

Auto Trait Implementations§

§

impl Freeze for BlockLruCache

§

impl RefUnwindSafe for BlockLruCache

§

impl Send for BlockLruCache

§

impl Sync for BlockLruCache

§

impl Unpin for BlockLruCache

§

impl UnwindSafe for BlockLruCache

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.