HttpCacheLayer

Struct HttpCacheLayer 

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

Source

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);
Source

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 responses
  • options - 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);
Source

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,

Source§

fn clone(&self) -> HttpCacheLayer<CM>

Returns a duplicate 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<S, CM> Layer<S> for HttpCacheLayer<CM>
where CM: CacheManager,

Source§

type Service = HttpCacheService<S, CM>

The wrapped service
Source§

fn layer(&self, inner: S) -> Self::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

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

Source§

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> ErasedDestructor for T
where T: 'static,