memory_serve/cache_control.rs
1use axum::http::{HeaderName, HeaderValue, header::CACHE_CONTROL};
2
3/// Options to choose from to configure the Cache-Control header for served files.
4/// See [Cache control](index.html#cache-control)
5#[derive(Debug, Clone, Copy)]
6pub enum CacheControl {
7 /// clients can keep assets that have cache busting for a year: `"max-age=31536000, immutable"`
8 Long,
9 /// assets without cache busting are revalidated after a day and can be kept for a week: `"max-age=604800, stale-while-revalidate=86400"`
10 Medium,
11 /// cache kept for max 5 minutes, only at the client (not in a proxy): `"max-age:300, private"`
12 Short,
13 /// do not cache if freshness is really vital: `"no-cache"`
14 NoCache,
15 /// custom value
16 Custom(&'static str),
17}
18
19impl CacheControl {
20 /// Convert the cache-control variant into the header key/value pair.
21 pub(crate) fn as_header(&self) -> (HeaderName, HeaderValue) {
22 let value = match self {
23 Self::Long => "max-age=31536000, immutable",
24 Self::Medium => "max-age=604800, stale-while-revalidate=86400",
25 Self::Short => "max-age:300, private",
26 Self::NoCache => "no-cache",
27 Self::Custom(value) => value,
28 };
29
30 (CACHE_CONTROL, HeaderValue::from_static(value))
31 }
32}