pub struct L1Cache<S: CacheSource> { /* private fields */ }Expand description
In-process L1 cache that fronts a CacheSource with a DashMap.
get serves fresh keys from memory and falls back to the wrapped store on a
miss or once an entry’s TTL elapses, repopulating the cache on the way out
(cache-aside). Concurrent misses for the same key collapse to a single
load call (stampede protection), so a burst of cold lookups never fans out
into N backend round-trips.
Implementations§
Source§impl<S: CacheSource> L1Cache<S>
impl<S: CacheSource> L1Cache<S>
Sourcepub fn new(inner: S, ttl: Duration) -> Self
pub fn new(inner: S, ttl: Duration) -> Self
Wrap inner, expiring cached entries ttl after insertion.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of entries currently held (including any past their TTL but not yet evicted). Intended for diagnostics, not control flow.
Sourcepub fn invalidate(&self, key: &S::Key) -> bool
pub fn invalidate(&self, key: &S::Key) -> bool
Drop the cached entry for key; returns whether one was present.
This is the hook the Epic C push-invalidation channel calls when the
Gateway reports that an agent’s policy changed: the next get reloads
from the source of truth rather than serving a stale entry.
Sourcepub async fn get(&self, key: S::Key) -> Result<S::Value>
pub async fn get(&self, key: S::Key) -> Result<S::Value>
Fetch the value for key, serving from cache when fresh.
Cache-aside: a hit clones out of the DashMap; a miss (or an expired
entry) loads from the wrapped store, populates the cache, and returns.
Stampede protection: the first caller to miss a key becomes the leader
and performs the single load; concurrent callers become followers,
wait on a shared Notify, then re-read the now-populated cache. The
inner store therefore sees exactly one call per key per miss window.