Struct worker::Cache

source ·
pub struct Cache { /* private fields */ }
Expand description

Provides access to the Cache API. Because match is a reserved keyword in Rust, the match method has been renamed to get.

Our implementation of the Cache API respects the following HTTP headers on the response passed to put():

  • Cache-Control
  • Cache-Tag
    • Allows resource purging by tag(s) later (Enterprise only).
  • ETag
    • Allows cache.get() to evaluate conditional requests with If-None-Match.
  • Expires
    • A string that specifies when the resource becomes invalid.
  • Last-Modified
    • Allows cache.get() to evaluate conditional requests with If-Modified-Since.

This differs from the web browser Cache API as they do not honor any headers on the request or response.

Responses with Set-Cookie headers are never cached, because this sometimes indicates that the response contains unique data. To store a response with a Set-Cookie header, either delete that header or set Cache-Control: private=Set-Cookie on the response before calling cache.put().

Use the Cache-Control method to store the response without the Set-Cookie header.

Implementations§

source§

impl Cache

source

pub async fn open(name: String) -> Self

Opens a Cache by name. To access the default global cache, use Cache::default().

source

pub async fn put<'a, K: Into<CacheKey<'a>>>( &self, key: K, response: Response ) -> Result<()>

Adds to the cache a Response keyed to the given request.

The Response should include a cache-control header with max-age or s-maxage directives, otherwise the Cache API will not cache the response. The stale-while-revalidate and stale-if-error directives are not supported when using the cache.put or cache.get methods. For more information about how the Cache works, visit the documentation at Cache API and Cloudflare Cache-Control Directives

The Cache API will throw an error if:

  • the request passed is a method other than GET.
  • the response passed has a status of 206 Partial Content.
  • the response passed contains the header Vary: * (required by the Cache API specification).
source

pub async fn get<'a, K: Into<CacheKey<'a>>>( &self, key: K, ignore_method: bool ) -> Result<Option<Response>>

Returns the Response object keyed to that request. Never sends a subrequest to the origin. If no matching response is found in cache, returns None.

Unlike the browser Cache API, Cloudflare Workers do not support the ignoreSearch or ignoreVary options on get(). You can accomplish this behavior by removing query strings or HTTP headers at put() time. In addition, the stale-while-revalidate and stale-if-error directives are not supported when using the cache.put or cache.get methods.

Our implementation of the Cache API respects the following HTTP headers on the request passed to get():

  • Range
    • Results in a 206 response if a matching response with a Content-Length header is found. Your Cloudflare cache always respects range requests, even if an Accept-Ranges header is on the response.
  • If-Modified-Since
    • Results in a 304 response if a matching response is found with a Last-Modified header with a value after the time specified in If-Modified-Since.
  • If-None-Match
    • Results in a 304 response if a matching response is found with an ETag header with a value that matches a value in If-None-Match.
source

pub async fn delete<'a, K: Into<CacheKey<'a>>>( &self, key: K, ignore_method: bool ) -> Result<CacheDeletionOutcome>

Deletes the Response object associated with the key.

Returns:

  • Success, if the response was cached but is now deleted
  • ResponseNotFound, if the response was not in the cache at the time of deletion

Trait Implementations§

source§

impl Debug for Cache

source§

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

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

impl Default for Cache

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Cache

§

impl RefUnwindSafe for Cache

§

impl !Send for Cache

§

impl !Sync for Cache

§

impl Unpin for Cache

§

impl UnwindSafe for Cache

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more