Skip to main content

L1Cache

Struct L1Cache 

Source
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

Source

pub fn new(capacity: usize) -> Self

Create a new L1 cache that holds at most capacity entries.

Source

pub fn get(&self, key: &str) -> Option<Vec<u8>>

Retrieve cached bytes by key, or None if absent or expired.

Source

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).

Source

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.

Source

pub fn delete(&self, key: &str)

Remove an entry by key.

Source

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.

Source

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§

Source§

impl Clone for L1Cache

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more