Skip to main content

CacheableHttpResponse

Struct CacheableHttpResponse 

Source
pub struct CacheableHttpResponse<ResBody>
where ResBody: HttpBody,
{ pub parts: Parts, pub body: BufferedBody<ResBody>, }
Expand description

Wraps an HTTP response for cache storage and retrieval.

This type holds the response metadata (Parts) and a BufferedBody. When caching, the body is collected into bytes and stored as a SerializableHttpResponse. When retrieving from cache, the response is reconstructed with a BufferedBody::Complete containing the cached bytes.

§Type Parameters

  • ResBody - The HTTP response body type. Must implement hyper::body::Body with Send bounds. Common concrete types:
    • Empty<Bytes> - No body (204 responses)
    • Full<Bytes> - Complete body in memory
    • BoxBody<Bytes, E> - Type-erased body for dynamic dispatch

§Examples

use bytes::Bytes;
use http::Response;
use http_body_util::Empty;
use hitbox_http::{BufferedBody, CacheableHttpResponse};

let response = Response::builder()
    .status(200)
    .header("Content-Type", "application/json")
    .body(BufferedBody::Passthrough(Empty::<Bytes>::new()))
    .unwrap();

let cacheable = CacheableHttpResponse::from_response(response);

§Cache Storage

When a response is cacheable, the body is fully collected and stored as SerializableHttpResponse. This means:

  • The entire response body must fit in memory
  • Streaming responses are buffered before storage
  • Body collection errors result in a NonCacheable policy

§Performance

Retrieving a cached response is allocation-efficient: the body bytes are wrapped directly in BufferedBody::Complete without copying.

Fields§

§parts: Parts

Response metadata (status, version, headers, extensions).

§body: BufferedBody<ResBody>

The response body in one of three buffering states.

Implementations§

Source§

impl<ResBody> CacheableHttpResponse<ResBody>
where ResBody: HttpBody,

Source

pub fn from_response(response: Response<BufferedBody<ResBody>>) -> Self

Creates a cacheable response from an HTTP response with a buffered body.

The response body must already be wrapped in a BufferedBody. Use BufferedBody::Passthrough for responses that haven’t been inspected yet.

Source

pub fn into_response(self) -> Response<BufferedBody<ResBody>>

Converts back into a standard HTTP response.

Use this after cache policy evaluation to send the response to the client.

Trait Implementations§

Source§

impl<ResBody> CacheStatusExt for CacheableHttpResponse<ResBody>
where ResBody: HttpBody,

Source§

type Config = HeaderName

Configuration type for applying cache status (e.g., header name for HTTP).
Source§

fn cache_status(&mut self, status: CacheStatus, config: &Self::Config)

Applies cache status information to the response.
Source§

impl<ResBody> CacheableResponse for CacheableHttpResponse<ResBody>
where ResBody: HttpBody + Send + 'static, ResBody::Error: Send, ResBody::Data: Send,

Source§

type Cached = SerializableHttpResponse

The serializable type stored in cache.
Source§

type Subject = CacheableHttpResponse<ResBody>

The type that response predicates evaluate. Read more
Source§

type IntoCachedFuture = Pin<Box<dyn Future<Output = CachePolicy<<CacheableHttpResponse<ResBody> as CacheableResponse>::Cached, CacheableHttpResponse<ResBody>>> + Send>>

Future type for into_cached method.
Source§

type FromCachedFuture = Ready<CacheableHttpResponse<ResBody>>

Future type for from_cached method.
Source§

async fn cache_policy<P>( self, predicates: P, config: &EntityPolicyConfig, ) -> ResponseCachePolicy<Self>
where P: Predicate<Subject = Self::Subject> + Send + Sync,

Determine if this response should be cached. Read more
Source§

fn into_cached(self) -> Self::IntoCachedFuture

Convert this response to its cached representation. Read more
Source§

fn from_cached(cached: Self::Cached) -> Self::FromCachedFuture

Reconstruct a response from cached data. Read more
Source§

impl<ResBody> CacheableSubject for CacheableHttpResponse<ResBody>
where ResBody: HttpBody,

Source§

type Body = ResBody

The HTTP body type.
Source§

type Parts = Parts

The metadata type (e.g., http::request::Parts or http::response::Parts).
Source§

fn into_parts(self) -> (Self::Parts, BufferedBody<Self::Body>)

Decomposes this subject into metadata and body. Read more
Source§

fn from_parts(parts: Self::Parts, body: BufferedBody<Self::Body>) -> Self

Reconstructs a subject from metadata and body. Read more
Source§

impl<ResBody> Debug for CacheableHttpResponse<ResBody>
where ResBody: HttpBody + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<ResBody> HasHeaders for CacheableHttpResponse<ResBody>
where ResBody: HttpBody,

Source§

fn headers(&self) -> &HeaderMap

Returns a reference to the HTTP headers.
Source§

impl<ResBody> HasVersion for CacheableHttpResponse<ResBody>
where ResBody: HttpBody,

Source§

fn http_version(&self) -> Version

Returns the HTTP protocol version.

Auto Trait Implementations§

§

impl<ResBody> !Freeze for CacheableHttpResponse<ResBody>

§

impl<ResBody> !RefUnwindSafe for CacheableHttpResponse<ResBody>

§

impl<ResBody> Send for CacheableHttpResponse<ResBody>
where ResBody: Send, <ResBody as Body>::Error: Send,

§

impl<ResBody> Sync for CacheableHttpResponse<ResBody>
where ResBody: Sync, <ResBody as Body>::Error: Sync,

§

impl<ResBody> Unpin for CacheableHttpResponse<ResBody>
where ResBody: Unpin,

§

impl<ResBody> !UnwindSafe for CacheableHttpResponse<ResBody>

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more