hitbox-reqwest
Hitbox cache integration for reqwest HTTP client via reqwest-middleware.
This crate provides [CacheMiddleware] for reqwest_middleware that adds
caching capabilities to the reqwest HTTP client using the hitbox caching
framework. Use [CacheMiddleware::builder()] to construct the middleware
with a fluent API.
Overview
hitbox-reqwest enables client-side HTTP caching with:
- Request and response predicates - Control what gets cached
- Cache key extraction - Build cache keys from request components
- Multiple backend support - Use in-memory, file storage, or distributed backends
- Dogpile prevention - Optional concurrency control to prevent thundering herd
- Cache status headers - Automatic
x-cache-statusheader (HIT/MISS/STALE), configurable name
Core Concepts
- Predicate: A rule that determines if a request or response is cacheable.
Predicates return
CacheableorNonCacheable. See [hitbox_http::predicates] for built-in predicates. - Extractor: Generates cache key parts from HTTP components (method, path, headers).
See [
hitbox_http::extractors] for built-in extractors. - Backend: Storage layer for cached responses. Available backends include in-memory, file storage, and distributed options.
- Policy: Controls TTL, stale-while-revalidate, and other caching behavior.
- Dogpile effect: When a cache entry expires, multiple concurrent requests may
all attempt to refresh it simultaneously. Use [
BroadcastConcurrencyManager] to prevent this.
Quick Start
Basic Usage with Builder Pattern
use Duration;
use Client;
use ClientBuilder;
use CacheMiddleware;
use Config;
use ;
use PolicyConfig;
use MokaBackend;
#
# async
Response Headers
The middleware adds a cache status header to every response (default: x-cache-status):
| Value | Meaning |
|---|---|
HIT |
Response served from cache |
MISS |
Response fetched from upstream (may be cached) |
STALE |
Stale cache served (background refresh may occur) |
To use a custom header name, call .cache_status_header()
on the builder. The default is x-cache-status.
Re-exports
This crate re-exports commonly used types for convenience:
- From [
hitbox_http]: [CacheableHttpRequest], [CacheableHttpResponse], [predicates], [extractors] - From [
hitbox]: [Config], [CacheConfig], [PolicyConfig], concurrency managers - From [
hitbox_core]: [DisabledOffload]
Caveats
- No background revalidation: Unlike
hitbox-tower, this middleware uses [DisabledOffload] becausereqwest_middleware::Next<'_>has a non-'staticlifetime, preventing spawning of background tasks.
Internals
On cache miss, the middleware uses [ReqwestUpstream] to call the next
middleware in the chain and convert between hitbox and reqwest types.