pub struct CacheService<S, B, C, CM, O = DisabledOffload> { /* private fields */ }Expand description
Tower Service that wraps an upstream service with caching.
CacheService intercepts HTTP requests, checks the cache, and either
returns cached responses or forwards requests to the upstream service.
It adds a cache status header (HIT/MISS/STALE) to every response.
§When You’ll Encounter This
You typically don’t create this directly. It’s produced when you apply
a Cache layer to a service via tower::ServiceBuilder.
§Type Parameters
S- The upstream Tower service being wrappedB- Cache backend (e.g.,MokaBackend)C- Configuration with predicates, extractors, and policyCM- Concurrency manager for dogpile preventionO- Offload strategy for background revalidation
Implementations§
Source§impl<S, B, C, CM, O> CacheService<S, B, C, CM, O>
impl<S, B, C, CM, O> CacheService<S, B, C, CM, O>
Sourcepub fn new(
upstream: S,
backend: Arc<B>,
configuration: C,
offload: O,
concurrency_manager: CM,
cache_status_header: HeaderName,
) -> Self
pub fn new( upstream: S, backend: Arc<B>, configuration: C, offload: O, concurrency_manager: CM, cache_status_header: HeaderName, ) -> Self
Creates a new cache service wrapping the given upstream.
Prefer using Cache::builder() and tower::ServiceBuilder instead
of constructing this directly.
Trait Implementations§
Source§impl<S, B, C, CM, O> Clone for CacheService<S, B, C, CM, O>
impl<S, B, C, CM, O> Clone for CacheService<S, B, C, CM, O>
Source§impl<S, B, C, CM, O, ReqBody, ResBody> Service<Request<ReqBody>> for CacheService<S, B, C, CM, O>where
S: Service<Request<BufferedBody<ReqBody>>, Response = Response<ResBody>> + Clone + Send + 'static,
B: CacheBackend + Clone + Send + Sync + 'static,
S::Future: Send,
C: CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>,
CM: ConcurrencyManager<Result<CacheableHttpResponse<ResBody>, S::Error>> + Clone + 'static,
O: Offload<'static> + Clone,
ReqBody: HttpBody + Send + 'static,
ReqBody::Error: Send,
ResBody: HttpBody + Send + 'static,
ResBody::Error: Send,
ResBody::Data: Send,
S::Error: Send,
impl<S, B, C, CM, O, ReqBody, ResBody> Service<Request<ReqBody>> for CacheService<S, B, C, CM, O>where
S: Service<Request<BufferedBody<ReqBody>>, Response = Response<ResBody>> + Clone + Send + 'static,
B: CacheBackend + Clone + Send + Sync + 'static,
S::Future: Send,
C: CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>,
CM: ConcurrencyManager<Result<CacheableHttpResponse<ResBody>, S::Error>> + Clone + 'static,
O: Offload<'static> + Clone,
ReqBody: HttpBody + Send + 'static,
ReqBody::Error: Send,
ResBody: HttpBody + Send + 'static,
ResBody::Error: Send,
ResBody::Data: Send,
S::Error: Send,
Source§type Response = Response<BufferedBody<ResBody>>
type Response = Response<BufferedBody<ResBody>>
Responses given by the service.
Source§type Error = <S as Service<Request<BufferedBody<ReqBody>>>>::Error
type Error = <S as Service<Request<BufferedBody<ReqBody>>>>::Error
Errors produced by the service.
Source§type Future = CacheServiceFuture<CacheFuture<'static, B, CacheableHttpRequest<ReqBody>, Result<CacheableHttpResponse<ResBody>, <S as Service<Request<BufferedBody<ReqBody>>>>::Error>, TowerUpstream<S, ReqBody, ResBody>, <C as CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>>::RequestPredicate, <C as CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>>::ResponsePredicate, <C as CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>>::Extractor, CM, O>, ResBody, <S as Service<Request<BufferedBody<ReqBody>>>>::Error>
type Future = CacheServiceFuture<CacheFuture<'static, B, CacheableHttpRequest<ReqBody>, Result<CacheableHttpResponse<ResBody>, <S as Service<Request<BufferedBody<ReqBody>>>>::Error>, TowerUpstream<S, ReqBody, ResBody>, <C as CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>>::RequestPredicate, <C as CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>>::ResponsePredicate, <C as CacheConfig<CacheableHttpRequest<ReqBody>, CacheableHttpResponse<ResBody>>>::Extractor, CM, O>, ResBody, <S as Service<Request<BufferedBody<ReqBody>>>>::Error>
The future response value.
Auto Trait Implementations§
impl<S, B, C, CM, O = DisabledOffload> !Freeze for CacheService<S, B, C, CM, O>
impl<S, B, C, CM, O> RefUnwindSafe for CacheService<S, B, C, CM, O>
impl<S, B, C, CM, O> Send for CacheService<S, B, C, CM, O>
impl<S, B, C, CM, O> Sync for CacheService<S, B, C, CM, O>
impl<S, B, C, CM, O> Unpin for CacheService<S, B, C, CM, O>
impl<S, B, C, CM, O> UnwindSafe for CacheService<S, B, C, CM, O>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
Yields a mutable reference to the service when it is ready to accept a request.
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
Yields the service when it is ready to accept a request.
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Consume this
Service, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
Executes a new future after this service’s future resolves. This does
not alter the behaviour of the
poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
Maps this service’s response value to a different value. This does not
alter the behaviour of the
poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
Maps this service’s error value to a different value. This does not
alter the behaviour of the
poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Maps this service’s result type (
Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read moreSource§fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F>
fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F>
Composes a function in front of the service. Read more
Source§fn then<F, Response, Error, Fut>(self, f: F) -> Then<Self, F>
fn then<F, Response, Error, Fut>(self, f: F) -> Then<Self, F>
Composes an asynchronous function after this service. Read more
Source§fn map_future<F, Fut, Response, Error>(self, f: F) -> MapFuture<Self, F>
fn map_future<F, Fut, Response, Error>(self, f: F) -> MapFuture<Self, F>
Composes a function that transforms futures produced by the service. Read more