pub struct CachePolicy { /* private fields */ }
Expand description

Identifies when responses can be reused from a cache, taking into account HTTP RFC 7234 rules for user agents and shared caches. It’s aware of many tricky details such as the Vary header, proxy revalidation, and authenticated responses.

Implementations§

source§

impl CachePolicy

source

pub fn new<Req: RequestLike, Res: ResponseLike>(req: &Req, res: &Res) -> Self

Cacheability of an HTTP response depends on how it was requested, so both request and response are required to create the policy.

source

pub fn new_options<Req: RequestLike, Res: ResponseLike>( req: &Req, res: &Res, response_time: SystemTime, opts: CacheOptions ) -> Self

Caching with customized behavior. See CacheOptions for details.

response_time is a timestamp when the response has been received, usually SystemTime::now().

source

pub fn is_storable(&self) -> bool

Returns true if the response can be stored in a cache. If it’s false then you MUST NOT store either the request or the response.

source

pub fn before_request<Req: RequestLike>( &self, req: &Req, now: SystemTime ) -> BeforeRequest

Returns whether the cached response is still fresh in the context of the new request.

If it returns Fresh, then the given request matches the original response this cache policy has been created with, and the response can be reused without contacting the server.

If it returns Stale, then the response may not be matching at all (e.g. it’s for a different URL or method), or may require to be refreshed first. Either way, the new request’s headers will have been updated for sending it to the origin server.

source

pub fn age(&self, now: SystemTime) -> Duration

Tells how long the response has been sitting in cache(s).

Value of the Age header, updated for the current time.

source

pub fn time_to_live(&self, now: SystemTime) -> Duration

Returns approximate time until the response becomes stale (i.e. not fresh). This is the correct way of getting the current max-age value.

After that time (when time_to_live() == Duration::ZERO) the response might not be usable without revalidation. However, there are exceptions, e.g. a client can explicitly allow stale responses, so always check with before_request().

If you’re storing responses in a cache/database, keep them approximately for the time_to_live duration plus some extra time to allow for revalidation (an expired response is still useful).

source

pub fn is_stale(&self, now: SystemTime) -> bool

Stale responses shouldn’t be used without contacting the server (revalidation)

source

pub fn after_response<Req: RequestLike, Res: ResponseLike>( &self, request: &Req, response: &Res, response_time: SystemTime ) -> AfterResponse

Creates CachePolicy with information combined from the previews response, and the new revalidation response.

Returns {policy, modified} where modified is a boolean indicating whether the response body has been modified, and old cached body can’t be used.

Trait Implementations§

source§

impl Clone for CachePolicy

source§

fn clone(&self) -> CachePolicy

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for CachePolicy

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for CachePolicy

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for CachePolicy

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

§

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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,