oxcache 0.5.0-rc.7

A production-grade multi-level cache library for Rust: L1 memory (Moka/DashMap) + L2 distributed (Redis/Valkey/Dragonfly/Aerospike) + optional L3 disk (redb).
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: MIT
//! `Cache<K,V>` sync 面长尾与 stale 集成路径覆盖:
//!
//! - sync 杂项方法(clear/health_check/shutdown/stats/len/capacity_sync)
//! - `get_or_option_sync` 全分支(无 null_ttl / 有 null_ttl 哨兵短路 /
//!   fallback Err 透传 / with_ttl 包装)
//! - `get_or_sync` 在未启用 sync_mode 时的显性报错与 guard 清理
//! - `stale_ttl` 三种策略下的 stale 命中语义(Return / Revalidate /
//!   OffloadRevalidate 后台刷新)

#![cfg(feature = "full")]

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

use oxcache::Cache;
use oxcache::features::stale::StalePolicy;

// ============================================================================
// sync 杂项面
// ============================================================================

#[tokio::test(flavor = "multi_thread")]
async fn sync_misc_surface() {
    let cache: Cache<String, String> = Cache::builder().sync_mode(true).build().await.unwrap();

    cache.set_sync(&"a".to_string(), &"1".to_string()).unwrap();
    cache.set_sync(&"b".to_string(), &"2".to_string()).unwrap();

    // Moka entry_count 最终一致,写后立即读可能滞后——只调用不断言数值
    let _ = cache.len_sync().unwrap();
    let _cap = cache.capacity_sync().unwrap();
    let stats = cache.stats_sync().unwrap();
    let _ = stats;
    cache.health_check_sync().unwrap();

    cache.clear_sync().unwrap();
    let _ = cache.len_sync().unwrap();

    // shutdown 同步面:no-op 安全
    cache.shutdown_sync();
}

// ============================================================================
// get_or_option_sync 全分支
// ============================================================================

#[tokio::test(flavor = "multi_thread")]
async fn get_or_option_sync_value_none_and_error() {
    let cache: Cache<String, String> = Cache::builder().sync_mode(true).build().await.unwrap();

    // fallback Some → 缓存并返回
    let calls = AtomicU32::new(0);
    let v = cache
        .get_or_option_sync(&"k:1".to_string(), || {
            calls.fetch_add(1, Ordering::SeqCst);
            Ok(Some("v1".to_string()))
        })
        .unwrap();
    assert_eq!(v.as_deref(), Some("v1"));
    // 二次调用命中缓存,fallback 不再执行
    let v2 = cache
        .get_or_option_sync(&"k:1".to_string(), || {
            calls.fetch_add(1, Ordering::SeqCst);
            Ok(Some("other".to_string()))
        })
        .unwrap();
    assert_eq!(v2.as_deref(), Some("v1"));
    assert_eq!(calls.load(Ordering::SeqCst), 1);

    // fallback None(未配置 null_ttl)→ 返回 None,不缓存哨兵
    let n = cache
        .get_or_option_sync(&"k:2".to_string(), || {
            Ok::<Option<String>, oxcache::OxCacheError>(None)
        })
        .unwrap();
    assert!(n.is_none());

    // fallback Err → 显性透传
    let err = cache
        .get_or_option_sync(&"k:3".to_string(), || {
            Err(oxcache::OxCacheError::L1Error("boom".to_string()))
        })
        .unwrap_err();
    assert!(err.to_string().contains("boom"));
}

#[tokio::test(flavor = "multi_thread")]
async fn get_or_option_sync_null_sentinel_short_circuit() {
    let cache: Cache<String, String> = Cache::builder()
        .sync_mode(true)
        .null_cache_ttl(Duration::from_secs(30))
        .build()
        .await
        .unwrap();

    let calls = AtomicU32::new(0);
    let first = cache
        .get_or_option_sync(&"nk".to_string(), || {
            calls.fetch_add(1, Ordering::SeqCst);
            Ok::<Option<String>, oxcache::OxCacheError>(None)
        })
        .unwrap();
    assert!(first.is_none());
    assert_eq!(calls.load(Ordering::SeqCst), 1);

    // 哨兵已缓存 → 第二次调用短路返回 None,不再执行 fallback
    let second = cache
        .get_or_option_sync(&"nk".to_string(), || {
            calls.fetch_add(1, Ordering::SeqCst);
            Ok::<Option<String>, oxcache::OxCacheError>(None)
        })
        .unwrap();
    assert!(second.is_none());
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test(flavor = "multi_thread")]
async fn get_or_option_with_ttl_sync_variants() {
    let cache: Cache<String, String> = Cache::builder()
        .sync_mode(true)
        .null_cache_ttl(Duration::from_secs(30))
        .build()
        .await
        .unwrap();

    // with_ttl 包装:值路径
    let v = cache
        .get_or_option_with_ttl_sync(&"tk".to_string(), Some(Duration::from_secs(60)), || {
            Ok(Some("tv".to_string()))
        })
        .unwrap();
    assert_eq!(v.as_deref(), Some("tv"));

    // with_ttl 包装:None + 哨兵(jitter 分支在 null_cache_ttl 存在时走 apply_jitter)
    let n = cache
        .get_or_option_with_ttl_sync(&"tn".to_string(), Some(Duration::from_secs(60)), || {
            Ok::<Option<String>, oxcache::OxCacheError>(None)
        })
        .unwrap();
    assert!(n.is_none());
    let n2 = cache
        .get_or_option_with_ttl_sync(&"tn".to_string(), Some(Duration::from_secs(60)), || {
            Ok::<Option<String>, oxcache::OxCacheError>(None)
        })
        .unwrap();
    assert!(n2.is_none());
}

#[tokio::test(flavor = "multi_thread")]
async fn sync_api_without_sync_mode_fails_explicitly() {
    // 未启用 sync_mode → sync 面显性报错(不 panic、不静默)
    let cache: Cache<String, String> = Cache::builder().build().await.unwrap();
    let err = cache
        .get_or_sync(&"k".to_string(), || Ok("v".to_string()))
        .unwrap_err();
    assert!(!err.to_string().is_empty());
    let err = cache.get_sync(&"k".to_string()).unwrap_err();
    assert!(!err.to_string().is_empty());
}

// ============================================================================
// get_or_sync 竞争路径(leader/follower)
// ============================================================================

#[tokio::test(flavor = "multi_thread")]
async fn get_or_sync_concurrent_leader_follower() {
    let cache: Arc<Cache<String, String>> =
        Arc::new(Cache::builder().sync_mode(true).build().await.unwrap());

    let calls = Arc::new(AtomicU32::new(0));
    let mut handles = Vec::new();
    for _ in 0..4 {
        let cache = Arc::clone(&cache);
        let calls = Arc::clone(&calls);
        handles.push(tokio::task::spawn_blocking(move || {
            cache
                .get_or_sync(&"contended".to_string(), || {
                    calls.fetch_add(1, Ordering::SeqCst);
                    std::thread::sleep(Duration::from_millis(120));
                    Ok("computed".to_string())
                })
                .unwrap()
        }));
    }
    for h in handles {
        assert_eq!(h.await.unwrap(), "computed");
    }
    // single-flight:fallback 实际执行次数远小于并发调用数
    let executed = calls.load(Ordering::SeqCst);
    assert!((1..=4).contains(&executed), "executed={executed}");
    // leader 已缓存 → 后续调用不再回源
    let again = cache
        .get_or_sync(&"contended".to_string(), || {
            calls.fetch_add(1, Ordering::SeqCst);
            Ok("should-not-run".to_string())
        })
        .unwrap();
    assert_eq!(again, "computed");
}

// ============================================================================
// stale 三策略(stale_ttl 启用 SWR 三态过期)
// ============================================================================

async fn build_stale_cache(policy: StalePolicy) -> Cache<String, String> {
    // stale 窗口拉宽到 2s:测试以短 sleep 越过 TTL 后落在 Stale 态,
    // 避免 llvm-cov 插桩/调度抖动下滑出 150ms 级窄窗误入 Expired 分支
    Cache::builder()
        .stale_ttl(Duration::from_secs(2))
        .stale_policy(policy)
        .build()
        .await
        .unwrap()
}

#[tokio::test(flavor = "multi_thread")]
async fn stale_return_policy_serves_old_value() {
    let cache = build_stale_cache(StalePolicy::Return).await;
    let calls = AtomicU32::new(0);

    let v = cache
        .get_or_with_ttl(&"s:1".to_string(), Some(Duration::from_millis(80)), || {
            calls.fetch_add(1, Ordering::SeqCst);
            async { Ok("fresh1".to_string()) }
        })
        .await
        .unwrap();
    assert_eq!(v, "fresh1");

    // 等待过期进入 stale 窗口 → get_or 命中 stale,返回旧值且不回源
    tokio::time::sleep(Duration::from_millis(200)).await;
    let v2 = cache
        .get_or_with_ttl(&"s:1".to_string(), Some(Duration::from_millis(80)), || {
            calls.fetch_add(1, Ordering::SeqCst);
            async { Ok("fresh2".to_string()) }
        })
        .await
        .unwrap();
    assert_eq!(v2, "fresh1", "Return 策略:stale 命中返回旧值");
    assert_eq!(calls.load(Ordering::SeqCst), 1, "stale 命中不执行 fallback");
}

#[tokio::test(flavor = "multi_thread")]
async fn stale_revalidate_policy_treats_as_miss() {
    let cache = build_stale_cache(StalePolicy::Revalidate).await;
    let calls = AtomicU32::new(0);

    let v = cache
        .get_or_with_ttl(&"s:2".to_string(), Some(Duration::from_millis(80)), || {
            calls.fetch_add(1, Ordering::SeqCst);
            async { Ok("v1".to_string()) }
        })
        .await
        .unwrap();
    assert_eq!(v, "v1");

    // stale 窗口内 → 视同 miss:删除 stale 条目并回源
    tokio::time::sleep(Duration::from_millis(200)).await;
    let v2 = cache
        .get_or_with_ttl(&"s:2".to_string(), Some(Duration::from_millis(80)), || {
            calls.fetch_add(1, Ordering::SeqCst);
            async { Ok("v2".to_string()) }
        })
        .await
        .unwrap();
    assert_eq!(v2, "v2", "Revalidate 策略:stale 视同 miss 并刷新");
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}

#[tokio::test(flavor = "multi_thread")]
async fn stale_offload_policy_returns_old_and_refreshes_background() {
    let cache = build_stale_cache(StalePolicy::OffloadRevalidate).await;
    let calls = Arc::new(AtomicU32::new(0));

    let v = cache
        .get_or_refresh(&"s:3".to_string(), Some(Duration::from_millis(80)), {
            let calls = Arc::clone(&calls);
            move || {
                calls.fetch_add(1, Ordering::SeqCst);
                async move { Ok("gen1".to_string()) }
            }
        })
        .await
        .unwrap();
    assert_eq!(v, "gen1");

    // stale 窗口内 → 立即返回旧值,fallback 交由 offload 后台执行
    tokio::time::sleep(Duration::from_millis(200)).await;
    let v2 = cache
        .get_or_refresh(&"s:3".to_string(), Some(Duration::from_millis(80)), {
            let calls = Arc::clone(&calls);
            move || {
                calls.fetch_add(1, Ordering::SeqCst);
                async move { Ok("gen2".to_string()) }
            }
        })
        .await
        .unwrap();
    assert_eq!(v2, "gen1", "OffloadRevalidate:立即返回旧值");
    assert_eq!(calls.load(Ordering::SeqCst), 1);

    // 等待后台刷新落盘后,再次 get 应得到新值
    for _ in 0..20 {
        tokio::time::sleep(Duration::from_millis(100)).await;
        if let Ok(Some(v)) = cache.get(&"s:3".to_string()).await
            && v == "gen2"
        {
            return;
        }
    }
    panic!("offload 后台刷新未落盘");
}

#[tokio::test(flavor = "multi_thread")]
async fn stale_get_or_refresh_revalidate_and_return_paths() {
    // Return 策略下 get_or_refresh:stale 命中直接返回旧值
    let cache = build_stale_cache(StalePolicy::Return).await;
    let calls = Arc::new(AtomicU32::new(0));
    let v = cache
        .get_or_refresh(&"r:1".to_string(), Some(Duration::from_millis(80)), {
            let calls = Arc::clone(&calls);
            move || {
                calls.fetch_add(1, Ordering::SeqCst);
                async move { Ok("old".to_string()) }
            }
        })
        .await
        .unwrap();
    assert_eq!(v, "old");
    tokio::time::sleep(Duration::from_millis(200)).await;
    let v2 = cache
        .get_or_refresh(&"r:1".to_string(), Some(Duration::from_millis(80)), {
            let calls = Arc::clone(&calls);
            move || {
                calls.fetch_add(1, Ordering::SeqCst);
                async move { Ok("new".to_string()) }
            }
        })
        .await
        .unwrap();
    assert_eq!(v2, "old");
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test(flavor = "multi_thread")]
async fn stale_miss_falls_through_to_normal_get_or() {
    let cache = build_stale_cache(StalePolicy::Return).await;
    // 无任何历史条目:stale 查询 miss → 正常 get_or 路径
    let v = cache
        .get_or_with_ttl(&"s:none".to_string(), None, || async {
            Ok("plain".to_string())
        })
        .await
        .unwrap();
    assert_eq!(v, "plain");
}

// ============================================================================
// get_or / get_or_option 并发与哨兵分支补测
// ============================================================================

#[tokio::test(flavor = "multi_thread")]
async fn get_or_async_concurrent_follower_and_double_check() {
    let cache: Arc<Cache<String, String>> = Arc::new(Cache::builder().build().await.unwrap());
    let calls = Arc::new(AtomicU32::new(0));

    let mut handles = Vec::new();
    for i in 0..8 {
        let cache = Arc::clone(&cache);
        let calls = Arc::clone(&calls);
        handles.push(tokio::spawn(async move {
            cache
                .get_or(&"hot".to_string(), move || {
                    let calls = Arc::clone(&calls);
                    async move {
                        calls.fetch_add(1, Ordering::SeqCst);
                        tokio::time::sleep(Duration::from_millis(250)).await;
                        let _ = i;
                        Ok("computed".to_string())
                    }
                })
                .await
        }));
    }
    for h in handles {
        assert_eq!(h.await.unwrap().unwrap(), "computed");
    }
    let executed = calls.load(Ordering::SeqCst);
    assert!((1..=8).contains(&executed), "executed={executed}");
    // 后到者走 leader 双检命中(543-544):缓存已有值,fallback 不再执行
    let again = cache
        .get_or(&"hot".to_string(), || async { Ok("nope".to_string()) })
        .await
        .unwrap();
    assert_eq!(again, "computed");
}

#[tokio::test(flavor = "multi_thread")]
async fn get_or_option_async_follower_value_and_leader_failure() {
    let cache: Arc<Cache<String, String>> = Arc::new(Cache::builder().build().await.unwrap());

    // follower:leader 慢回源,follower 订阅后取缓存值(687)
    let c1 = Arc::clone(&cache);
    let leader = tokio::spawn(async move {
        c1.get_or_option(&"fv".to_string(), || async {
            tokio::time::sleep(Duration::from_millis(200)).await;
            Ok(Some("lv".to_string()))
        })
        .await
    });
    tokio::time::sleep(Duration::from_millis(50)).await;
    let follower_val = cache
        .get_or_option(&"fv".to_string(), || async {
            Ok(Some("ignored".to_string()))
        })
        .await
        .unwrap();
    assert_eq!(follower_val.as_deref(), Some("lv"));
    assert_eq!(leader.await.unwrap().unwrap().as_deref(), Some("lv"));

    // leader 回源失败:follower 醒来后无缓存 → 显性报错(695-699)
    let c2 = Arc::clone(&cache);
    let failing = tokio::spawn(async move {
        c2.get_or_option(&"ff".to_string(), || async {
            tokio::time::sleep(Duration::from_millis(200)).await;
            Err(oxcache::OxCacheError::L1Error("boom".to_string()))
        })
        .await
    });
    tokio::time::sleep(Duration::from_millis(50)).await;
    let err = cache
        .get_or_option(&"ff".to_string(), || async {
            Ok(Some("ignored".to_string()))
        })
        .await
        .unwrap_err();
    assert!(!err.to_string().is_empty());
    let _ = failing.await.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn get_or_option_with_ttl_async_value_path() {
    let cache: Cache<String, String> = Cache::builder().build().await.unwrap();
    let v = cache
        .get_or_option_with_ttl(
            &"ttl-obj".to_string(),
            Some(Duration::from_secs(30)),
            || async { Ok(Some("tv".to_string())) },
        )
        .await
        .unwrap();
    assert_eq!(v.as_deref(), Some("tv"));
}

#[tokio::test(flavor = "multi_thread")]
async fn get_or_option_sync_concurrent_follower_paths() {
    let cache: Arc<Cache<String, String>> =
        Arc::new(Cache::builder().sync_mode(true).build().await.unwrap());

    // follower 值路径(1186-1198 等待 + 1222-1223 返回)
    let mut handles = Vec::new();
    for _ in 0..6 {
        let cache = Arc::clone(&cache);
        handles.push(tokio::task::spawn_blocking(move || {
            cache
                .get_or_option_sync(&"of".to_string(), || {
                    std::thread::sleep(Duration::from_millis(200));
                    Ok(Some("sv".to_string()))
                })
                .unwrap()
        }));
    }
    for h in handles {
        assert_eq!(h.await.unwrap().as_deref(), Some("sv"));
    }

    // follower 哨兵路径:leader 缓存 null 哨兵,follower 返回 None(1200-1210)
    let sentinel_cache: Arc<Cache<String, String>> = Arc::new(
        Cache::builder()
            .sync_mode(true)
            .null_cache_ttl(Duration::from_secs(30))
            .build()
            .await
            .unwrap(),
    );
    let mut handles = Vec::new();
    for _ in 0..4 {
        let cache = Arc::clone(&sentinel_cache);
        handles.push(tokio::task::spawn_blocking(move || {
            cache
                .get_or_option_sync(&"ns".to_string(), || {
                    std::thread::sleep(Duration::from_millis(200));
                    Ok::<Option<String>, oxcache::OxCacheError>(None)
                })
                .unwrap()
        }));
    }
    for h in handles {
        assert!(h.await.unwrap().is_none());
    }
}

#[tokio::test(flavor = "multi_thread")]
async fn stale_sentinel_payload_treated_as_miss() {
    // stale 窗口拉宽到 2s,防插桩/调度抖动下滑出窄窗误入 Expired 分支
    let cache: Cache<String, String> = Cache::builder()
        .stale_ttl(Duration::from_secs(2))
        .stale_policy(StalePolicy::Return)
        .null_cache_ttl(Duration::from_millis(70))
        .build()
        .await
        .unwrap();

    // 哨兵写入(70ms TTL)
    let none = cache
        .get_or_option(&"sn".to_string(), || async {
            Ok::<Option<String>, oxcache::OxCacheError>(None)
        })
        .await
        .unwrap();
    assert!(none.is_none());

    // 过期进入 stale 窗口后 get_or:stale_step_basic 命中哨兵载荷 → 按 miss 处理
    tokio::time::sleep(Duration::from_millis(150)).await;
    let calls = AtomicU32::new(0);
    let v = cache
        .get_or(&"sn".to_string(), || {
            calls.fetch_add(1, Ordering::SeqCst);
            async { Ok("recomputed".to_string()) }
        })
        .await
        .unwrap();
    assert_eq!(v, "recomputed", "哨兵 stale 命中按 miss 重新回源");
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test(flavor = "multi_thread")]
async fn get_or_refresh_revalidate_deletes_stale_entry() {
    // stale 窗口拉宽到 2s,防插桩/调度抖动下滑出窄窗误入 Expired 分支
    let cache: Cache<String, String> = Cache::builder()
        .stale_ttl(Duration::from_secs(2))
        .stale_policy(StalePolicy::Revalidate)
        .build()
        .await
        .unwrap();
    let calls = Arc::new(AtomicU32::new(0));

    let v = cache
        .get_or_refresh(&"rr".to_string(), Some(Duration::from_millis(70)), {
            let calls = Arc::clone(&calls);
            move || {
                calls.fetch_add(1, Ordering::SeqCst);
                async move { Ok("gen1".to_string()) }
            }
        })
        .await
        .unwrap();
    assert_eq!(v, "gen1");

    // stale 窗口:Revalidate 删除 stale 条目并走 miss 回源(466)
    tokio::time::sleep(Duration::from_millis(120)).await;
    let v2 = cache
        .get_or_refresh(&"rr".to_string(), Some(Duration::from_millis(70)), {
            let calls = Arc::clone(&calls);
            move || {
                calls.fetch_add(1, Ordering::SeqCst);
                async move { Ok("gen2".to_string()) }
            }
        })
        .await
        .unwrap();
    assert_eq!(v2, "gen2");
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}