pub struct L1Cache { /* private fields */ }Expand description
In-process LRU cache with per-entry TTL, backed by moka.
Used as the L1 layer in the dual-layer cache architecture. Clone is
cheap and shares the underlying store (moka is internally referenced).
Implementations§
Source§impl L1Cache
impl L1Cache
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new L1 cache with the given maximum entry capacity.
Sourcepub fn get(&self, key: &str) -> Option<Vec<u8>>
pub fn get(&self, key: &str) -> Option<Vec<u8>>
Retrieve cached bytes by key, or None if absent or expired.
Sourcepub fn get_with_swr(&self, key: &str, threshold_ratio: f64) -> L1SwrRead
pub fn get_with_swr(&self, key: &str, threshold_ratio: f64) -> L1SwrRead
Retrieve cached bytes with stale-while-revalidate classification.
An entry is fresh until it has lived threshold_ratio of its own
TTL (±10% jitter, drawn once when the entry is inserted so a hot read
never touches the entropy source), stale from then until hard
expiry, and a miss after that. The freshness window
derives from the TTL the entry was inserted with: a direct write
carries the caller’s full TTL, an L2 backfill carries the capped
backfill TTL — see CacheKit’s L1 documentation.
Semantics mirror cachekit-py’s swr_threshold_ratio (elapsed
lifetime > ratio × TTL ⇒ stale). Hard expiry is enforced by moka:
an expired entry is never returned, so SWR can never serve past it.
This is a pure read — it does not track refresh state. Callers own
refresh scheduling and deduplication (the #[cachekit] macro uses
CacheKit::single_flight).
Sourcepub fn set(&self, key: &str, value: &[u8], ttl: Duration)
pub fn set(&self, key: &str, value: &[u8], ttl: Duration)
Insert or overwrite an entry with the given TTL.
Sourcepub fn run_pending_tasks(&self)
pub fn run_pending_tasks(&self)
Drive moka’s internal eviction machinery. Useful in tests to force pending invalidations and expiry checks to complete synchronously.