amalgam-cache 0.2.0

A robust, multi-level, fail-safe cache — an idiomatic Rust port of .NET's FusionCache (cache-stampede protection, fail-safe, soft/hard timeouts, eager refresh, L1+L2, backplane, tagging).
Documentation
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! End-to-end behavioural tests — the parity oracle.
//!
//! Each test pins one FusionCache behaviour and asserts `amalgam` reproduces it.
//! Expiration is driven by an injected [`ManualClock`] for determinism; factory
//! timeouts use the real tokio timer (they are wall-clock by nature).

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use amalgam::{
    Cache, CacheEvent, Clock, EagerThreshold, EntryOptions, Error, FactoryContext, ManualClock,
    MaybeValue, Tag, Timeout,
};

fn build<V: Clone + Send + Sync + 'static>() -> (Cache<V>, Arc<ManualClock>) {
    let clock = Arc::new(ManualClock::default());
    let dyn_clock: Arc<dyn Clock> = clock.clone();
    (Cache::builder().clock(dyn_clock).build(), clock)
}

fn fail_safe_opts() -> EntryOptions {
    EntryOptions::new(Duration::from_secs(10)).with_fail_safe(
        true,
        Some(Duration::from_secs(100)),
        Some(Duration::from_secs(30)),
    )
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stampede_runs_factory_once() {
    let cache: Cache<i32> = Cache::new();
    let calls = Arc::new(AtomicUsize::new(0));

    let mut handles = Vec::new();
    for _ in 0..32 {
        let cache = cache.clone();
        let calls = calls.clone();
        handles.push(tokio::spawn(async move {
            cache
                .get_or_set("hot", move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    tokio::time::sleep(Duration::from_millis(30)).await;
                    Ok(ctx.value(42))
                })
                .await
        }));
    }
    for h in handles {
        assert_eq!(h.await.unwrap().unwrap(), 42);
    }
    assert_eq!(
        calls.load(Ordering::SeqCst),
        1,
        "factory must run exactly once"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fail_safe_serves_stale_on_factory_error() {
    let (cache, clock) = build::<String>();
    let opts = fail_safe_opts();

    let primed = cache
        .get_or_set_with(
            "k",
            |ctx| async move { Ok(ctx.value("fresh".to_owned())) },
            opts.clone(),
        )
        .await
        .unwrap();
    assert_eq!(primed, "fresh");

    clock.advance(Duration::from_secs(20)); // logically stale, physically alive

    let served = cache
        .get_or_set_with("k", |ctx| async move { Err(ctx.fail("boom")) }, opts)
        .await
        .unwrap();
    assert_eq!(served, "fresh", "stale value reused as fail-safe fallback");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fail_safe_default_used_when_no_stale() {
    let cache: Cache<String> = Cache::new();
    let opts = EntryOptions::new(Duration::from_secs(10)).with_fail_safe(true, None, None);

    let served = cache
        .get_or_set_full(
            "k",
            |ctx| async move { Err(ctx.fail("boom")) },
            Some(opts),
            Box::from([]),
            MaybeValue::from_value("default".to_owned()),
        )
        .await
        .unwrap();
    assert_eq!(served, "default");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn factory_error_propagates_without_fail_safe() {
    let cache: Cache<String> = Cache::new();
    let result = cache
        .get_or_set("k", |ctx| async move { Err(ctx.fail("boom")) })
        .await;
    assert!(matches!(result, Err(Error::Factory { .. })));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn soft_timeout_returns_stale_then_completes_in_background() {
    let (cache, clock) = build::<String>();
    let opts = EntryOptions::new(Duration::from_secs(10))
        .with_fail_safe(
            true,
            Some(Duration::from_secs(100)),
            Some(Duration::from_secs(1)),
        )
        .with_factory_timeouts(
            Timeout::After(Duration::from_millis(50)),
            Timeout::Infinite,
            true,
        );

    cache
        .get_or_set_with(
            "k",
            |ctx| async move { Ok(ctx.value("v1".to_owned())) },
            opts.clone(),
        )
        .await
        .unwrap();
    clock.advance(Duration::from_secs(20)); // stale

    let served = cache
        .get_or_set_with(
            "k",
            |ctx| async move {
                tokio::time::sleep(Duration::from_millis(300)).await;
                Ok(ctx.value("v2".to_owned()))
            },
            opts,
        )
        .await
        .unwrap();
    assert_eq!(served, "v1", "soft timeout returns stale immediately");

    // Background factory completes and replaces the value.
    tokio::time::sleep(Duration::from_millis(500)).await;
    let after = cache.try_get("k", None).await;
    assert_eq!(
        after.value(),
        Some(&"v2".to_owned()),
        "background completion updates cache"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn hard_timeout_without_fallback_errors() {
    let cache: Cache<String> = Cache::new();
    let opts = EntryOptions::new(Duration::from_secs(10)).with_factory_timeouts(
        Timeout::Infinite,
        Timeout::After(Duration::from_millis(50)),
        false,
    );
    let result = cache
        .get_or_set_with(
            "k",
            |ctx| async move {
                tokio::time::sleep(Duration::from_millis(300)).await;
                Ok(ctx.value("v".to_owned()))
            },
            opts,
        )
        .await;
    assert!(matches!(result, Err(Error::FactoryTimeout { .. })));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn eager_refresh_triggers_background_update() {
    let (cache, clock) = build::<i32>();
    let threshold = EagerThreshold::new(0.5).unwrap();
    let opts = EntryOptions::new(Duration::from_secs(10)).with_eager_refresh(Some(threshold));
    let calls = Arc::new(AtomicUsize::new(0));

    {
        let calls = calls.clone();
        cache
            .get_or_set_with(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(1))
                },
                opts.clone(),
            )
            .await
            .unwrap();
    }

    clock.advance(Duration::from_secs(6)); // past 50% threshold, still fresh

    {
        let calls = calls.clone();
        let served = cache
            .get_or_set_with(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(2))
                },
                opts,
            )
            .await
            .unwrap();
        assert_eq!(
            served, 1,
            "still returns the fresh value while refreshing in background"
        );
    }

    tokio::time::sleep(Duration::from_millis(150)).await;
    assert_eq!(
        calls.load(Ordering::SeqCst),
        2,
        "eager refresh ran the factory in background"
    );
    assert_eq!(cache.try_get("k", None).await.value(), Some(&2));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn expired_entry_without_fail_safe_reruns_factory() {
    let (cache, clock) = build::<i32>();
    let opts = EntryOptions::new(Duration::from_secs(10)); // no fail-safe
    let calls = Arc::new(AtomicUsize::new(0));

    {
        let calls = calls.clone();
        cache
            .get_or_set_with(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(1))
                },
                opts.clone(),
            )
            .await
            .unwrap();
    }
    clock.advance(Duration::from_secs(11)); // logically expired

    let v = {
        let calls = calls.clone();
        cache
            .get_or_set_with(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(2))
                },
                opts,
            )
            .await
            .unwrap()
    };
    assert_eq!(v, 2);
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn adaptive_caching_overrides_duration() {
    let (cache, clock) = build::<i32>();
    let long = || EntryOptions::new(Duration::from_secs(100));

    // The factory adapts the produced entry's duration down to 2s.
    cache
        .get_or_set_with(
            "k",
            |mut ctx: FactoryContext<i32>| async move {
                ctx.adapt(|o| o.with_duration(Duration::from_secs(2)));
                Ok(ctx.value(42))
            },
            long(),
        )
        .await
        .unwrap();

    clock.advance(Duration::from_secs(1)); // within adapted 2s ⇒ cached
    let cached = cache
        .get_or_set_with("k", |ctx| async move { Ok(ctx.value(999)) }, long())
        .await
        .unwrap();
    assert_eq!(cached, 42, "adapted duration still fresh");

    clock.advance(Duration::from_secs(2)); // now past adapted 2s ⇒ re-run
    let refreshed = cache
        .get_or_set_with("k", |ctx| async move { Ok(ctx.value(7)) }, long())
        .await
        .unwrap();
    assert_eq!(
        refreshed, 7,
        "adapted duration governs expiration, not the 100s call duration"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn conditional_refresh_not_modified_reuses_stale_and_bumps_expiration() {
    let (cache, clock) = build::<String>();
    let opts = fail_safe_opts();

    cache
        .get_or_set_with(
            "k",
            |ctx| async move { Ok(ctx.modified("data-v1".to_owned()).etag("abc").done()) },
            opts.clone(),
        )
        .await
        .unwrap();
    clock.advance(Duration::from_secs(20)); // stale

    let calls = Arc::new(AtomicUsize::new(0));
    let served = {
        let calls = calls.clone();
        cache
            .get_or_set_with(
                "k",
                move |ctx: FactoryContext<String>| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    assert_eq!(
                        ctx.stale_etag(),
                        Some("abc"),
                        "stale ETag is exposed to factory"
                    );
                    ctx.not_modified()
                },
                opts,
            )
            .await
            .unwrap()
    };
    assert_eq!(served, "data-v1", "NotModified reuses the stale value");
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    // Expiration was bumped ⇒ now fresh again at the current clock.
    assert_eq!(
        cache.try_get("k", None).await.value(),
        Some(&"data-v1".to_owned())
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn conditional_refresh_modified_replaces_value() {
    let (cache, clock) = build::<String>();
    let opts = fail_safe_opts();

    cache
        .get_or_set_with(
            "k",
            |ctx| async move { Ok(ctx.modified("data-v1".to_owned()).etag("abc").done()) },
            opts.clone(),
        )
        .await
        .unwrap();
    clock.advance(Duration::from_secs(20));

    let served = cache
        .get_or_set_with(
            "k",
            |ctx: FactoryContext<String>| async move {
                Ok(ctx.modified("data-v2".to_owned()).etag("def").done())
            },
            opts,
        )
        .await
        .unwrap();
    assert_eq!(served, "data-v2");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn remove_by_tag_invalidates_matching_entries() {
    let (cache, clock) = build::<i32>();
    let long = || EntryOptions::new(Duration::from_secs(100));
    let calls = Arc::new(AtomicUsize::new(0));
    let tagged = || -> Box<[Tag]> { Box::from([Tag::new("group").unwrap()]) };

    {
        let calls = calls.clone();
        cache
            .get_or_set_full(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(1))
                },
                Some(long()),
                tagged(),
                MaybeValue::none(),
            )
            .await
            .unwrap();
    }

    clock.advance(Duration::from_secs(1));
    cache.remove_by_tag("group").await;
    clock.advance(Duration::from_secs(1)); // so the new entry is created after the marker

    let v = {
        let calls = calls.clone();
        cache
            .get_or_set_full(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(2))
                },
                Some(long()),
                tagged(),
                MaybeValue::none(),
            )
            .await
            .unwrap()
    };
    assert_eq!(v, 2, "tagged entry was invalidated and the factory re-ran");
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn disable_tagging_ignores_remove_by_tag() {
    let clock = Arc::new(ManualClock::default());
    let dyn_clock: Arc<dyn Clock> = clock.clone();
    let cache: Cache<i32> = Cache::builder()
        .clock(dyn_clock)
        .disable_tagging(true)
        .build();
    let long = || EntryOptions::new(Duration::from_secs(100));
    let calls = Arc::new(AtomicUsize::new(0));
    let tagged = || -> Box<[Tag]> { Box::from([Tag::new("group").unwrap()]) };

    {
        let calls = calls.clone();
        cache
            .get_or_set_full(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(1))
                },
                Some(long()),
                tagged(),
                MaybeValue::none(),
            )
            .await
            .unwrap();
    }

    clock.advance(Duration::from_secs(1));
    cache.remove_by_tag("group").await; // ignored: tagging is disabled
    clock.advance(Duration::from_secs(1));

    let v = {
        let calls = calls.clone();
        cache
            .get_or_set_full(
                "k",
                move |ctx| async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(ctx.value(2))
                },
                Some(long()),
                tagged(),
                MaybeValue::none(),
            )
            .await
            .unwrap()
    };
    assert_eq!(
        v, 1,
        "with tagging disabled, remove_by_tag is ignored and the entry survives"
    );
    assert_eq!(calls.load(Ordering::SeqCst), 1, "factory ran only once");
}

#[test]
fn wait_for_initial_backplane_subscribe_defaults_true_and_is_configurable() {
    let on: Cache<i32> = Cache::new();
    assert!(on.wait_for_initial_backplane_subscribe());
    let off: Cache<i32> = Cache::builder()
        .wait_for_initial_backplane_subscribe(false)
        .build();
    assert!(!off.wait_for_initial_backplane_subscribe());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn clear_removes_everything() {
    let (cache, _clock) = build::<i32>();
    cache.set("a", 1).await;
    cache.set("b", 2).await;
    assert_eq!(cache.try_get("a", None).await.value(), Some(&1));

    cache.clear(false).await; // hard remove
    cache.run_pending_tasks().await;
    assert!(!cache.try_get("a", None).await.has_value());
    assert!(!cache.try_get("b", None).await.has_value());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn try_get_and_get_or_default() {
    let cache: Cache<i32> = Cache::new();
    assert!(!cache.try_get("k", None).await.has_value());
    assert_eq!(cache.get_or_default("k", -1, None).await, -1);

    cache.set("k", 5).await;
    assert_eq!(cache.try_get("k", None).await.value(), Some(&5));
    assert_eq!(cache.get_or_default("k", -1, None).await, 5);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn allow_stale_on_read_only_serves_stale() {
    let (cache, clock) = build::<i32>();
    let opts = fail_safe_opts();
    cache
        .get_or_set_with("k", |ctx| async move { Ok(ctx.value(9)) }, opts)
        .await
        .unwrap();
    clock.advance(Duration::from_secs(20)); // stale

    // Default read-only: stale hidden.
    assert!(!cache.try_get("k", None).await.has_value());
    // With allow_stale_on_read_only: stale visible.
    let stale_opts = EntryOptions::new(Duration::from_secs(10)).with_allow_stale_on_read_only(true);
    assert_eq!(cache.try_get("k", Some(stale_opts)).await.value(), Some(&9));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn events_are_emitted() {
    let cache: Cache<i32> = Cache::new();
    let mut rx = cache.events().subscribe();

    cache.set("k", 1).await;
    let _ = cache
        .get_or_set("k", |ctx| async move { Ok(ctx.value(1)) })
        .await
        .unwrap();

    let mut saw_set = false;
    let mut saw_fresh_hit = false;
    // Drain what is buffered.
    for _ in 0..8 {
        match rx.try_recv() {
            Ok(CacheEvent::Set { .. }) => saw_set = true,
            Ok(CacheEvent::Hit { stale: false, .. }) => saw_fresh_hit = true,
            Ok(_) => {}
            Err(_) => break,
        }
    }
    assert!(saw_set, "a Set event was emitted");
    assert!(saw_fresh_hit, "a fresh Hit event was emitted");
}