pub enum CachePolicy {
NoCache,
Ttl {
ttl_ms: u64,
},
StaleWhileRevalidate {
ttl_ms: u64,
stale_ms: u64,
},
}core only.Expand description
How cached data is treated when a query is accessed.
NoCache: Always fetch fresh data.Ttl: Use cached data if fresh (within TTL).StaleWhileRevalidate: Return stale data immediately while fetching fresh data in the background.
Variants§
NoCache
Never cache — always fetch fresh data.
Ttl
Cache with a time-to-live. Data is considered fresh within the TTL.
Note: ttl_ms should be greater than zero. A ttl_ms of 0 is
equivalent to NoCache for all practical purposes —
data is only “fresh” at the exact instant it is stored, so every
begin_request call triggers a new fetch. This is not validated at
runtime in release builds, but a debug_assert will fire in debug
builds if ttl_ms is zero.
StaleWhileRevalidate
Return stale data immediately while revalidating in the background.
Data within ttl_ms is served as a fresh cache hit (no refetch).
Data between ttl_ms and ttl_ms + stale_ms is served as stale data
and a background revalidation is triggered.
After ttl_ms + stale_ms, data is considered expired and a normal
fetch is performed (no stale data served).
Note: stale_ms should be greater than zero. Setting stale_ms
to 0 effectively disables the stale-while-revalidate feature,
degenerating to pure TTL behavior (the stale window is an empty set).
Both ttl_ms and stale_ms should be greater than zero. This is not
validated at runtime in release builds, but debug_asserts will fire
in debug builds if either value is zero.
Implementations§
Source§impl CachePolicy
impl CachePolicy
Sourcepub fn label(self) -> String
pub fn label(self) -> String
Human-readable label.
Sub-second values are shown with millisecond precision (e.g. “500ms”) rather than truncating to “0s” via integer division.
Thin wrapper around the Display impl that
allocates a String. Prefer format!("{policy}") or writing directly
to a formatter to avoid the heap allocation for log/diagnostic callers.
Sourcepub fn can_short_circuit(self) -> bool
pub fn can_short_circuit(self) -> bool
Whether this policy can short-circuit (return cached data without fetching).
Returns true for Ttl and StaleWhileRevalidate since both can serve
cached data when it is fresh (within the TTL window). The actual freshness
check is done separately in is_fresh.
Sourcepub fn can_serve_stale(self) -> bool
pub fn can_serve_stale(self) -> bool
Whether this policy allows serving stale data while revalidating.
Sourcepub fn stale_ms(self) -> Option<u64>
pub fn stale_ms(self) -> Option<u64>
The stale-while-revalidate window in milliseconds beyond TTL.
Returns None for policies that are not StaleWhileRevalidate.
Sourcepub fn total_valid_ms(self) -> Option<u64>
pub fn total_valid_ms(self) -> Option<u64>
Total valid window (TTL + stale) in milliseconds.
This is the maximum age at which data can still be served under this policy.
For Ttl, this equals ttl_ms. For StaleWhileRevalidate, it equals
ttl_ms + stale_ms. Returns None for NoCache.
On overflow (extremely large ttl_ms + stale_ms), saturates to u64::MAX,
effectively treating the data as indefinitely valid.
Sourcepub fn is_fresh(self, age_ms: u64) -> bool
pub fn is_fresh(self, age_ms: u64) -> bool
Whether the data is fresh (within the TTL window).
Returns false if the policy has no TTL or the data age exceeds TTL.
Sourcepub fn is_stale_but_serveable(self, age_ms: u64) -> bool
pub fn is_stale_but_serveable(self, age_ms: u64) -> bool
Whether the data is stale but still within the stale-while-revalidate window.
Data is “stale-but-serveable” when:
- The policy is
StaleWhileRevalidate - Data age is past TTL but within
ttl_ms + stale_ms
Sourcepub fn is_expired(self, age_ms: u64) -> bool
pub fn is_expired(self, age_ms: u64) -> bool
Whether the data is expired (past the total valid window).
Returns true if the data age exceeds the total valid window for this policy.
Trait Implementations§
Source§impl Clone for CachePolicy
impl Clone for CachePolicy
Source§fn clone(&self) -> CachePolicy
fn clone(&self) -> CachePolicy
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for CachePolicy
Source§impl Debug for CachePolicy
impl Debug for CachePolicy
Source§impl Default for CachePolicy
impl Default for CachePolicy
Source§impl<'de> Deserialize<'de> for CachePolicy
impl<'de> Deserialize<'de> for CachePolicy
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for CachePolicy
impl Display for CachePolicy
impl Eq for CachePolicy
Source§impl PartialEq for CachePolicy
impl PartialEq for CachePolicy
Source§impl Serialize for CachePolicy
impl Serialize for CachePolicy
impl StructuralPartialEq for CachePolicy
Auto Trait Implementations§
impl Freeze for CachePolicy
impl RefUnwindSafe for CachePolicy
impl Send for CachePolicy
impl Sync for CachePolicy
impl Unpin for CachePolicy
impl UnsafeUnpin for CachePolicy
impl UnwindSafe for CachePolicy
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().