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"