#[non_exhaustive]pub struct CachedResponse {
pub body: Bytes,
pub etag: Option<String>,
pub last_modified: Option<String>,
pub fetched_at: Instant,
}Expand description
Cached HTTP response with validation headers.
Stores response body and cache validation headers (ETag, Last-Modified)
for efficient conditional requests. The body uses Bytes which is an
Arc-like type optimized for network data, enabling zero-cost cloning
across multiple consumers without copying.
§Examples
use deps_core::cache::CachedResponse;
use bytes::Bytes;
use std::time::Instant;
let response = CachedResponse::new(Bytes::from("response data")).with_etag("\"abc123\"");
// Clone is cheap - only increments reference count
let cloned = response.clone();Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.body: BytesRaw response body, shareable across consumers without copying.
etag: Option<String>ETag header from the response, used for If-None-Match revalidation.
last_modified: Option<String>Last-Modified header from the response, used for If-Modified-Since revalidation.
fetched_at: InstantLocal time the response was fetched, used for TTL expiry checks.
Implementations§
Source§impl CachedResponse
impl CachedResponse
Sourcepub fn new(body: Bytes) -> Self
pub fn new(body: Bytes) -> Self
Constructs a CachedResponse for body, fetched now, with Self::etag and
Self::last_modified left None — chain Self::with_etag and/or
Self::with_last_modified to attach them.
Needed because Self is #[non_exhaustive]: a struct literal only works inside
this crate, so every other crate must go through this constructor instead.
§Examples
use deps_core::cache::CachedResponse;
use bytes::Bytes;
let response = CachedResponse::new(Bytes::from("response data")).with_etag("\"abc123\"");
assert_eq!(response.etag.as_deref(), Some("\"abc123\""));Sourcepub fn with_etag(self, etag: impl Into<String>) -> Self
pub fn with_etag(self, etag: impl Into<String>) -> Self
Attaches the response’s ETag header. See Self::etag.
Sourcepub fn with_last_modified(self, last_modified: impl Into<String>) -> Self
pub fn with_last_modified(self, last_modified: impl Into<String>) -> Self
Attaches the response’s Last-Modified header. See Self::last_modified.
Sourcepub const fn with_fetched_at(self, fetched_at: Instant) -> Self
pub const fn with_fetched_at(self, fetched_at: Instant) -> Self
Overrides when the response was fetched — otherwise Self::new stamps
Instant::now. Lets external code (tests, benches) construct a backdated entry to
exercise TTL-expiry behavior, the one thing a direct struct literal used to allow.