1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use Hash;
use Duration;
use cratehash_value;
/// Marker trait for cache keys whose updates should be broadcast
/// to other pods via Redis pub/sub.
///
/// Derive this on your key enum with [`#[derive(CriticalKey)]`](crate::CriticalKey)
/// to automatically invalidate the corresponding cache entry across all
/// pods whenever [`through_imc_keyed`](crate::through_imc_keyed) stores a
/// new value for that key.
///
/// Requires the `critical` feature.
///
/// # Example
///
/// ```rust,ignore
/// use imc::CriticalKey;
///
/// #[derive(Hash, Clone, CriticalKey)]
/// enum UserKey { ById(i32), ByEmail(String) }
/// ```
/// Eviction strategy for a per-type cache namespace.
///
/// Each type that implements [`ImcCacheable`] chooses one strategy.
/// The strategy determines which entry is evicted when the cache
/// exceeds [`ImcCacheable::cache_max_size`].
///
/// # Example
///
/// ```rust
/// use imc::CacheStrategy;
///
/// match CacheStrategy::Lru {
/// CacheStrategy::Lru => println!("evict least recently used"),
/// _ => unreachable!(),
/// }
/// ```
/// Any type that should be cacheable **must** implement this trait.
///
/// # Required methods
///
/// | Method | Returns | Purpose |
/// |--------|---------|---------|
/// | [`cache_id`](ImcCacheable::cache_id) | `Self::Id` | Extract unique identity from a value |
///
/// # Associated types
///
/// | Type | Bound | Purpose |
/// |------|-------|---------|
/// | `Id` | `Hash + Eq + Clone + Send + 'static` | Unique identity (e.g. primary key) |
/// | `Key` | `Hash + Clone + Send + 'static` | Cache-key type for [`through_imc_keyed`](crate::through_imc_keyed) |
///
/// # Optional methods
///
/// | Method | Default | Purpose |
/// |--------|---------|---------|
/// | [`cache_strategy`](ImcCacheable::cache_strategy) | `Lru` | Eviction strategy |
/// | [`cache_ttl`](ImcCacheable::cache_ttl) | `None` | Time-to-live per entry |
/// | [`cache_max_size`](ImcCacheable::cache_max_size) | `10_000` | Max entries per type |
/// | [`cache_value_size`](ImcCacheable::cache_value_size) | `None` | Byte-size for per-value limit |
/// | [`cache_max_value_size`](ImcCacheable::cache_max_value_size) | `1_048_576` | Max bytes per value (1 MiB) |
/// | [`cache_invalidation_channel`](ImcCacheable::cache_invalidation_channel) | `None` | Redis pub/sub channel for cross-process invalidation |
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use imc::{ImcCacheable, CacheStrategy};
///
/// #[derive(Clone, Debug, PartialEq)]
/// struct Product { id: u64, name: String, price: f64 }
///
/// impl ImcCacheable for Product {
/// type Id = u64;
/// type Key = String;
///
/// fn cache_id(&self) -> u64 { self.id }
///
/// fn cache_strategy() -> CacheStrategy { CacheStrategy::Lfu }
/// fn cache_ttl() -> Option<Duration> { Some(Duration::from_secs(600)) }
/// fn cache_max_size() -> usize { 5_000 }
/// }
/// ```
// ---------------------------------------------------------------------------
// Blanket impl for Vec<T> — cache entire result sets
// ---------------------------------------------------------------------------
/// [`Vec<T>`] implements [`ImcCacheable`] when `T` does.
///
/// This makes it possible to cache entire filtered result sets obtained
/// through tuple-based multi-condition keys.
///
/// # Dedup
///
/// The [`cache_id`](ImcCacheable::cache_id) hashes all element IDs
/// together. Two queries returning the same logical set of entities
/// share one cached `Vec`, even if the query arguments differ.