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 entry_count(&self) -> u64
pub fn entry_count(&self) -> u64
Number of entries currently held.
Runs moka’s pending housekeeping first, so expired and evicted entries are already gone from the count — the exact figure at the time of the call, not moka’s eventually-consistent estimate.
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.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for L1Cache
impl !UnwindSafe for L1Cache
impl Freeze for L1Cache
impl Send for L1Cache
impl Sync for L1Cache
impl Unpin for L1Cache
impl UnsafeUnpin for L1Cache
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more