pub struct Cache<S> { /* private fields */ }Expand description
Implement a HTTP cache.
Implementations§
Source§impl<S> Cache<S>where
S: CacheStorage,
impl<S> Cache<S>where
S: CacheStorage,
Sourcepub fn new(storage: S) -> Self
pub fn new(storage: S) -> Self
Construct a new cache with the given storage.
Defaults to a private cache.
Sourcepub fn new_with_options(storage: S, options: CacheOptions) -> Self
pub fn new_with_options(storage: S, options: CacheOptions) -> Self
Construct a new cache with the given storage and options.
Sourcepub fn with_revalidation_hook(
self,
hook: impl Fn(&dyn RequestLike, &mut HeaderMap) -> Result<()> + Send + Sync + 'static,
) -> Self
pub fn with_revalidation_hook( self, hook: impl Fn(&dyn RequestLike, &mut HeaderMap) -> Result<()> + Send + Sync + 'static, ) -> Self
Sets the revalidation hook to use.
The hook is provided the original request and a mutable header map containing headers explicitly set for the revalidation request.
For example, a hook may alter the revalidation headers to update an
Authorization header based on the headers used for revalidation.
If the hook returns an error, the error is propagated out as the result of the original request.
Sourcepub async fn send<B: Body + Send>(
&self,
request: impl Request<B>,
) -> Result<Response<CacheBody<B>>>
pub async fn send<B: Body + Send>( &self, request: impl Request<B>, ) -> Result<Response<CacheBody<B>>>
Sends a HTTP request through the cache.
If a previous response is cached and not stale, the request is not sent upstream and the cached response is returned.
If a previous response is cached and is stale, the response is revalidated, the cache is updated, and the cached response returned.
If a previous response is not in the cache, the request is sent upstream and the response is cached, if it is cacheable.