Skip to main content

coil_cache/
topology.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
4pub enum LocalCacheBackend {
5    Moka,
6}
7
8impl std::fmt::Display for LocalCacheBackend {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        match self {
11            Self::Moka => f.write_str("moka"),
12        }
13    }
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
17pub enum DistributedCacheBackend {
18    Redis,
19    Valkey,
20}
21
22impl std::fmt::Display for DistributedCacheBackend {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            Self::Redis => f.write_str("redis"),
26            Self::Valkey => f.write_str("valkey"),
27        }
28    }
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
32pub enum RequestCoalescingMode {
33    Disabled,
34    Local,
35    Cluster,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39pub struct CacheTopology {
40    l1: LocalCacheBackend,
41    l2: Option<DistributedCacheBackend>,
42    request_coalescing: RequestCoalescingMode,
43}
44
45impl CacheTopology {
46    pub fn moka_only() -> Self {
47        Self {
48            l1: LocalCacheBackend::Moka,
49            l2: None,
50            request_coalescing: RequestCoalescingMode::Local,
51        }
52    }
53
54    pub fn with_redis() -> Self {
55        Self {
56            l1: LocalCacheBackend::Moka,
57            l2: Some(DistributedCacheBackend::Redis),
58            request_coalescing: RequestCoalescingMode::Cluster,
59        }
60    }
61
62    pub fn with_valkey() -> Self {
63        Self {
64            l1: LocalCacheBackend::Moka,
65            l2: Some(DistributedCacheBackend::Valkey),
66            request_coalescing: RequestCoalescingMode::Cluster,
67        }
68    }
69
70    pub fn l1(&self) -> LocalCacheBackend {
71        self.l1
72    }
73
74    pub fn l2(&self) -> Option<DistributedCacheBackend> {
75        self.l2
76    }
77
78    pub fn request_coalescing_mode(&self) -> RequestCoalescingMode {
79        self.request_coalescing
80    }
81
82    pub fn supports_shared_invalidation(&self) -> bool {
83        self.l2.is_some()
84    }
85
86    pub fn supports_shared_coalescing(&self) -> bool {
87        matches!(self.request_coalescing, RequestCoalescingMode::Cluster)
88    }
89}