use std::sync::OnceLock;
pub(super) const CDN_LIVE: &str = "public, max-age=1, stale-if-error=300";
const CDN_AGGRESSIVE: &str = "public, max-age=31536000, immutable";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum CdnCacheMode {
#[default]
Live,
Aggressive,
}
impl CdnCacheMode {
const fn as_str(self) -> &'static str {
match self {
Self::Live => CDN_LIVE,
Self::Aggressive => CDN_AGGRESSIVE,
}
}
}
static CDN_CACHE_MODE: OnceLock<CdnCacheMode> = OnceLock::new();
pub(crate) fn init(mode: CdnCacheMode) {
if CDN_CACHE_MODE.set(mode).is_err() {
let existing = CDN_CACHE_MODE.get().copied().unwrap_or_default();
if existing != mode {
tracing::warn!(
"cache::init called with {mode:?} but mode is already set to {existing:?}; ignoring"
);
}
}
}
pub(super) fn cdn_cached() -> &'static str {
CDN_CACHE_MODE
.get()
.copied()
.unwrap_or_default()
.as_str()
}