pub struct LruBlockCache { /* private fields */ }Expand description
Bounded LRU. Uses a doubly-linked list of node indices plus a hashmap for
O(1) lookup. The list is implemented over a Vec<Node> to avoid the
allocator overhead of Box<Node> per insert.
Single-threaded under the hood; the trait advertises Sync via an
internal mutex so the cache can be shared across reader threads.
Implementations§
Source§impl LruBlockCache
impl LruBlockCache
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Examples found in repository?
More examples
examples/sample_app.rs (line 312)
309fn block_cache_read_path() {
310 use subms_lsm_tree::{Block, BlockCache, BlockKey, LruBlockCache};
311 println!("\n== block-cache-integration: read-side block cache ==");
312 let cache = LruBlockCache::new(2);
313 let hot = BlockKey::new(1, 0);
314
315 assert!(cache.get(&hot).is_none(), "cold: a miss");
316 cache.put(hot, Block::from(b"AAPL block".as_slice()));
317 let served = cache.get(&hot).expect("warm: a hit");
318 println!(
319 " {} hit / {} miss after one warm read",
320 cache.hits(),
321 cache.misses()
322 );
323 assert_eq!(&*served, b"AAPL block", "the cached payload is served");
324
325 // A third distinct block evicts the least-recently-used entry (cap 2).
326 cache.put(BlockKey::new(2, 0), Block::from(b"MSFT block".as_slice()));
327 cache.put(BlockKey::new(3, 0), Block::from(b"GOOG block".as_slice()));
328 assert!(
329 cache.get(&hot).is_none(),
330 "coldest block evicted at capacity"
331 );
332}pub fn capacity(&self) -> usize
Sourcepub fn hits(&self) -> u64
pub fn hits(&self) -> u64
Examples found in repository?
examples/sample_app.rs (line 320)
309fn block_cache_read_path() {
310 use subms_lsm_tree::{Block, BlockCache, BlockKey, LruBlockCache};
311 println!("\n== block-cache-integration: read-side block cache ==");
312 let cache = LruBlockCache::new(2);
313 let hot = BlockKey::new(1, 0);
314
315 assert!(cache.get(&hot).is_none(), "cold: a miss");
316 cache.put(hot, Block::from(b"AAPL block".as_slice()));
317 let served = cache.get(&hot).expect("warm: a hit");
318 println!(
319 " {} hit / {} miss after one warm read",
320 cache.hits(),
321 cache.misses()
322 );
323 assert_eq!(&*served, b"AAPL block", "the cached payload is served");
324
325 // A third distinct block evicts the least-recently-used entry (cap 2).
326 cache.put(BlockKey::new(2, 0), Block::from(b"MSFT block".as_slice()));
327 cache.put(BlockKey::new(3, 0), Block::from(b"GOOG block".as_slice()));
328 assert!(
329 cache.get(&hot).is_none(),
330 "coldest block evicted at capacity"
331 );
332}Sourcepub fn misses(&self) -> u64
pub fn misses(&self) -> u64
Examples found in repository?
examples/sample_app.rs (line 321)
309fn block_cache_read_path() {
310 use subms_lsm_tree::{Block, BlockCache, BlockKey, LruBlockCache};
311 println!("\n== block-cache-integration: read-side block cache ==");
312 let cache = LruBlockCache::new(2);
313 let hot = BlockKey::new(1, 0);
314
315 assert!(cache.get(&hot).is_none(), "cold: a miss");
316 cache.put(hot, Block::from(b"AAPL block".as_slice()));
317 let served = cache.get(&hot).expect("warm: a hit");
318 println!(
319 " {} hit / {} miss after one warm read",
320 cache.hits(),
321 cache.misses()
322 );
323 assert_eq!(&*served, b"AAPL block", "the cached payload is served");
324
325 // A third distinct block evicts the least-recently-used entry (cap 2).
326 cache.put(BlockKey::new(2, 0), Block::from(b"MSFT block".as_slice()));
327 cache.put(BlockKey::new(3, 0), Block::from(b"GOOG block".as_slice()));
328 assert!(
329 cache.get(&hot).is_none(),
330 "coldest block evicted at capacity"
331 );
332}Trait Implementations§
Source§impl BlockCache for LruBlockCache
impl BlockCache for LruBlockCache
Auto Trait Implementations§
impl !Freeze for LruBlockCache
impl RefUnwindSafe for LruBlockCache
impl Send for LruBlockCache
impl Sync for LruBlockCache
impl Unpin for LruBlockCache
impl UnsafeUnpin for LruBlockCache
impl UnwindSafe for LruBlockCache
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