Skip to main content

lru_ovo/
lib.rs

1//! 一个线程安全、异步友好的双层 LRU 缓存库。
2//! A thread-safe, async-friendly two-tier LRU cache library.
3//!
4//! 缓存由易失 LRU 层和带容量上限的持久 LRU 层组成。热 key 在命中次数达到阈值后
5//! 会从易失层提升到持久层;持久层容量满时同样按 LRU 淘汰。
6//! The cache consists of a volatile LRU tier and a bounded persistent LRU tier.
7//! Hot keys are promoted after their hit count reaches the threshold; the
8//! persistent tier also evicts by LRU when it reaches its capacity.
9//!
10//! ```
11//! use lru_ovo::{CacheConfig, LruCache};
12//!
13//! # fn block_on<F: std::future::Future>(future: F) -> F::Output {
14//! #     use std::task::{Context, Poll, Waker};
15//! #     let waker = Waker::noop();
16//! #     let mut context = Context::from_waker(waker);
17//! #     let mut future = Box::pin(future);
18//! #     loop {
19//! #         match future.as_mut().poll(&mut context) {
20//! #             Poll::Ready(output) => return output,
21//! #             Poll::Pending => std::thread::yield_now(),
22//! #         }
23//! #     }
24//! # }
25//!
26//! let cache = LruCache::with_config(CacheConfig::new(2, 2, 2));
27//! block_on(cache.insert("hot", 10));
28//! block_on(cache.insert("cold", 20));
29//!
30//! block_on(cache.get(&"hot"));
31//! block_on(cache.get(&"hot"));
32//!
33//! assert!(block_on(cache.is_persistent(&"hot")));
34//! assert_eq!(block_on(cache.total_len()), 2);
35//! ```
36
37pub mod core;
38
39pub use core::{CacheConfig, CacheConfigError, CacheIter, LruCache};