use once_cell::sync::Lazy;
use regex::Regex;
pub struct CacheableRoute {
pub pattern: &'static str,
pub methods: &'static [&'static str],
pub ttl_seconds: u64,
pub description: &'static str,
}
pub const CACHEABLE_ROUTES: &[CacheableRoute] = &[
CacheableRoute {
pattern: r"(?i)^/(?:emby/)?Items/[^/]+/PlaybackInfo",
methods: &["POST"],
ttl_seconds: 7200, description: "Playback info — Emby processing takes ~1400ms",
},
CacheableRoute {
pattern: r"(?i)^/(?:emby/)?Shows/NextUp",
methods: &["GET"],
ttl_seconds: 7200, description: "Next-up episode for a series",
},
CacheableRoute {
pattern: r"(?i)^/(?:emby/)?Shows/[^/]+/Episodes",
methods: &["GET"],
ttl_seconds: 7200, description: "Episode list for a series season",
},
];
pub struct CompiledCacheableRoute {
pub regex: Regex,
pub methods: &'static [&'static str],
pub ttl_seconds: u64,
}
pub static COMPILED_ROUTES: Lazy<Vec<CompiledCacheableRoute>> =
Lazy::new(|| {
CACHEABLE_ROUTES
.iter()
.filter_map(|route| {
Regex::new(route.pattern).ok().map(|regex| {
CompiledCacheableRoute {
regex,
methods: route.methods,
ttl_seconds: route.ttl_seconds,
}
})
})
.collect()
});
pub fn find_cacheable_route(
path: &str,
method: &str,
) -> Option<&'static CompiledCacheableRoute> {
COMPILED_ROUTES.iter().find(|route| {
route.regex.is_match(path)
&& route.methods.iter().any(|m| m.eq_ignore_ascii_case(method))
})
}