Skip to main content

tower_http_cache/
lib.rs

1//! Tower HTTP Cache
2//! ==================
3//!
4//! `tower-http-cache` provides a composable caching layer for Tower-based services with
5//! pluggable backends (in-memory, Redis, and more).
6//!
7//! The crate exposes a single [`CacheLayer`] that can be configured with
8//! a variety of policies and storage backends. Most consumers will
9//! start from [`CacheLayer::builder`] and choose an in-memory or Redis backend:
10//!
11//! ```no_run
12//! use std::time::Duration;
13//! use tower::{Service, ServiceBuilder, ServiceExt};
14//! use tower_http_cache::prelude::*;
15//!
16//! # async fn run() -> Result<(), tower_http_cache::layer::BoxError> {
17//! let layer = CacheLayer::builder(InMemoryBackend::new(1_000))
18//!     .ttl(Duration::from_secs(30))
19//!     .stale_while_revalidate(Duration::from_secs(10))
20//!    .build();
21//!
22//! let mut svc = ServiceBuilder::new()
23//!     .layer(layer)
24//!     .service(tower::service_fn(|_req| async {
25//!         Ok::<_, std::convert::Infallible>(http::Response::new(http_body_util::Full::from("ok")))
26//!     }));
27//!
28//! let response = svc
29//!     .ready()
30//!     .await?
31//!     .call(http::Request::new(()))
32//!     .await?;
33//! # drop(response);
34//! # Ok(())
35//! # }
36//! ```
37//!
38//! ## Status
39//! The project is under active development. The public API is not yet stabilized.
40
41#[cfg(feature = "admin-api")]
42pub mod admin;
43pub mod backend;
44pub mod chunks;
45#[cfg(feature = "serde")]
46pub mod codec;
47pub mod error;
48pub mod layer;
49pub mod logging;
50pub mod policy;
51pub mod prelude;
52pub mod range;
53pub mod refresh;
54pub mod request_id;
55pub mod streaming;
56pub mod tags;
57/// UTC timestamp formatting. Private: replaces the former `chrono` dependency
58/// and is not part of the public API.
59mod time_fmt;
60
61pub use chunks::{ChunkCache, ChunkCacheStats, ChunkMetadata, ChunkedEntry};
62pub use layer::{CacheLayer, CacheLayerBuilder, KeyExtractor};
63#[cfg(feature = "serde")]
64pub use logging::CacheEvent;
65pub use logging::{CacheEventType, MLLoggingConfig};
66pub use range::{RangeHandling, RangeRequest};
67pub use request_id::RequestId;
68pub use streaming::{StreamingDecision, StreamingPolicy};
69pub use tags::{TagIndex, TagPolicy};
70
71#[cfg(feature = "admin-api")]
72pub use admin::{AdminConfig, AdminState};