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 implementhyper::body::BodywithSendbounds. Common concrete types:Empty<Bytes>- No body (204 responses)Full<Bytes>- Complete body in memoryBoxBody<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
NonCacheablepolicy
§Performance
Retrieving a cached response is allocation-efficient: the body bytes are
wrapped directly in BufferedBody::Complete without copying.
Fields§
§parts: PartsResponse 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,
impl<ResBody> CacheableHttpResponse<ResBody>where
ResBody: HttpBody,
Sourcepub fn from_response(response: Response<BufferedBody<ResBody>>) -> Self
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.
Sourcepub fn into_response(self) -> Response<BufferedBody<ResBody>>
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,
impl<ResBody> CacheStatusExt for CacheableHttpResponse<ResBody>where
ResBody: HttpBody,
Source§type Config = HeaderName
type Config = HeaderName
Source§fn cache_status(&mut self, status: CacheStatus, config: &Self::Config)
fn cache_status(&mut self, status: CacheStatus, config: &Self::Config)
Source§impl<ResBody> CacheableResponse for CacheableHttpResponse<ResBody>
impl<ResBody> CacheableResponse for CacheableHttpResponse<ResBody>
Source§type Cached = SerializableHttpResponse
type Cached = SerializableHttpResponse
Source§type Subject = CacheableHttpResponse<ResBody>
type Subject = CacheableHttpResponse<ResBody>
Source§type IntoCachedFuture = Pin<Box<dyn Future<Output = CachePolicy<<CacheableHttpResponse<ResBody> as CacheableResponse>::Cached, CacheableHttpResponse<ResBody>>> + Send>>
type IntoCachedFuture = Pin<Box<dyn Future<Output = CachePolicy<<CacheableHttpResponse<ResBody> as CacheableResponse>::Cached, CacheableHttpResponse<ResBody>>> + Send>>
into_cached method.Source§type FromCachedFuture = Ready<CacheableHttpResponse<ResBody>>
type FromCachedFuture = Ready<CacheableHttpResponse<ResBody>>
from_cached method.Source§async fn cache_policy<P>(
self,
predicates: P,
config: &EntityPolicyConfig,
) -> ResponseCachePolicy<Self>
async fn cache_policy<P>( self, predicates: P, config: &EntityPolicyConfig, ) -> ResponseCachePolicy<Self>
Source§fn into_cached(self) -> Self::IntoCachedFuture
fn into_cached(self) -> Self::IntoCachedFuture
Source§fn from_cached(cached: Self::Cached) -> Self::FromCachedFuture
fn from_cached(cached: Self::Cached) -> Self::FromCachedFuture
Source§impl<ResBody> CacheableSubject for CacheableHttpResponse<ResBody>where
ResBody: HttpBody,
impl<ResBody> CacheableSubject for CacheableHttpResponse<ResBody>where
ResBody: HttpBody,
Source§type Parts = Parts
type Parts = Parts
http::request::Parts or http::response::Parts).