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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! Configuration for cache.
use crate::events::CacheEvent;
use crate::eviction::EvictionPolicy;
use std::hash::Hash;
use std::sync::Arc;
use std::time::Duration;
use tower_resilience_core::{EventListeners, FnListener};
/// Function that extracts a cache key from a request.
pub type KeyExtractor<Req, K> = Arc<dyn Fn(&Req) -> K + Send + Sync>;
/// Configuration for the cache pattern.
pub struct CacheConfig<Req, K> {
pub(crate) max_size: usize,
pub(crate) ttl: Option<Duration>,
pub(crate) eviction_policy: EvictionPolicy,
pub(crate) key_extractor: KeyExtractor<Req, K>,
pub(crate) event_listeners: EventListeners<CacheEvent>,
pub(crate) name: String,
}
/// Builder for configuring and constructing a cache.
pub struct CacheConfigBuilder<Req, K> {
max_size: usize,
ttl: Option<Duration>,
eviction_policy: EvictionPolicy,
key_extractor: Option<KeyExtractor<Req, K>>,
event_listeners: EventListeners<CacheEvent>,
name: String,
}
impl<Req, K> CacheConfigBuilder<Req, K>
where
K: Hash + Eq + Clone + Send + 'static,
{
/// Creates a new builder with default values.
pub fn new() -> Self {
Self {
max_size: 100,
ttl: None,
eviction_policy: EvictionPolicy::default(),
key_extractor: None,
event_listeners: EventListeners::new(),
name: String::from("<unnamed>"),
}
}
/// Sets the maximum number of entries in the cache.
///
/// Default: 100
pub fn max_size(mut self, size: usize) -> Self {
self.max_size = size;
self
}
/// Sets the time-to-live for cached entries.
///
/// If set, entries will expire after the specified duration.
/// Default: None (no expiration)
pub fn ttl(mut self, ttl: Duration) -> Self {
self.ttl = Some(ttl);
self
}
/// Sets the eviction policy for the cache.
///
/// Determines which entry to evict when the cache reaches capacity.
///
/// # Options
///
/// - `EvictionPolicy::Lru` - Least Recently Used (default)
/// - Evicts entries that haven't been accessed recently
/// - Best for general-purpose caching
///
/// - `EvictionPolicy::Lfu` - Least Frequently Used
/// - Evicts entries with the lowest access count
/// - Best for long-lived caches with consistently popular items
///
/// - `EvictionPolicy::Fifo` - First In, First Out
/// - Evicts the oldest entry regardless of access pattern
/// - Best for time-based caching where age matters
///
/// # Example
///
/// ```rust
/// use tower_resilience_cache::{CacheLayer, EvictionPolicy};
///
/// let cache = CacheLayer::<String, String>::builder()
/// .max_size(100)
/// .eviction_policy(EvictionPolicy::Lfu)
/// .key_extractor(|req| req.clone())
/// .build();
/// ```
///
/// Default: `EvictionPolicy::Lru`
pub fn eviction_policy(mut self, policy: EvictionPolicy) -> Self {
self.eviction_policy = policy;
self
}
/// Sets the function that extracts a cache key from a request.
///
/// This function must be provided before building.
pub fn key_extractor<F>(mut self, f: F) -> Self
where
F: Fn(&Req) -> K + Send + Sync + 'static,
{
self.key_extractor = Some(Arc::new(f));
self
}
/// Sets the name of this cache instance for observability.
///
/// Default: `"<unnamed>"`
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
/// Registers a callback when a cache hit occurs.
///
/// A cache hit occurs when a requested entry is found in the cache and has not expired.
///
/// # Callback Signature
/// `Fn()` - Called with no parameters when a cache hit is detected.
///
/// # Example
/// ```rust,no_run
/// use tower_resilience_cache::CacheLayer;
/// use std::sync::atomic::{AtomicUsize, Ordering};
/// use std::sync::Arc;
///
/// #[derive(Clone, Hash, Eq, PartialEq)]
/// struct Request {
/// id: String,
/// }
///
/// let hit_count = Arc::new(AtomicUsize::new(0));
/// let counter = Arc::clone(&hit_count);
///
/// let config = CacheLayer::<Request, String>::builder()
/// .key_extractor(|req| req.id.clone())
/// .on_hit(move || {
/// let count = counter.fetch_add(1, Ordering::SeqCst);
/// println!("Cache hit #{}", count + 1);
/// })
/// .build();
/// ```
pub fn on_hit<F>(mut self, f: F) -> Self
where
F: Fn() + Send + Sync + 'static,
{
self.event_listeners.add(FnListener::new(move |event| {
if matches!(event, CacheEvent::Hit { .. }) {
f();
}
}));
self
}
/// Registers a callback when a cache miss occurs.
///
/// A cache miss occurs when a requested entry is not found in the cache or has expired.
/// The underlying service will be called to fetch the value, which will then be cached.
///
/// # Callback Signature
/// `Fn()` - Called with no parameters when a cache miss is detected.
///
/// # Example
/// ```rust,no_run
/// use tower_resilience_cache::CacheLayer;
/// use std::sync::atomic::{AtomicUsize, Ordering};
/// use std::sync::Arc;
///
/// #[derive(Clone, Hash, Eq, PartialEq)]
/// struct Request {
/// id: String,
/// }
///
/// let miss_count = Arc::new(AtomicUsize::new(0));
/// let counter = Arc::clone(&miss_count);
///
/// let config = CacheLayer::<Request, String>::builder()
/// .key_extractor(|req| req.id.clone())
/// .on_miss(move || {
/// let count = counter.fetch_add(1, Ordering::SeqCst);
/// println!("Cache miss #{} - fetching from service", count + 1);
/// })
/// .build();
/// ```
pub fn on_miss<F>(mut self, f: F) -> Self
where
F: Fn() + Send + Sync + 'static,
{
self.event_listeners.add(FnListener::new(move |event| {
if matches!(event, CacheEvent::Miss { .. }) {
f();
}
}));
self
}
/// Registers a callback when an entry is evicted from the cache.
///
/// Eviction occurs when:
/// - The cache reaches its maximum size and needs to make room for new entries
/// - An entry expires due to TTL (time-to-live) configuration
///
/// # Callback Signature
/// `Fn()` - Called with no parameters when a cache eviction occurs.
///
/// # Example
/// ```rust,no_run
/// use tower_resilience_cache::CacheLayer;
/// use std::sync::atomic::{AtomicUsize, Ordering};
/// use std::sync::Arc;
/// use std::time::Duration;
///
/// #[derive(Clone, Hash, Eq, PartialEq)]
/// struct Request {
/// id: String,
/// }
///
/// let eviction_count = Arc::new(AtomicUsize::new(0));
/// let counter = Arc::clone(&eviction_count);
///
/// let config = CacheLayer::<Request, String>::builder()
/// .key_extractor(|req| req.id.clone())
/// .max_size(100)
/// .ttl(Duration::from_secs(300))
/// .on_eviction(move || {
/// let count = counter.fetch_add(1, Ordering::SeqCst);
/// println!("Entry evicted (total: {})", count + 1);
/// })
/// .build();
/// ```
pub fn on_eviction<F>(mut self, f: F) -> Self
where
F: Fn() + Send + Sync + 'static,
{
self.event_listeners.add(FnListener::new(move |event| {
if matches!(event, CacheEvent::Eviction { .. }) {
f();
}
}));
self
}
/// Builds the cache layer.
///
/// # Panics
///
/// Panics if `key_extractor` was not set.
pub fn build(self) -> crate::CacheLayer<Req, K> {
let key_extractor = self
.key_extractor
.expect("key_extractor must be set before building");
let config = CacheConfig {
max_size: self.max_size,
ttl: self.ttl,
eviction_policy: self.eviction_policy,
key_extractor,
event_listeners: self.event_listeners,
name: self.name,
};
crate::CacheLayer::new(config)
}
}
impl<Req, K> Default for CacheConfigBuilder<Req, K>
where
K: Hash + Eq + Clone + Send + 'static,
{
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CacheLayer;
#[derive(Clone, Hash, Eq, PartialEq)]
struct TestRequest {
id: String,
}
#[test]
fn test_builder_defaults() {
let _layer = CacheLayer::<TestRequest, String>::builder()
.key_extractor(|req| req.id.clone())
.build();
// If this compiles and doesn't panic, the builder works
}
#[test]
fn test_builder_custom_values() {
let _layer = CacheLayer::<TestRequest, String>::builder()
.max_size(500)
.ttl(Duration::from_secs(60))
.key_extractor(|req| req.id.clone())
.name("my-cache")
.build();
// If this compiles and doesn't panic, the builder works
}
#[test]
fn test_event_listeners() {
let _layer = CacheLayer::<TestRequest, String>::builder()
.key_extractor(|req| req.id.clone())
.on_hit(|| {})
.on_miss(|| {})
.on_eviction(|| {})
.build();
// If this compiles and doesn't panic, the event listener registration works
}
#[test]
#[should_panic(expected = "key_extractor must be set")]
fn test_builder_panics_without_key_extractor() {
let _config = CacheLayer::<TestRequest, String>::builder().build();
}
}