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// CACacheManager no longer implements Default in http-cache 1.x.
38// We provide a helper that picks the right constructor per manager type.
39
40#[cfg(all(
41    feature = "cache_request",
42    any(
43        all(feature = "cache", not(feature = "cache_mem")),
44        all(not(feature = "cache"), not(feature = "cache_mem"))
45    )
46))]
47fn make_manager() -> RequestCacheManager {
48    CACacheManager::new(
49        std::env::temp_dir().join("spider-http-cache"),
50        false,
51    )
52}
53
54#[cfg(all(
55    feature = "cache_request",
56    any(
57        feature = "cache_mem",
58    )
59))]
60fn make_manager() -> RequestCacheManager {
61    RequestCacheManager::default()
62}
63
64#[cfg(feature = "cache_request")]
65lazy_static::lazy_static! {
66    /// Cache manager for requests.
67    pub static ref CACACHE_MANAGER: RequestCacheManager = make_manager();
68}
69
70#[cfg(test)]
71mod tests {
72    #[cfg(all(feature = "cache_request", feature = "cache", feature = "cache_mem"))]
73    #[test]
74    fn test_both_cache_features_prefer_memory_manager() {
75        fn manager_type_name<T>(_: &T) -> &'static str {
76            std::any::type_name::<T>()
77        }
78
79        let ty = manager_type_name(&*super::CACACHE_MANAGER);
80        assert!(
81            ty.contains("MokaManager"),
82            "expected MokaManager when both cache and cache_mem are enabled, got {ty}"
83        );
84    }
85}