pub struct LocalResponseCache {
entries: DashMap<String, (CachedResponse, Instant)>,
capacity: AtomicUsize,
metrics: Option<Arc<GatewayMetrics>>,
}Expand description
Process-local cache. Shared by every policy: local node in the gateway.
Fields§
§entries: DashMap<String, (CachedResponse, Instant)>§capacity: AtomicUsizeRead on every insert; set once at startup from cache.max_entries.
metrics: Option<Arc<GatewayMetrics>>Set once at construction (PluginResources::new); None disables
recording (unit tests). Used only to count evictions — hits, misses
and errors are metered by the proxy-cache plugin, which knows the
store label this backend does not have.
Implementations§
Source§impl LocalResponseCache
impl LocalResponseCache
Sourcepub fn new(metrics: Option<Arc<GatewayMetrics>>) -> Self
pub fn new(metrics: Option<Arc<GatewayMetrics>>) -> Self
Creates the process-local cache, optionally wired to the metrics
registry so make_room can count evictions.
Sourcepub fn set_capacity(&self, max_entries: usize)
pub fn set_capacity(&self, max_entries: usize)
Sets the entry bound. Called once at startup, before traffic.
Sourcefn make_room(&self, capacity: usize)
fn make_room(&self, capacity: usize)
Makes room for one more entry.
Expired entries go first, since they are already worthless. If that is not enough, the entries expiring soonest go next — deliberately not an LRU (no LRU crate is in the dependency tree, and adding one for this is not worth the supply-chain surface). For a cache whose entries all carry TTLs this discards what was about to become useless anyway; the cost is that a hot short-TTL entry loses to a cold long-TTL one.
The live-entry eviction reclaims down to a low-water mark
(capacity - capacity / RECLAIM_FRACTION) in one sorted pass, rather
than removing exactly one entry via a fresh per-entry min_by_key
scan. Once the cache is saturated, a per-entry scan would make every
put pay an O(n) cost for the life of the process; amortising the
sort over a batch of evictions means most inserts, most of the time,
find room already waiting and skip straight to the insert.
Trait Implementations§
Source§impl Default for LocalResponseCache
impl Default for LocalResponseCache
Source§impl ResponseCache for LocalResponseCache
impl ResponseCache for LocalResponseCache
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<CachedResponse>, CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn put<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
entry: &'life2 CachedResponse,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<(), CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Source§fn purge<'life0, 'life1, 'async_trait>(
&'life0 self,
id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<u64, CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn purge<'life0, 'life1, 'async_trait>(
&'life0 self,
id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<u64, CacheError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
id and returns how many. Read moreAuto Trait Implementations§
impl !Freeze for LocalResponseCache
impl !RefUnwindSafe for LocalResponseCache
impl !UnwindSafe for LocalResponseCache
impl Send for LocalResponseCache
impl Sync for LocalResponseCache
impl Unpin for LocalResponseCache
impl UnsafeUnpin for LocalResponseCache
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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