pub struct HttpCache { /* private fields */ }Expand description
HTTP cache with ETag and Last-Modified validation.
Implements RFC 7232 conditional requests to minimize network traffic.
All responses are cached with their validation headers, and subsequent
requests use If-None-Match (ETag) or If-Modified-Since headers
to check for updates.
The cache uses Bytes for response bodies, enabling efficient sharing
of cached data across multiple consumers without copying. Bytes is
an Arc-like type optimized for network I/O.
§Examples
use deps_core::cache::HttpCache;
let cache = HttpCache::new();
// First request - fetches from network
let data1 = cache.get_cached("https://index.crates.io/se/rd/serde").await?;
// Second request - uses conditional GET (304 Not Modified if unchanged)
let data2 = cache.get_cached("https://index.crates.io/se/rd/serde").await?;Implementations§
Source§impl HttpCache
impl HttpCache
Sourcepub fn new() -> HttpCache
pub fn new() -> HttpCache
Creates a new HTTP cache with default configuration.
The cache uses a configurable timeout for all requests and identifies itself with an auto-versioned user agent.
Sourcepub async fn get_cached(&self, url: &str) -> Result<Bytes, DepsError>
pub async fn get_cached(&self, url: &str) -> Result<Bytes, DepsError>
Retrieves data from URL with intelligent caching.
On first request, fetches data from the network and caches it. On subsequent requests, performs a conditional GET request using cached ETag or Last-Modified headers. If the server responds with 304 Not Modified, returns the cached data. Otherwise, fetches and caches the new data.
If the conditional request fails due to network errors, falls back to the cached data (stale-while-revalidate pattern).
§Returns
Returns Bytes containing the response body. Multiple calls for the
same URL return cheap clones (reference counting) without copying data.
§Errors
Returns DepsError::RegistryError if the initial fetch fails or
if no cached data exists and the network is unavailable.
§Examples
let cache = HttpCache::new();
let data = cache.get_cached("https://example.com/api/data").await?;
println!("Fetched {} bytes", data.len());Trait Implementations§
Auto Trait Implementations§
impl Freeze for HttpCache
impl !RefUnwindSafe for HttpCache
impl Send for HttpCache
impl Sync for HttpCache
impl Unpin for HttpCache
impl !UnwindSafe for HttpCache
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more