Expand description
§Tower HTTP Cache
tower-http-cache provides a composable caching layer for Tower-based services with
pluggable backends (in-memory, Redis, and more).
The crate exposes a single CacheLayer that can be configured with
a variety of policies and storage backends. Most consumers will
start from CacheLayer::builder and choose an in-memory or Redis backend:
use std::time::Duration;
use tower::{Service, ServiceBuilder, ServiceExt};
use tower_http_cache::prelude::*;
let layer = CacheLayer::builder(InMemoryBackend::new(1_000))
.ttl(Duration::from_secs(30))
.stale_while_revalidate(Duration::from_secs(10))
.build();
let mut svc = ServiceBuilder::new()
.layer(layer)
.service(tower::service_fn(|_req| async {
Ok::<_, std::convert::Infallible>(http::Response::new(http_body_util::Full::from("ok")))
}));
let response = svc
.ready()
.await?
.call(http::Request::new(()))
.await?;§Status
The project is under active development. The public API is not yet stabilized.
Re-exports§
pub use chunks::ChunkCache;pub use chunks::ChunkCacheStats;pub use chunks::ChunkMetadata;pub use chunks::ChunkedEntry;pub use layer::CacheLayer;pub use layer::CacheLayerBuilder;pub use layer::KeyExtractor;pub use logging::CacheEvent;pub use logging::CacheEventType;pub use logging::MLLoggingConfig;pub use range::RangeHandling;pub use range::RangeRequest;pub use request_id::RequestId;pub use streaming::StreamingDecision;pub use streaming::StreamingPolicy;pub use tags::TagIndex;pub use tags::TagPolicy;
Modules§
- admin
- Admin API for cache introspection and management.
- backend
- Storage backends for the cache layer.
- chunks
- Chunk-based caching for large files and efficient range request handling.
- codec
- error
- layer
- logging
- ML-ready structured logging for cache operations.
- policy
- prelude
- Re-exports for consumers who prefer a single import.
- range
- HTTP Range request parsing and handling.
- refresh
- Auto-refresh functionality for frequently accessed cache entries.
- request_
id - Request ID infrastructure for request correlation and tracing.
- streaming
- Smart streaming and large file handling for tower-http-cache.
- tags
- Cache tags and invalidation groups.