Skip to main content

CacheLayer

Struct CacheLayer 

Source
pub struct CacheLayer<Req, K> { /* private fields */ }
Expand description

A Tower Layer that applies response caching to a service.

This layer wraps a service with a Cache middleware that stores successful responses and returns cached values for subsequent requests with the same key.

§State Isolation

Note: Each call to layer() creates a new cache store. If you need multiple services to share the same cache (e.g., when using a ServiceFactory that creates per-session services), use SharedCacheLayer instead, or call .shared() on this layer.

§Examples

use tower_resilience_cache::CacheLayer;
use tower::ServiceBuilder;
use std::time::Duration;

let cache_layer = CacheLayer::builder()
    .max_size(100)
    .ttl(Duration::from_secs(60))
    .key_extractor(|req: &String| req.clone())
    .build();

let service = ServiceBuilder::new()
    .layer(cache_layer)
    .service(my_service());

Implementations§

Source§

impl<Req, K> CacheLayer<Req, K>
where K: Hash + Eq + Clone + Send + 'static,

Source

pub fn new(config: CacheConfig<Req, K>) -> Self

Creates a new CacheLayer with the given configuration.

Source

pub fn builder() -> CacheConfigBuilder<Req, K>

Creates a new builder for configuring a cache layer.

§Examples
use tower_resilience_cache::CacheLayer;
use std::time::Duration;

let layer = CacheLayer::builder()
    .max_size(100)
    .ttl(Duration::from_secs(60))
    .key_extractor(|req: &String| req.clone())
    .build();
Source

pub fn shared<Resp>(self) -> SharedCacheLayer<Req, K, Resp>
where Resp: Clone + Send + 'static,

Converts this cache layer into a SharedCacheLayer that shares the cache store across all services created via layer().

This is useful when multiple service instances need to share the same cache, such as when services are created per-session or per-request.

§Type Parameters
  • Resp: The response type that will be cached. This must match the Response type of any service this layer is applied to.
§Examples
use tower_resilience_cache::CacheLayer;
use tower::ServiceBuilder;
use std::time::Duration;

let shared_cache = CacheLayer::builder()
    .max_size(100)
    .ttl(Duration::from_secs(60))
    .key_extractor(|req: &String| req.clone())
    .build()
    .shared::<String>();  // Specify the response type

// Both services share the same cache
let service1 = ServiceBuilder::new()
    .layer(shared_cache.clone())
    .service(my_service());

let service2 = ServiceBuilder::new()
    .layer(shared_cache)
    .service(my_service());

Trait Implementations§

Source§

impl<Req: Clone, K: Clone> Clone for CacheLayer<Req, K>

Source§

fn clone(&self) -> CacheLayer<Req, K>

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, Req, K> Layer<S> for CacheLayer<Req, K>
where K: Hash + Eq + Clone + Send + 'static, S: Service<Req>, S::Response: Clone + Send + 'static,

Source§

type Service = Cache<S, Req, K, <S as Service<Req>>::Response>

The wrapped service
Source§

fn layer(&self, service: 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<Req, K> Freeze for CacheLayer<Req, K>

§

impl<Req, K> !RefUnwindSafe for CacheLayer<Req, K>

§

impl<Req, K> Send for CacheLayer<Req, K>

§

impl<Req, K> Sync for CacheLayer<Req, K>

§

impl<Req, K> Unpin for CacheLayer<Req, K>

§

impl<Req, K> UnsafeUnpin for CacheLayer<Req, K>

§

impl<Req, K> !UnwindSafe for CacheLayer<Req, K>

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