pub struct ResponseCache { /* private fields */ }Expand description
Response cache: LRU + TTL + in-flight deduplication.
Cheaply Clone-able — all clones share the same underlying Arc state.
A TTL of Duration::ZERO disables the cache entirely — get
always returns None and insert is a no-op.
Implementations§
Source§impl ResponseCache
impl ResponseCache
Sourcepub fn disabled() -> Self
pub fn disabled() -> Self
Create a disabled cache (TTL = Duration::ZERO).
All operations are no-ops. No memory is allocated for entries.
Sourcepub fn new(ttl: Duration, capacity: usize) -> Self
pub fn new(ttl: Duration, capacity: usize) -> Self
Create an enabled cache with the given ttl and LRU capacity.
Entries older than ttl are treated as expired on next access.
When the cache is full the least-recently-used entry is evicted.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
true when the cache is active (TTL > 0).
Sourcepub async fn get(&self, key: &str) -> Option<ServerStatus>
pub async fn get(&self, key: &str) -> Option<ServerStatus>
Try to return a non-expired cached ServerStatus for key.
Returns None when:
- the cache is disabled,
- no entry exists for
key, or - the entry has expired (age ≥ TTL).
On a hit, the returned status has cached = true and latency = 0.0.
Sourcepub async fn insert(&self, key: String, status: ServerStatus)
pub async fn insert(&self, key: String, status: ServerStatus)
Store a successful ServerStatus under key and broadcast it to
any tasks waiting on the in-flight channel for this key.
If the cache is disabled this is a no-op (but the broadcast still fires if an in-flight entry exists, releasing any waiting subscribers).
Sourcepub async fn insert_error(&self, key: &str, err: &McError)
pub async fn insert_error(&self, key: &str, err: &McError)
Notify in-flight subscribers of a failure for key, then free the slot.
The error message is broadcast as a String so subscribers can surface
a meaningful error without cloning the original McError.
Sourcepub async fn join_or_register(
&self,
key: &str,
) -> Option<Receiver<Result<ServerStatus, String>>>
pub async fn join_or_register( &self, key: &str, ) -> Option<Receiver<Result<ServerStatus, String>>>
Attempt to join an existing in-flight request for key, or register
this task as the designated pinger.
Returns:
Some(receiver)— another task is already pinging; wait on this channel for the result.None— this task is first; it must callinsertorinsert_errorwhen the ping completes.
Always returns None when the cache is disabled.
Trait Implementations§
Source§impl Clone for ResponseCache
impl Clone for ResponseCache
Source§fn clone(&self) -> ResponseCache
fn clone(&self) -> ResponseCache
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for ResponseCache
impl !UnwindSafe for ResponseCache
impl Freeze for ResponseCache
impl Send for ResponseCache
impl Sync for ResponseCache
impl Unpin for ResponseCache
impl UnsafeUnpin for ResponseCache
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