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() -> Self
pub fn new() -> Self
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>
pub async fn get_cached(&self, url: &str) -> Result<Bytes>
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());