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>
impl CacheLayer<InMemoryBackend>
Sourcepub fn new_in_memory(max_capacity: u64) -> Self
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,
impl<B> CacheLayer<B>where
B: CacheBackend,
Sourcepub fn new(backend: B) -> Self
pub fn new(backend: B) -> Self
Builds a cache layer with the default CachePolicy.
Sourcepub fn builder(backend: B) -> CacheLayerBuilder<B>
pub fn builder(backend: B) -> CacheLayerBuilder<B>
Returns a builder for fine-grained control over the cache policy.
pub fn with_policy(self, policy: CachePolicy) -> Self
pub fn with_ttl(self, ttl: Duration) -> Self
pub fn with_negative_ttl(self, ttl: Duration) -> Self
pub fn with_stale_while_revalidate(self, duration: Duration) -> Self
pub fn with_refresh_before(self, duration: Duration) -> Self
pub fn with_max_body_size(self, size: Option<usize>) -> Self
pub fn with_min_body_size(self, size: Option<usize>) -> Self
pub fn with_allow_streaming_bodies(self, allow: bool) -> Self
pub fn with_compression(self, config: CompressionConfig) -> Self
pub fn with_respect_cache_control(self, enabled: bool) -> Self
pub fn with_cache_statuses( self, statuses: impl IntoIterator<Item = u16>, ) -> Self
pub fn with_method_predicate<F>(self, predicate: F) -> Self
pub fn with_header_allowlist<I, S>(self, headers: I) -> Self
pub fn with_key_extractor(self, extractor: KeyExtractor) -> Self
Sourcepub async fn init_auto_refresh<S, ResBody>(
&self,
service: S,
) -> Result<(), String>
pub async fn init_auto_refresh<S, ResBody>( &self, service: S, ) -> Result<(), String>
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>
impl<B: Clone> Clone for CacheLayer<B>
Source§fn clone(&self) -> CacheLayer<B>
fn clone(&self) -> CacheLayer<B>
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<B> Drop for CacheLayer<B>
impl<B> Drop for CacheLayer<B>
Source§impl<S, B> Layer<S> for CacheLayer<B>where
B: CacheBackend,
impl<S, B> Layer<S> for CacheLayer<B>where
B: CacheBackend,
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> 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