Skip to main content

http_global_cache/
lib.rs

1// ---------- disk cache: CACacheManager (cache flag) ----------
2
3#[cfg(all(feature = "cache_request", feature = "cache", not(feature = "cache_mem")))]
4use http_cache_reqwest::CACacheManager;
5
6#[cfg(all(feature = "cache_request", feature = "cache", not(feature = "cache_mem")))]
7type RequestCacheManager = CACacheManager;
8
9// ---------- in-memory cache: MokaManager (cache_mem flag) ----------
10
11#[cfg(all(feature = "cache_request", feature = "cache_mem", not(feature = "cache")))]
12use http_cache_reqwest::MokaManager;
13
14#[cfg(all(feature = "cache_request", feature = "cache_mem", not(feature = "cache")))]
15type RequestCacheManager = MokaManager;
16
17// ---------- both enabled: prefer in-memory manager ----------
18//
19// Cargo feature unification can enable both flags in workspace `--all-features` builds.
20// Pick Moka deterministically so these builds remain valid and runtime behavior is explicit.
21#[cfg(all(feature = "cache_request", feature = "cache", feature = "cache_mem"))]
22use http_cache_reqwest::MokaManager;
23
24#[cfg(all(feature = "cache_request", feature = "cache", feature = "cache_mem"))]
25type RequestCacheManager = MokaManager;
26
27// ---------- default: CACacheManager when neither cache nor cache_mem is set ----------
28
29#[cfg(all(feature = "cache_request", not(feature = "cache"), not(feature = "cache_mem")))]
30use http_cache_reqwest::CACacheManager;
31
32#[cfg(all(feature = "cache_request", not(feature = "cache"), not(feature = "cache_mem")))]
33type RequestCacheManager = CACacheManager;
34
35// ---------- global cache manager ----------
36
37#[cfg(feature = "cache_request")]
38lazy_static::lazy_static! {
39    /// Cache manager for requests.
40    pub static ref CACACHE_MANAGER: RequestCacheManager = RequestCacheManager::default();
41}
42
43#[cfg(test)]
44mod tests {
45    #[cfg(all(feature = "cache_request", feature = "cache", feature = "cache_mem"))]
46    #[test]
47    fn test_both_cache_features_prefer_memory_manager() {
48        fn manager_type_name<T>(_: &T) -> &'static str {
49            std::any::type_name::<T>()
50        }
51
52        let ty = manager_type_name(&*super::CACACHE_MANAGER);
53        assert!(
54            ty.contains("MokaManager"),
55            "expected MokaManager when both cache and cache_mem are enabled, got {ty}"
56        );
57    }
58}