inklog 0.1.2

Enterprise-grade Rust logging infrastructure
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
// Copyright (c) 2026 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

//! Cache trait - 抽象缓存操作
//!
//! 提供 CRUD 操作的抽象接口,支持不同缓存后端的统一访问。

use crate::InklogError;
use async_trait::async_trait;

/// Cache trait - 抽象缓存操作
///
/// 提供基本的缓存 CRUD 操作接口,所有方法都是异步的。
/// 实现必须保证线程安全(`Send + Sync`)。
///
/// # 错误处理
///
/// 所有方法返回 `Result<_, InklogError>`,错误显性传播,不静默吞错。
/// 实现应该是非阻塞的,适合在 async 上下文中调用。
///
/// # 示例
///
/// ```ignore
/// use inklog::infrastructure::Cache;
///
/// async fn example(cache: &dyn Cache) -> Result<(), Box<dyn std::error::Error>> {
///     cache.set("key", "value".to_string()).await?;
///     let value = cache.get("key").await?;
///     assert_eq!(value, Some("value".to_string()));
///     Ok(())
/// }
/// ```
#[async_trait]
pub trait Cache: Send + Sync {
    /// 获取缓存值
    ///
    /// # 参数
    ///
    /// * `key` - 缓存键
    ///
    /// # 返回
    ///
    /// - `Ok(Some(value))` - 键存在
    /// - `Ok(None)` - 键不存在
    /// - `Err(InklogError)` - 缓存访问失败
    async fn get(&self, key: &str) -> Result<Option<String>, InklogError>;

    /// 设置缓存值
    ///
    /// # 参数
    ///
    /// * `key` - 缓存键
    /// * `value` - 缓存值
    ///
    /// # 返回
    ///
    /// 成功返回 `Ok(())`,失败返回 `Err(InklogError)`
    async fn set(&self, key: &str, value: String) -> Result<(), InklogError>;

    /// 删除缓存值
    ///
    /// # 参数
    ///
    /// * `key` - 缓存键
    ///
    /// # 返回
    ///
    /// - `Ok(true)` - 键存在并已删除
    /// - `Ok(false)` - 键不存在
    /// - `Err(InklogError)` - 缓存访问失败
    async fn delete(&self, key: &str) -> Result<bool, InklogError>;

    /// 检查键是否存在
    ///
    /// # 参数
    ///
    /// * `key` - 缓存键
    ///
    /// # 返回
    ///
    /// - `Ok(true)` - 键存在
    /// - `Ok(false)` - 键不存在
    /// - `Err(InklogError)` - 缓存访问失败
    async fn exists(&self, key: &str) -> Result<bool, InklogError>;
}

// ============================================================================
// OxCacheAdapter - oxcache 适配器实现
// ============================================================================

use oxcache::Cache as OxCache;
use std::collections::HashMap;
use std::sync::RwLock;

/// oxcache 适配器
///
/// 将 oxcache 库的 `Cache<K, V>` 适配为 `Cache` trait。
/// 使用 `Cache<String, String>` 作为底层存储类型。
///
/// # 示例
///
/// ```ignore
/// use inklog::infrastructure::cache::{Cache, OxCacheAdapter};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let cache = OxCacheAdapter::new()?;
///
///     cache.set("user:1", "Alice".to_string()).await?;
///     let name = cache.get("user:1").await?;
///     assert_eq!(name, Some("Alice".to_string()));
///
///     Ok(())
/// }
/// ```
pub struct OxCacheAdapter {
    inner: OxCache<String, String>,
}

impl OxCacheAdapter {
    /// 创建新的 oxcache 适配器
    ///
    /// 使用默认的内存后端创建缓存实例。
    ///
    /// # 返回
    ///
    /// 成功返回 `Ok(Self)`,失败返回 `Err(InklogError)`
    ///
    /// # 错误
    ///
    /// - `InklogError::CacheError` - 缓存初始化失败
    pub fn new() -> Result<Self, InklogError> {
        let cache = OxCache::new();
        Ok(Self { inner: cache })
    }

    /// 使用构建器创建适配器
    ///
    /// 提供更细粒度的配置选项,如 TTL、容量等。
    /// 必须在 async 上下文中调用 `build().await`。
    ///
    /// # 示例
    ///
    /// ```ignore
    /// use std::time::Duration;
    /// use inklog::infrastructure::cache::OxCacheAdapter;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let cache = OxCacheAdapter::builder()
    ///     .ttl(Duration::from_secs(3600))
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder() -> OxCacheAdapterBuilder {
        OxCacheAdapterBuilder::default()
    }
}

#[async_trait]
impl Cache for OxCacheAdapter {
    async fn get(&self, key: &str) -> Result<Option<String>, InklogError> {
        self.inner.get(&key.to_string()).await.map_err(|e| {
            InklogError::CacheError(format!("Failed to get cache key '{}': {}", key, e))
        })
    }

    async fn set(&self, key: &str, value: String) -> Result<(), InklogError> {
        self.inner.set(&key.to_string(), &value).await.map_err(|e| {
            InklogError::CacheError(format!("Failed to set cache key '{}': {}", key, e))
        })
    }

    async fn delete(&self, key: &str) -> Result<bool, InklogError> {
        // 先检查键是否存在,避免无谓的 delete 调用
        let exists = self.exists(key).await?;
        if !exists {
            return Ok(false);
        }
        self.inner.delete(&key.to_string()).await.map_err(|e| {
            InklogError::CacheError(format!("Failed to delete cache key '{}': {}", key, e))
        })?;
        Ok(true)
    }

    async fn exists(&self, key: &str) -> Result<bool, InklogError> {
        self.inner.exists(&key.to_string()).await.map_err(|e| {
            InklogError::CacheError(format!(
                "Failed to check existence of cache key '{}': {}",
                key, e
            ))
        })
    }
}

/// OxCacheAdapter 构建器
///
/// 提供链式配置 API。
#[derive(Default)]
pub struct OxCacheAdapterBuilder {
    ttl: Option<std::time::Duration>,
    capacity: Option<u64>,
}

impl OxCacheAdapterBuilder {
    /// 设置 TTL(生存时间)
    pub fn ttl(mut self, ttl: std::time::Duration) -> Self {
        self.ttl = Some(ttl);
        self
    }

    /// 设置缓存容量
    pub fn capacity(mut self, capacity: u64) -> Self {
        self.capacity = Some(capacity);
        self
    }

    /// 异步构建适配器
    ///
    /// 将配置的 TTL 和容量应用到 oxcache 后端。
    /// 必须在 async 上下文中调用,因为 oxcache 0.2.0 的
    /// `CacheBuilder::build()` 是异步方法。
    ///
    /// # 错误
    ///
    /// - `InklogError::CacheError` - oxcache 构建失败
    pub async fn build(self) -> Result<OxCacheAdapter, InklogError> {
        let mut builder = OxCache::builder();
        if let Some(ttl) = self.ttl {
            builder = builder.ttl(ttl);
        }
        if let Some(capacity) = self.capacity {
            builder = builder.capacity(capacity);
        }
        let cache = builder
            .build()
            .await
            .map_err(|e| InklogError::CacheError(format!("Failed to build oxcache: {}", e)))?;
        Ok(OxCacheAdapter { inner: cache })
    }
}

// ============================================================================
// MockCache - 单元测试 Mock 实现
// ============================================================================

/// Mock 缓存实现,用于单元测试
///
/// 提供基于内存的简单缓存实现,支持可选的延迟模拟。
/// 使用 `RwLock` 保护内部存储,确保线程安全。
///
/// # 示例
///
/// ```ignore
/// use inklog::infrastructure::cache::{Cache, MockCache};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let cache = MockCache::new();
///
///     cache.set("key", "value".to_string()).await?;
///     assert_eq!(cache.get("key").await?, Some("value".to_string()));
///
///     // 带延迟的 Mock
///     let delayed_cache = MockCache::with_delay(100);
///     delayed_cache.set("slow", "op".to_string()).await?;
///     Ok(())
/// }
/// ```
pub struct MockCache {
    /// 内部存储,使用 RwLock 保护并发访问
    storage: RwLock<HashMap<String, String>>,
    /// 模拟延迟(毫秒)
    delay_ms: u64,
}

impl MockCache {
    /// 创建新的 MockCache 实例
    ///
    /// 创建一个空的内存存储,无延迟。
    ///
    /// # 示例
    ///
    /// ```ignore
    /// let cache = MockCache::new();
    /// ```
    pub fn new() -> Self {
        Self {
            storage: RwLock::new(HashMap::new()),
            delay_ms: 0,
        }
    }

    /// 创建带延迟的 MockCache 实例
    ///
    /// 用于测试异步场景或模拟网络延迟。
    ///
    /// # 参数
    ///
    /// * `ms` - 延迟毫秒数
    ///
    /// # 示例
    ///
    /// ```ignore
    /// let cache = MockCache::with_delay(100); // 100ms 延迟
    /// ```
    pub fn with_delay(ms: u64) -> Self {
        Self {
            storage: RwLock::new(HashMap::new()),
            delay_ms: ms,
        }
    }
}

impl Default for MockCache {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Cache for MockCache {
    async fn get(&self, key: &str) -> Result<Option<String>, InklogError> {
        if self.delay_ms > 0 {
            tokio::time::sleep(tokio::time::Duration::from_millis(self.delay_ms)).await;
        }
        let storage = self.storage.read().unwrap();
        Ok(storage.get(key).cloned())
    }

    async fn set(&self, key: &str, value: String) -> Result<(), InklogError> {
        if self.delay_ms > 0 {
            tokio::time::sleep(tokio::time::Duration::from_millis(self.delay_ms)).await;
        }
        let mut storage = self.storage.write().unwrap();
        storage.insert(key.to_string(), value);
        Ok(())
    }

    async fn delete(&self, key: &str) -> Result<bool, InklogError> {
        if self.delay_ms > 0 {
            tokio::time::sleep(tokio::time::Duration::from_millis(self.delay_ms)).await;
        }
        let mut storage = self.storage.write().unwrap();
        Ok(storage.remove(key).is_some())
    }

    async fn exists(&self, key: &str) -> Result<bool, InklogError> {
        if self.delay_ms > 0 {
            tokio::time::sleep(tokio::time::Duration::from_millis(self.delay_ms)).await;
        }
        let storage = self.storage.read().unwrap();
        Ok(storage.contains_key(key))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ============================================================================
    // OxCacheAdapter 测试
    // ============================================================================

    #[tokio::test]
    async fn test_oxcache_adapter_basic_operations() {
        let cache = OxCacheAdapter::new().expect("Failed to create cache");

        // 测试 set 和 get
        cache
            .set("key1", "value1".to_string())
            .await
            .expect("Failed to set");
        let value = cache.get("key1").await.expect("Failed to get");
        assert_eq!(value, Some("value1".to_string()));

        // 测试 exists
        assert!(cache.exists("key1").await.expect("exists failed"));
        assert!(!cache.exists("nonexistent").await.expect("exists failed"));

        // 测试 delete
        assert!(cache.delete("key1").await.expect("delete failed"));
        assert!(!cache.exists("key1").await.expect("exists failed"));
        assert!(
            !cache.delete("key1").await.expect("delete failed"),
            "再次删除返回 false"
        );
    }

    #[tokio::test]
    async fn test_oxcache_adapter_get_nonexistent() {
        let cache = OxCacheAdapter::new().expect("Failed to create cache");

        let value = cache.get("nonexistent_key").await.expect("Failed to get");
        assert_eq!(value, None);
    }

    #[tokio::test]
    async fn test_oxcache_adapter_overwrite() {
        let cache = OxCacheAdapter::new().expect("Failed to create cache");

        cache
            .set("key", "value1".to_string())
            .await
            .expect("Failed to set");
        cache
            .set("key", "value2".to_string())
            .await
            .expect("Failed to set");

        let value = cache.get("key").await.expect("Failed to get");
        assert_eq!(value, Some("value2".to_string()));
    }

    #[tokio::test]
    async fn test_oxcache_adapter_builder_default() {
        // builder().build() 不带配置应等价于 new()
        let cache = OxCacheAdapter::builder()
            .build()
            .await
            .expect("Failed to build default cache");

        cache
            .set("bkey", "bval".to_string())
            .await
            .expect("Failed to set");
        assert_eq!(
            cache.get("bkey").await.expect("Failed to get"),
            Some("bval".to_string())
        );
    }

    #[tokio::test]
    async fn test_oxcache_adapter_builder_with_ttl_and_capacity() {
        // 验证 ttl 和容量配置被真正应用到后端
        let cache = OxCacheAdapter::builder()
            .ttl(std::time::Duration::from_secs(60))
            .capacity(100)
            .build()
            .await
            .expect("Failed to build configured cache");

        cache
            .set("ckey", "cval".to_string())
            .await
            .expect("Failed to set");
        assert_eq!(
            cache.get("ckey").await.expect("Failed to get"),
            Some("cval".to_string())
        );
        assert!(cache.exists("ckey").await.expect("exists failed"));
    }

    // ============================================================================
    // MockCache 测试
    // ============================================================================

    #[tokio::test]
    async fn test_mock_cache_basic_operations() {
        let cache = MockCache::new();

        // 测试 set 和 get
        cache
            .set("key1", "value1".to_string())
            .await
            .expect("Failed to set");
        let value = cache.get("key1").await.expect("Failed to get");
        assert_eq!(value, Some("value1".to_string()));

        // 测试 exists
        assert!(cache.exists("key1").await.expect("exists failed"));
        assert!(!cache.exists("nonexistent").await.expect("exists failed"));

        // 测试 delete
        assert!(cache.delete("key1").await.expect("delete failed"));
        assert!(!cache.exists("key1").await.expect("exists failed"));
        assert!(
            !cache.delete("key1").await.expect("delete failed"),
            "再次删除返回 false"
        );
    }

    #[tokio::test]
    async fn test_mock_cache_get_nonexistent() {
        let cache = MockCache::new();

        let value = cache.get("nonexistent_key").await.expect("Failed to get");
        assert_eq!(value, None);
    }

    #[tokio::test]
    async fn test_mock_cache_overwrite() {
        let cache = MockCache::new();

        cache
            .set("key", "value1".to_string())
            .await
            .expect("Failed to set");
        cache
            .set("key", "value2".to_string())
            .await
            .expect("Failed to set");

        let value = cache.get("key").await.expect("Failed to get");
        assert_eq!(value, Some("value2".to_string()));
    }

    #[tokio::test]
    async fn test_mock_cache_with_delay() {
        let cache = MockCache::with_delay(10);

        let start = std::time::Instant::now();
        cache.set("key", "value".to_string()).await.unwrap();
        let elapsed = start.elapsed();

        // 验证至少有 10ms 延迟
        assert!(elapsed.as_millis() >= 10);

        let start = std::time::Instant::now();
        let _ = cache.get("key").await.unwrap();
        let elapsed = start.elapsed();

        assert!(elapsed.as_millis() >= 10);
    }

    #[tokio::test]
    async fn test_mock_cache_delete_operation() {
        let cache = MockCache::new();

        // 删除不存在的键
        assert!(!cache.delete("nonexistent").await.expect("delete failed"));

        // 设置并删除
        cache.set("key", "value".to_string()).await.unwrap();
        assert!(cache.delete("key").await.expect("delete failed"));

        // 验证已删除
        assert_eq!(cache.get("key").await.expect("get failed"), None);
        assert!(!cache.exists("key").await.expect("exists failed"));
    }

    #[tokio::test]
    async fn test_mock_cache_exists_operation() {
        let cache = MockCache::new();

        // 不存在的键
        assert!(!cache.exists("key1").await.expect("exists failed"));

        // 设置后存在
        cache.set("key1", "value1".to_string()).await.unwrap();
        assert!(cache.exists("key1").await.expect("exists failed"));

        // 删除后不存在
        cache.delete("key1").await.unwrap();
        assert!(!cache.exists("key1").await.expect("exists failed"));
    }

    #[tokio::test]
    async fn test_mock_cache_thread_safety() {
        use std::sync::Arc;
        use tokio::task;

        let cache = Arc::new(MockCache::new());
        let mut handles = vec![];

        // 并发写入
        for i in 0..10 {
            let cache_clone = cache.clone();
            let handle = task::spawn(async move {
                cache_clone
                    .set(&format!("key{}", i), format!("value{}", i))
                    .await
                    .unwrap();
            });
            handles.push(handle);
        }

        // 等待所有写入完成
        for handle in handles {
            handle.await.unwrap();
        }

        // 验证所有写入
        for i in 0..10 {
            let value = cache.get(&format!("key{}", i)).await.expect("get failed");
            assert_eq!(value, Some(format!("value{}", i)));
        }
    }

    // ============================================================================
    // Default 实现测试 - 覆盖 MockCache::default(OxCacheAdapter::default 已移除)
    // ============================================================================

    #[tokio::test]
    async fn test_mock_cache_default_equals_new() {
        // 覆盖 MockCache::default() 实现
        let default_cache = MockCache::default();
        let new_cache = MockCache::new();
        // 两者行为应等价:初始为空
        assert_eq!(default_cache.get("any").await.expect("get failed"), None);
        assert_eq!(new_cache.get("any").await.expect("get failed"), None);
        // default 实例可正常写入读取
        default_cache
            .set("k", "v".to_string())
            .await
            .expect("default mock set should succeed");
        assert_eq!(
            default_cache.get("k").await.expect("get failed"),
            Some("v".to_string())
        );
    }

    // ============================================================================
    // MockCache with_delay 覆盖 delete/exists 延迟分支
    // ============================================================================

    #[tokio::test]
    async fn test_mock_cache_with_delay_delete_measures_delay() {
        // 覆盖 MockCache::delete 的 delay 分支
        let cache = MockCache::with_delay(15);
        // 先放入一条记录
        cache
            .set("delay_key", "v".to_string())
            .await
            .expect("set should succeed");
        // delete 应当至少消耗 delay_ms 的时间
        let start = std::time::Instant::now();
        let deleted = cache.delete("delay_key").await.expect("delete failed");
        let elapsed = start.elapsed();
        assert!(deleted, "existing key should be deleted");
        assert!(
            elapsed.as_millis() >= 15,
            "delete should respect delay_ms, got {}ms",
            elapsed.as_millis()
        );
    }

    #[tokio::test]
    async fn test_mock_cache_with_delay_exists_measures_delay() {
        // 覆盖 MockCache::exists 的 delay 分支
        let cache = MockCache::with_delay(15);
        cache
            .set("k", "v".to_string())
            .await
            .expect("set should succeed");
        // exists 应当至少消耗 delay_ms 的时间
        let start = std::time::Instant::now();
        let exists = cache.exists("k").await.expect("exists failed");
        let elapsed = start.elapsed();
        assert!(exists, "key should exist");
        assert!(
            elapsed.as_millis() >= 15,
            "exists should respect delay_ms, got {}ms",
            elapsed.as_millis()
        );
    }

    // ============================================================================
    // OxCacheAdapter TTL 过期与边界场景测试
    // ============================================================================

    #[tokio::test]
    async fn test_oxcache_adapter_ttl_expiration() {
        // 覆盖 TTL 过期路径:set 后等待 TTL 到期,get 应返回 None
        let cache = OxCacheAdapter::builder()
            .ttl(std::time::Duration::from_millis(100))
            .build()
            .await
            .expect("Failed to build cache with TTL");

        cache
            .set("expiring_key", "expiring_value".to_string())
            .await
            .expect("Failed to set");

        // 立即读取应能拿到值
        assert_eq!(
            cache.get("expiring_key").await.expect("get failed"),
            Some("expiring_value".to_string())
        );

        // 等待 TTL 过期
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        // 过期后应返回 None
        assert_eq!(
            cache.get("expiring_key").await.expect("get failed"),
            None,
            "expired key should return None"
        );
    }

    #[tokio::test]
    async fn test_oxcache_adapter_exists_after_ttl_expiration() {
        // 覆盖 exists 在 TTL 过期后返回 false 的路径
        let cache = OxCacheAdapter::builder()
            .ttl(std::time::Duration::from_millis(80))
            .build()
            .await
            .expect("Failed to build cache with TTL");

        cache
            .set("ttl_exists_key", "v".to_string())
            .await
            .expect("Failed to set");
        assert!(cache.exists("ttl_exists_key").await.expect("exists failed"));

        tokio::time::sleep(std::time::Duration::from_millis(150)).await;
        assert!(
            !cache.exists("ttl_exists_key").await.expect("exists failed"),
            "exists should return false after TTL expiration"
        );
    }

    #[tokio::test]
    async fn test_oxcache_adapter_delete_after_ttl_expiration() {
        // 覆盖 delete 在键已过期(exists 返回 false)时返回 false 的路径
        // 命中 OxCacheAdapter::delete 中 `if !exists { return false }` 分支
        let cache = OxCacheAdapter::builder()
            .ttl(std::time::Duration::from_millis(80))
            .build()
            .await
            .expect("Failed to build cache with TTL");

        cache
            .set("ttl_delete_key", "v".to_string())
            .await
            .expect("Failed to set");

        tokio::time::sleep(std::time::Duration::from_millis(150)).await;

        // 键已过期,exists 返回 false,delete 应短路返回 false
        let deleted = cache.delete("ttl_delete_key").await.expect("delete failed");
        assert!(
            !deleted,
            "delete should return false for expired key (exists short-circuit)"
        );
    }

    #[tokio::test]
    async fn test_oxcache_adapter_builder_ttl_only() {
        // 覆盖 builder 仅配置 TTL(不配置 capacity)的分支
        let cache = OxCacheAdapter::builder()
            .ttl(std::time::Duration::from_secs(60))
            .build()
            .await
            .expect("Failed to build cache with TTL only");

        cache
            .set("ttl_only_key", "ttl_only_value".to_string())
            .await
            .expect("Failed to set");
        assert_eq!(
            cache.get("ttl_only_key").await.expect("get failed"),
            Some("ttl_only_value".to_string())
        );
        assert!(cache.exists("ttl_only_key").await.expect("exists failed"));
    }

    #[tokio::test]
    async fn test_oxcache_adapter_builder_capacity_only() {
        // 覆盖 builder 仅配置 capacity(不配置 TTL)的分支
        let cache = OxCacheAdapter::builder()
            .capacity(50)
            .build()
            .await
            .expect("Failed to build cache with capacity only");

        cache
            .set("cap_only_key", "cap_only_value".to_string())
            .await
            .expect("Failed to set");
        assert_eq!(
            cache.get("cap_only_key").await.expect("get failed"),
            Some("cap_only_value".to_string())
        );
        assert!(cache.exists("cap_only_key").await.expect("exists failed"));
    }

    #[tokio::test]
    async fn test_oxcache_adapter_empty_key() {
        // 边界场景:空键
        let cache = OxCacheAdapter::new().expect("Failed to create cache");

        cache
            .set("", "empty_key_value".to_string())
            .await
            .expect("Failed to set with empty key");
        assert_eq!(
            cache.get("").await.expect("get failed"),
            Some("empty_key_value".to_string())
        );
        assert!(cache.exists("").await.expect("exists failed"));
        assert!(cache.delete("").await.expect("delete failed"));
        assert!(!cache.exists("").await.expect("exists failed"));
    }

    #[tokio::test]
    async fn test_oxcache_adapter_special_characters_in_key() {
        // 边界场景:键包含特殊字符
        let cache = OxCacheAdapter::new().expect("Failed to create cache");

        let keys = [
            "user:1",
            "namespace::key",
            "key with spaces",
            "key\twith\ttabs",
            "key\nwith\nnewlines",
            "中文键",
            "key\"with\"quotes",
            "key'with'apostrophes",
        ];

        for key in &keys {
            let value = format!("value_for_{}", key);
            cache
                .set(key, value.clone())
                .await
                .expect("Failed to set with special char key");
            assert_eq!(
                cache.get(key).await.expect("get failed"),
                Some(value),
                "get should return the set value for key: {:?}",
                key
            );
        }
    }

    #[tokio::test]
    async fn test_oxcache_adapter_large_value() {
        // 边界场景:大值
        let cache = OxCacheAdapter::new().expect("Failed to create cache");

        let large_value = "x".repeat(100_000);
        cache
            .set("large_key", large_value.clone())
            .await
            .expect("Failed to set large value");
        let retrieved = cache.get("large_key").await.expect("get failed");
        assert_eq!(retrieved.as_ref().map(|v| v.len()), Some(100_000));
        assert_eq!(retrieved, Some(large_value));
    }

    #[tokio::test]
    async fn test_oxcache_adapter_concurrent_access() {
        // 覆盖 OxCacheAdapter 的并发安全性(与 MockCache 的并发测试对称)
        use std::sync::Arc;
        use tokio::task;

        let cache = Arc::new(OxCacheAdapter::new().expect("Failed to create cache"));
        let mut handles = vec![];

        for i in 0..10 {
            let cache_clone = cache.clone();
            let handle = task::spawn(async move {
                cache_clone
                    .set(&format!("ckey{}", i), format!("cvalue{}", i))
                    .await
                    .expect("Failed to set");
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.await.expect("Task panicked");
        }

        for i in 0..10 {
            assert_eq!(
                cache.get(&format!("ckey{}", i)).await.expect("get failed"),
                Some(format!("cvalue{}", i)),
                "concurrent write for key {} should be visible",
                i
            );
        }
    }

    #[tokio::test]
    async fn test_oxcache_adapter_delete_nonexistent_after_set() {
        // 覆盖 delete 在 exists=false 时返回 false 的路径(非 TTL 场景)
        // 即 OxCacheAdapter::delete 中 `if !exists { return false }` 分支
        let cache = OxCacheAdapter::new().expect("Failed to create cache");

        // 从未设置过的键
        assert!(
            !cache.delete("never_set_key").await.expect("delete failed"),
            "delete on never-set key should return false"
        );

        // 设置后删除,再删除应返回 false
        cache
            .set("temp_key", "temp_value".to_string())
            .await
            .expect("Failed to set");
        assert!(cache.delete("temp_key").await.expect("delete failed"));
        assert!(
            !cache.delete("temp_key").await.expect("delete failed"),
            "delete on already-deleted key should return false"
        );
    }

    #[tokio::test]
    async fn test_oxcache_adapter_overwrite_does_not_reset_ttl() {
        // 覆盖 TTL 场景下的 overwrite:新值覆盖旧值后应可读
        // (不验证 TTL 是否重置,因为 oxcache 的 TTL 语义由后端决定)
        let cache = OxCacheAdapter::builder()
            .ttl(std::time::Duration::from_secs(60))
            .build()
            .await
            .expect("Failed to build cache");

        cache
            .set("ow_key", "v1".to_string())
            .await
            .expect("Failed to set v1");
        cache
            .set("ow_key", "v2".to_string())
            .await
            .expect("Failed to set v2");

        assert_eq!(
            cache.get("ow_key").await.expect("get failed"),
            Some("v2".to_string()),
            "overwrite should replace value"
        );
    }
}