pub struct HttpCacheLayer<CM>where
CM: CacheManager,{ /* private fields */ }
Expand description
HTTP cache layer for Tower services.
This layer implements HTTP caching according to RFC 7234, automatically caching GET and HEAD responses based on their cache-control headers and invalidating cache entries when unsafe methods (PUT, POST, DELETE, PATCH) are used.
§Example
use http_cache_tower::{HttpCacheLayer, CACacheManager};
use tower::ServiceBuilder;
use tower::service_fn;
use http::{Request, Response};
use http_body_util::Full;
use bytes::Bytes;
use std::convert::Infallible;
let cache_manager = CACacheManager::new("./cache".into(), true);
let cache_layer = HttpCacheLayer::new(cache_manager);
// Use with ServiceBuilder
let service = ServiceBuilder::new()
.layer(cache_layer)
.service_fn(|_req: Request<Full<Bytes>>| async {
Ok::<_, Infallible>(Response::new(Full::new(Bytes::from("Hello"))))
});
Implementations§
Source§impl<CM> HttpCacheLayer<CM>where
CM: CacheManager,
impl<CM> HttpCacheLayer<CM>where
CM: CacheManager,
Sourcepub fn new(cache_manager: CM) -> Self
pub fn new(cache_manager: CM) -> Self
Create a new HTTP cache layer with default configuration.
Uses CacheMode::Default
and default HttpCacheOptions
.
§Arguments
cache_manager
- The cache manager to use for storing responses
§Example
use http_cache_tower::{HttpCacheLayer, CACacheManager};
let cache_manager = CACacheManager::new("./cache".into(), true);
let layer = HttpCacheLayer::new(cache_manager);
Sourcepub fn with_options(cache_manager: CM, options: HttpCacheOptions) -> Self
pub fn with_options(cache_manager: CM, options: HttpCacheOptions) -> Self
Create a new HTTP cache layer with custom options.
Uses CacheMode::Default
but allows customizing the cache behavior
through HttpCacheOptions
.
§Arguments
cache_manager
- The cache manager to use for storing responsesoptions
- Custom cache options
§Example
use http_cache_tower::{HttpCacheLayer, CACacheManager};
use http_cache::HttpCacheOptions;
let cache_manager = CACacheManager::new("./cache".into(), true);
let options = HttpCacheOptions {
cache_key: Some(std::sync::Arc::new(|req: &http::request::Parts| {
format!("custom:{}:{}", req.method, req.uri)
})),
..Default::default()
};
let layer = HttpCacheLayer::with_options(cache_manager, options);
Sourcepub fn with_cache(cache: HttpCache<CM>) -> Self
pub fn with_cache(cache: HttpCache<CM>) -> Self
Create a new HTTP cache layer with a pre-configured cache.
This method gives you full control over the cache configuration, including the cache mode.
§Arguments
cache
- A fully configured HttpCache instance
§Example
use http_cache_tower::{HttpCacheLayer, CACacheManager};
use http_cache::{HttpCache, CacheMode, HttpCacheOptions};
let cache_manager = CACacheManager::new("./cache".into(), true);
let cache = HttpCache {
mode: CacheMode::ForceCache,
manager: cache_manager,
options: HttpCacheOptions::default(),
};
let layer = HttpCacheLayer::with_cache(cache);
Trait Implementations§
Source§impl<CM> Clone for HttpCacheLayer<CM>where
CM: CacheManager + Clone,
impl<CM> Clone for HttpCacheLayer<CM>where
CM: CacheManager + Clone,
Source§fn clone(&self) -> HttpCacheLayer<CM>
fn clone(&self) -> HttpCacheLayer<CM>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<S, CM> Layer<S> for HttpCacheLayer<CM>where
CM: CacheManager,
impl<S, CM> Layer<S> for HttpCacheLayer<CM>where
CM: CacheManager,
Auto Trait Implementations§
impl<CM> Freeze for HttpCacheLayer<CM>
impl<CM> !RefUnwindSafe for HttpCacheLayer<CM>
impl<CM> Send for HttpCacheLayer<CM>
impl<CM> Sync for HttpCacheLayer<CM>
impl<CM> Unpin for HttpCacheLayer<CM>
impl<CM> !UnwindSafe for HttpCacheLayer<CM>
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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