Skip to main content

CacheableHttpResponse

Struct CacheableHttpResponse 

Source
pub struct CacheableHttpResponse<ResBody>
where ResBody: Body,
{ 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: Body,

Source

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

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: Body,

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: &<CacheableHttpResponse<ResBody> as CacheStatusExt>::Config, )

Applies cache status information to the response.
Source§

impl<ResBody> CacheableResponse for CacheableHttpResponse<ResBody>
where ResBody: Body + Send + 'static, <ResBody as Body>::Error: Send, <ResBody as Body>::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, ) -> CachePolicy<CacheValue<<CacheableHttpResponse<ResBody> as CacheableResponse>::Cached>, CacheableHttpResponse<ResBody>>
where P: Predicate<Subject = <CacheableHttpResponse<ResBody> as CacheableResponse>::Subject> + Send + Sync,

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

fn into_cached( self, ) -> <CacheableHttpResponse<ResBody> as CacheableResponse>::IntoCachedFuture

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

fn from_cached( cached: <CacheableHttpResponse<ResBody> as CacheableResponse>::Cached, ) -> <CacheableHttpResponse<ResBody> as CacheableResponse>::FromCachedFuture

Reconstruct a response from cached data. Read more
Source§

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

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, ) -> (<CacheableHttpResponse<ResBody> as CacheableSubject>::Parts, BufferedBody<<CacheableHttpResponse<ResBody> as CacheableSubject>::Body>)

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

fn from_parts( parts: <CacheableHttpResponse<ResBody> as CacheableSubject>::Parts, body: BufferedBody<<CacheableHttpResponse<ResBody> as CacheableSubject>::Body>, ) -> CacheableHttpResponse<ResBody>

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

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

Source§

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

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

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

Source§

fn headers(&self) -> &HeaderMap

Returns a reference to the HTTP headers.
Source§

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

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,