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    pub(crate) fn as_header(&self) -> (HeaderName, HeaderValue) {
21        let value = match self {
22            Self::Long => "max-age=31536000, immutable",
23            Self::Medium => "max-age=604800, stale-while-revalidate=86400",
24            Self::Short => "max-age:300, private",
25            Self::NoCache => "no-cache",
26            Self::Custom(value) => value,
27        };
28
29        (CACHE_CONTROL, HeaderValue::from_static(value))
30    }
31}