pub struct L1Cache { /* private fields */ }Expand description
In-process cache with per-entry TTL, backed by quick_cache.
Used as the L1 layer in the dual-layer cache architecture. Clone is
cheap and shares the underlying store.
It never holds more than capacity entries. quick_cache evicts with a
scan-resistant policy (a CLOCK-Pro variant close to S3-FIFO): a run of
keys written once and never read does not flush the entries being read,
as it would under LRU. Capacity is split into shards, capacity / 128
rounded down to a power of two, from 1 to 64 (4 at the default 1,000),
and a full shard evicts even while others have room.
No read returns an expired entry. An expired entry leaves memory when a
read finds it, when eviction reaches it, or when Self::entry_count or
Self::run_pending_tasks sweeps; until then it counts against capacity.
Implementations§
Source§impl L1Cache
impl L1Cache
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 checked on every
read: 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.
An overwrite is a new entry: its TTL and freshness window start now.
Sourcepub fn entry_count(&self) -> u64
pub fn entry_count(&self) -> u64
Number of entries currently held.
Sweeps expired entries first (see Self::run_pending_tasks), so the
figure is exact at the time of the call. The sweep visits every entry:
poll this, don’t put it on a hot path.
Sourcepub fn run_pending_tasks(&self)
pub fn run_pending_tasks(&self)
Remove every expired entry now, one shard at a time.
Reads never return an expired entry, so this only frees the memory and the capacity they hold. It visits every entry, holding each shard’s write lock while it walks that shard.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for L1Cache
impl RefUnwindSafe for L1Cache
impl Send for L1Cache
impl Sync for L1Cache
impl Unpin for L1Cache
impl UnsafeUnpin for L1Cache
impl UnwindSafe 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