Caching Headers
提供對緩存頭配置支持的中間件.
實際上實現內部包含了 CachingHeaders
, Modified
, ETag
三個 Handler
的實現, CachingHeaders
是後兩者的組合. 正常情況下使用 CachingHeaders
.
示例代碼
use salvo::prelude::*;
#[handler]
async fn hello() -> &'static str {
"Hello World"
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
// CachingHeader must be before Compression.
let router = Router::with_hoop(CachingHeaders::new())
.hoop(Compression::new().min_length(0))
.get(hello);
let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
Server::new(acceptor).serve(router).await;
}
[package]
name = "example-caching-headers"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
salvo = { workspace = true, features = ["caching-headers", "compression"] }
tokio = { version = "1", features = ["macros"] }
tracing = "0.1"
tracing-subscriber = "0.3"