CacheLayer

Struct CacheLayer 

Source
pub struct CacheLayer<B> { /* private fields */ }
Expand description

Configurable caching layer for Tower services.

The layer wraps an inner service and caches HTTP responses based on the configured CachePolicy. Create instances via CacheLayer::builder or CacheLayer::new for a sensible default policy.

Cloning a CacheLayer is cheap and shares the underlying backend and in-flight stampede locks.

Implementations§

Source§

impl CacheLayer<InMemoryBackend>

Source

pub fn new_in_memory(max_capacity: u64) -> Self

Creates a cache layer backed by an in-memory InMemoryBackend.

Source§

impl<B> CacheLayer<B>
where B: CacheBackend,

Source

pub fn new(backend: B) -> Self

Builds a cache layer with the default CachePolicy.

Source

pub fn builder(backend: B) -> CacheLayerBuilder<B>

Returns a builder for fine-grained control over the cache policy.

Source

pub fn with_policy(self, policy: CachePolicy) -> Self

Source

pub fn with_ttl(self, ttl: Duration) -> Self

Source

pub fn with_negative_ttl(self, ttl: Duration) -> Self

Source

pub fn with_stale_while_revalidate(self, duration: Duration) -> Self

Source

pub fn with_refresh_before(self, duration: Duration) -> Self

Source

pub fn with_max_body_size(self, size: Option<usize>) -> Self

Source

pub fn with_min_body_size(self, size: Option<usize>) -> Self

Source

pub fn with_allow_streaming_bodies(self, allow: bool) -> Self

Source

pub fn with_compression(self, config: CompressionConfig) -> Self

Source

pub fn with_respect_cache_control(self, enabled: bool) -> Self

Source

pub fn with_cache_statuses( self, statuses: impl IntoIterator<Item = u16>, ) -> Self

Source

pub fn with_method_predicate<F>(self, predicate: F) -> Self
where F: Fn(&Method) -> bool + Send + Sync + 'static,

Source

pub fn with_header_allowlist<I, S>(self, headers: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Source

pub fn with_key_extractor(self, extractor: KeyExtractor) -> Self

Source

pub async fn init_auto_refresh<S, ResBody>( &self, service: S, ) -> Result<(), String>
where S: Service<Request<()>, Response = Response<ResBody>> + Clone + Send + Sync + 'static, S::Future: Send + 'static, S::Error: Into<BoxError> + Send, ResBody: Body<Data = Bytes> + Send + 'static, ResBody::Error: Into<BoxError> + Send, B: Clone,

Manually initialize the auto-refresh manager with a service instance.

This should be called after constructing the service to start the background refresh task. This is only necessary if auto-refresh is enabled.

§Example
let layer = CacheLayer::builder(backend)
    .auto_refresh(config)
    .build();

layer.init_auto_refresh(my_service.clone()).await?;

Trait Implementations§

Source§

impl<B: Clone> Clone for CacheLayer<B>

Source§

fn clone(&self) -> CacheLayer<B>

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<B> Drop for CacheLayer<B>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<S, B> Layer<S> for CacheLayer<B>
where B: CacheBackend,

Source§

type Service = CacheService<S, B>

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<B> Freeze for CacheLayer<B>
where B: Freeze,

§

impl<B> !RefUnwindSafe for CacheLayer<B>

§

impl<B> Send for CacheLayer<B>
where B: Send,

§

impl<B> Sync for CacheLayer<B>
where B: Sync,

§

impl<B> Unpin for CacheLayer<B>
where B: Unpin,

§

impl<B> !UnwindSafe for CacheLayer<B>

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.