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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use async_lock::RwLock;
use azure_core::time::Duration;
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::time::Instant;
/// Cache entry with optional TTL tracking
#[derive(Clone, Debug)]
struct CacheEntry<V> {
value: V,
expires_at: Option<Instant>,
}
impl<V> CacheEntry<V> {
fn new(value: V, ttl: Option<Duration>) -> Self {
Self {
value,
expires_at: ttl.map(|d| Instant::now() + d),
}
}
fn is_expired(&self) -> bool {
self.expires_at
.map(|exp| Instant::now() >= exp)
.unwrap_or(false)
}
}
/// A generic async cache with optional TTL support.
///
/// When created with `new()`, entries expire after the specified TTL.
#[derive(Clone)]
pub(crate) struct AsyncCache<K, V>
where
K: Eq + Hash + Clone,
V: Clone,
{
store: Arc<RwLock<HashMap<K, CacheEntry<V>>>>,
ttl: Option<Duration>,
}
impl<K, V> AsyncCache<K, V>
where
K: Eq + Hash + Clone + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
{
/// Creates a new `AsyncCache` with an optional TTL.
///
/// # Arguments
/// * `ttl` - Optional time-to-live for cache entries. If `None`, entries never expire.
pub fn new(ttl: Option<Duration>) -> Self {
Self {
store: Arc::new(RwLock::new(HashMap::new())),
ttl,
}
}
/// Gets a value from the cache, or computes it using the provided async function if not present or expired
///
/// When the entry is expired or a force_refresh is requested, the cache is automatically updated
/// with the newly computed value.
///
/// # Arguments
/// * `key` - The cache key to look up
/// * `should_refresh` - Callback function that receives the cached value (if any) and returns true
/// if the cache should be refreshed. Receives `Some(&V)` if there's a cached value, or `None` if not.
/// * `compute` - Async function to compute the value if not cached or refresh is requested
pub async fn get<F, Fut, E, R>(&self, key: K, should_refresh: R, compute: F) -> Result<V, E>
where
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = Result<V, E>>,
R: FnOnce(Option<&V>) -> bool,
{
// First, check what's in the cache and determine if we need to refresh
let (cached_value, force_refresh) = {
let store = self.store.read().await;
match store.get(&key) {
Some(entry) if !entry.is_expired() => (
Some(entry.value.clone()),
should_refresh(Some(&entry.value)),
),
Some(entry) => {
// Entry exists but is expired - still pass it to should_refresh for inspection
(None, should_refresh(Some(&entry.value)))
}
None => (None, should_refresh(None)),
}
};
// Fast path: return cached value if it exists and no refresh is needed
if !force_refresh {
if let Some(value) = cached_value {
return Ok(value);
}
}
// Slow path: value missing, expired, or force refresh requested - need to compute (write lock)
let mut store = self.store.write().await;
// Double-check after acquiring write lock (another task might have updated it)
// Only skip recompute if not force_refresh and entry is still valid
if !force_refresh {
if let Some(entry) = store.get(&key) {
if entry.is_expired() {
// Remove the entry from the cache.
store.remove(&key);
} else {
// Another task updated it while we waited for the lock
return Ok(entry.value.clone());
}
}
} else {
// force_refresh is true, remove the existing entry to ensure fresh computation
store.remove(&key);
}
// Compute new value
let value = compute().await?;
// Update cache with new value
let entry = CacheEntry::new(value.clone(), self.ttl);
store.insert(key, entry);
Ok(value)
}
/// Inserts a value directly into the cache.
#[allow(dead_code)] // Fundamental cache operation, will be used again
pub async fn insert(&self, key: K, value: V) {
let mut store = self.store.write().await;
let entry = CacheEntry::new(value, self.ttl);
store.insert(key, entry);
}
/// Removes a value from the cache
/// Returns the removed value if it existed and wasn't expired
pub async fn remove(&self, key: &K) -> Option<V> {
let mut store = self.store.write().await;
if let Some(entry) = store.remove(key) {
if !entry.is_expired() {
return Some(entry.value);
}
}
None
}
}
impl<K, V> std::fmt::Debug for AsyncCache<K, V>
where
K: Eq + Hash + Clone + std::fmt::Debug,
V: Clone + std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AsyncCache")
.field("ttl", &self.ttl)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
#[tokio::test]
async fn get_and_compute() {
let cache = AsyncCache::new(Some(Duration::seconds(60)));
let compute_count = Arc::new(AtomicUsize::new(0));
let count_clone = compute_count.clone();
// First get - should compute
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value1".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value1");
assert_eq!(compute_count.load(Ordering::SeqCst), 1);
// Second get - should return cached value
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value2".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value1");
assert_eq!(compute_count.load(Ordering::SeqCst), 1); // Not incremented
}
#[tokio::test]
async fn key_expiration() {
let cache = AsyncCache::new(Some(Duration::seconds(60)));
// Add entry
cache
.get(
"key1".to_string(),
|_| false,
|| async { Ok::<String, &str>("value1".to_string()) },
)
.await
.unwrap();
// Manually expire the cache entry by setting expires_at to a past time
{
let mut store = cache.store.write().await;
if let Some(entry) = store.get_mut(&"key1".to_string()) {
entry.expires_at = Some(Instant::now() - Duration::seconds(1));
}
}
// Get again - should recompute after expiration
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async { Ok::<String, &str>("value2".to_string()) },
)
.await
.unwrap();
assert_eq!(value, "value2");
}
#[tokio::test]
async fn key_remove() {
let cache = AsyncCache::new(Some(Duration::seconds(60)));
// Add entry
cache
.get(
"key1".to_string(),
|_| false,
|| async { Ok::<String, &str>("value1".to_string()) },
)
.await
.unwrap();
// Remove
let removed = cache.remove(&"key1".to_string()).await;
assert_eq!(removed, Some("value1".to_string()));
// Verify it's gone
let compute_count = Arc::new(AtomicUsize::new(0));
let count_clone = compute_count.clone();
cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value2".to_string())
},
)
.await
.unwrap();
assert_eq!(compute_count.load(Ordering::SeqCst), 1); // Recomputed
}
#[tokio::test]
async fn force_refresh() {
let cache = AsyncCache::new(Some(Duration::seconds(60)));
let compute_count = Arc::new(AtomicUsize::new(0));
let count_clone = compute_count.clone();
// First get - should compute
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value1".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value1");
assert_eq!(compute_count.load(Ordering::SeqCst), 1);
// Second get without force_refresh - should return cached value
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value2".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value1");
assert_eq!(compute_count.load(Ordering::SeqCst), 1); // Not incremented
// Third get WITH force_refresh (callback returns true) - should recompute
let value = cache
.get(
"key1".to_string(),
|_| true,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value3".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value3");
assert_eq!(compute_count.load(Ordering::SeqCst), 2); // Incremented due to force_refresh
// Fourth get without force_refresh - should return newly cached value
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value4".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value3");
assert_eq!(compute_count.load(Ordering::SeqCst), 2); // Not incremented
}
#[tokio::test]
async fn conditional_refresh_based_on_cached_value() {
let cache = AsyncCache::new(Some(Duration::seconds(60)));
// First get - cache is empty, should_refresh receives None
let value = cache
.get(
"key1".to_string(),
|cached| {
assert!(cached.is_none()); // No cached value yet
false
},
|| async { Ok::<String, &str>("value1".to_string()) },
)
.await
.unwrap();
assert_eq!(value, "value1");
// Second get - cache has value, should_refresh receives Some
let value = cache
.get(
"key1".to_string(),
|cached| {
assert_eq!(cached, Some(&"value1".to_string()));
false // Don't refresh
},
|| async { Ok::<String, &str>("value2".to_string()) },
)
.await
.unwrap();
assert_eq!(value, "value1"); // Still the original value
// Third, get - conditionally refresh based on cached value content
let value = cache
.get(
"key1".to_string(),
|cached| {
// Refresh only if cached value is "value1"
cached.is_some_and(|v| v == "value1")
},
|| async { Ok::<String, &str>("value3".to_string()) },
)
.await
.unwrap();
assert_eq!(value, "value3"); // Refreshed because condition was met
}
#[tokio::test]
async fn non_expiring_cache() {
let cache = AsyncCache::new(None);
let compute_count = Arc::new(AtomicUsize::new(0));
let count_clone = compute_count.clone();
// First get - should compute
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value1".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value1");
assert_eq!(compute_count.load(Ordering::SeqCst), 1);
// Verify entry has no expiration
{
let store = cache.store.read().await;
let entry = store.get(&"key1".to_string()).unwrap();
assert!(entry.expires_at.is_none());
assert!(!entry.is_expired()); // Never expires
}
// Second get - should return cached value (never expires)
let value = cache
.get(
"key1".to_string(),
|_| false,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value2".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value1");
assert_eq!(compute_count.load(Ordering::SeqCst), 1); // Not incremented
// Force refresh still works on non-expiring cache
let value = cache
.get(
"key1".to_string(),
|_| true,
|| async {
count_clone.fetch_add(1, Ordering::SeqCst);
Ok::<String, &str>("value3".to_string())
},
)
.await
.unwrap();
assert_eq!(value, "value3");
assert_eq!(compute_count.load(Ordering::SeqCst), 2); // Incremented due to force_refresh
}
}