Skip to main content

SharedCacheLayer

Struct SharedCacheLayer 

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

A Tower Layer that applies response caching with a shared store.

Unlike CacheLayer, this layer shares a single cache store across all services created via layer(). This is useful when multiple service instances (e.g., per-session or per-request services) need to share the same cache.

§Type Parameters

  • Req: The request type
  • K: The cache key type (extracted from requests)
  • Resp: The response type that will be cached

§Examples

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

// Create a shared cache layer
let cache_layer: SharedCacheLayer<String, String, String> = SharedCacheLayer::builder()
    .max_size(100)
    .ttl(Duration::from_secs(60))
    .key_extractor(|req: &String| req.clone())
    .build();

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

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

§Creating from CacheLayer

You can also convert an existing CacheLayer configuration:

use tower_resilience_cache::CacheLayer;
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

Implementations§

Source§

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

Source

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

Creates a new SharedCacheLayer with the given configuration.

Source

pub fn builder() -> SharedCacheConfigBuilder<Req, K, Resp>

Creates a new builder for configuring a shared cache layer.

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

let layer: SharedCacheLayer<String, String, String> = SharedCacheLayer::builder()
    .max_size(100)
    .ttl(Duration::from_secs(60))
    .key_extractor(|req: &String| req.clone())
    .build();

Trait Implementations§

Source§

impl<Req: Clone, K: Clone, Resp: Clone> Clone for SharedCacheLayer<Req, K, Resp>

Source§

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

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

Source§

type Service = Cache<S, Req, K, Resp>

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, Resp> Freeze for SharedCacheLayer<Req, K, Resp>

§

impl<Req, K, Resp> !RefUnwindSafe for SharedCacheLayer<Req, K, Resp>

§

impl<Req, K, Resp> Send for SharedCacheLayer<Req, K, Resp>

§

impl<Req, K, Resp> Sync for SharedCacheLayer<Req, K, Resp>

§

impl<Req, K, Resp> Unpin for SharedCacheLayer<Req, K, Resp>

§

impl<Req, K, Resp> UnsafeUnpin for SharedCacheLayer<Req, K, Resp>

§

impl<Req, K, Resp> !UnwindSafe for SharedCacheLayer<Req, K, Resp>

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.