sdforge 0.3.1

Multi-protocol SDK framework with unified macro configuration
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//!
//! 缓存模块 — 同步缓存接口与 oxcache 透传
//!
//! 本模块提供:
//! - `SyncCache` trait:同步键值存储接口(用于 security 模块等需要同步操作的场景)
//! - `OxcacheSyncCache`:基于 oxcache `DashMapMemoryBackend` 的同步内存缓存实现
//! - `DashMapCache`:`OxcacheSyncCache` 的类型别名(保持调用方兼容)
//! - oxcache 异步缓存的透传(用于需要 TTL、分层缓存等高级功能的场景)
//!
//! # 架构
//!
//! 根据依赖注入架构设计:
//! - oxcache 属于底层组件层(Infrastructure Layer)
//! - sdforge 属于功能组件层(Feature Layer)
//! - 功能组件通过依赖注入使用底层组件
//!
//! # 同步缓存使用示例
//!
//! ```rust,ignore
//! use sdforge::cache::{SyncCache, DashMapCache};
//! use std::sync::Arc;
//!
//! let cache = Arc::new(DashMapCache::new());
//! cache.set("key", b"value".to_vec());
//! assert!(cache.get("key").is_some());
//! ```
//!
//! # 异步缓存使用示例(透传 oxcache)
//!
//! ```rust,ignore
//! use sdforge::cache::Cache;
//!
//! let cache: Cache<String, MyData> = Cache::builder().build().await?;
//! cache.set(&"key".to_string(), &data).await?;
//! ```

use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::sync::Mutex;

// =============================================================================
// 键规范化函数
// =============================================================================

/// 规范化缓存键,确保一致的格式
///
/// # Arguments
/// * `key` - 原始键
///
/// # Returns
/// 规范化后的键字符串
///
/// # Examples
/// ```
/// use sdforge::cache::canonicalize_cache_key;
///
/// let normalized = canonicalize_cache_key("  user:123  ");
/// assert_eq!(normalized, "user:123");
/// ```
pub fn canonicalize_cache_key(key: &str) -> String {
    key.trim().to_lowercase()
}

// =============================================================================
// 通配符模式匹配(无需 regex 依赖,用于 find_keys_by_pattern)
// =============================================================================

/// 通配符模式匹配:`*` 匹配任意字符序列(含空),`?` 匹配单个字符。
///
/// 与将 `*`→`.*`、`?`→`.` 后用 `^pattern$` 正则匹配的行为等价,
/// 但无需编译 regex,且不引入 regex 依赖到 `cache` feature。
fn matches_pattern(pattern: &str, text: &str) -> bool {
    let p: Vec<char> = pattern.chars().collect();
    let t: Vec<char> = text.chars().collect();
    let mut pi = 0usize;
    let mut ti = 0usize;
    let mut star_pi: Option<usize> = None;
    let mut star_ti = 0usize;

    while ti < t.len() {
        if pi < p.len() && (p[pi] == t[ti] || p[pi] == '?') {
            pi += 1;
            ti += 1;
        } else if pi < p.len() && p[pi] == '*' {
            star_pi = Some(pi);
            star_ti = ti;
            pi += 1;
        } else if let Some(spi) = star_pi {
            pi = spi + 1;
            star_ti += 1;
            ti = star_ti;
        } else {
            return false;
        }
    }

    while pi < p.len() && p[pi] == '*' {
        pi += 1;
    }

    pi == p.len()
}

// =============================================================================
// 同步缓存 Trait(用于 security 模块等需要同步操作的场景)
// =============================================================================

/// 同步缓存 trait — 功能组件的标准存储接口
///
/// 设计原则:
/// - 所有方法同步,立即返回
/// - 值存储为 `Vec<u8>`,序列化由调用方负责
/// - 不管理 TTL,TTL 由调用方或上层组件处理
pub trait SyncCache: Send + Sync {
    /// 获取值
    ///
    /// # Returns
    /// `Some(bytes)` 如果存在,`None` 如果不存在
    fn get(&self, key: &str) -> Option<Vec<u8>>;

    /// 批量获取多个键的值
    ///
    /// # Performance
    /// 比单独调用 get() 更高效,减少锁竞争和系统调用
    ///
    /// # Arguments
    /// * `keys` - 要获取的键列表
    ///
    /// # Returns
    /// HashMap 包含所有找到的键值对
    fn get_many(&self, keys: &[&str]) -> HashMap<String, Vec<u8>> {
        keys.iter()
            .filter_map(|&key| self.get(key).map(|v| (key.to_string(), v)))
            .collect()
    }

    /// 设置值(无 TTL,由调用方管理生命周期)
    fn set(&self, key: &str, value: Vec<u8>);

    /// 批量设置多个键值对
    ///
    /// # Performance
    /// 比单独调用 set() 更高效,减少锁竞争和系统调用
    ///
    /// # Arguments
    /// * `items` - 键值对切片
    fn set_many(&self, items: &[(String, Vec<u8>)]) {
        for (key, value) in items {
            self.set(key, value.clone());
        }
    }

    /// 删除键
    ///
    /// # Returns
    /// `true` 如果键存在并被删除,`false` 如果不存在
    fn delete(&self, key: &str) -> bool;

    /// 批量删除多个键
    ///
    /// # Performance
    /// 比单独调用 delete() 更高效,减少锁竞争和系统调用
    ///
    /// # Arguments
    /// * `keys` - 要删除的键列表
    ///
    /// # Returns
    /// 被删除的键的数量
    fn delete_many(&self, keys: &[&str]) -> usize {
        keys.iter().filter(|&&key| self.delete(key)).count()
    }

    /// 检查键是否存在
    fn contains(&self, key: &str) -> bool;

    /// 清空所有键
    fn clear(&self);

    /// 获取键的数量
    fn len(&self) -> usize;

    /// 检查是否为空
    fn is_empty(&self) -> bool;

    /// 根据模式删除匹配的键
    ///
    /// # Arguments
    /// * `pattern` - 匹配模式(支持通配符 * 和前缀匹配)
    ///
    /// # Returns
    /// 被删除的键的数量
    ///
    /// # Examples
    /// ```ignore
    /// cache.invalidate("user:*"); // 删除所有 user: 开头的键
    /// cache.invalidate("*session*"); // 删除包含 session 的键
    /// ```
    fn invalidate(&self, pattern: &str) -> usize {
        let keys = self.find_keys_by_pattern(pattern);
        self.delete_many(&keys.iter().map(|s| s.as_str()).collect::<Vec<_>>())
    }

    /// 根据模式查找匹配的键(不删除)
    ///
    /// # Arguments
    /// * `pattern` - 匹配模式
    ///
    /// # Returns
    /// 匹配的键列表
    fn find_keys_by_pattern(&self, pattern: &str) -> Vec<String>;

    /// 获取缓存统计信息
    ///
    /// # Returns
    /// 包含命中数、未命中数、命中率等的 HashMap
    fn get_stats(&self) -> HashMap<String, u64> {
        HashMap::new()
    }
}

/// SyncCache 的 Arc 智能指针别名
pub type SharedCache = Arc<dyn SyncCache>;

// =============================================================================
// OxcacheSyncCache — 基于 oxcache DashMapMemoryBackend 的同步缓存实现
// =============================================================================

/// 基于 oxcache `DashMapMemoryBackend` 的同步缓存实现。
///
/// 包装 oxcache 0.3.2 的 `DashMapMemoryBackend`(实现了 `SyncCacheBackend`),
/// 通过 `SyncCacheReader`/`SyncCacheWriter` trait 方法提供同步 get/set/delete 操作。
///
/// 由于 oxcache 0.3.2 的公开 sync API 不暴露键枚举,`find_keys_by_pattern`
/// 通过维护一个并行的键索引(`Mutex<HashSet<String>>`)实现。
///
/// # Example
///
/// ```rust,ignore
/// use sdforge::cache::{SyncCache, OxcacheSyncCache};
///
/// let cache = OxcacheSyncCache::new();
/// cache.set("key", b"value".to_vec());
/// assert_eq!(cache.get("key"), Some(b"value".to_vec()));
/// ```
pub struct OxcacheSyncCache {
    /// oxcache 后端(实现了 SyncCacheBackend)
    backend: oxcache::backend::DashMapMemoryBackend,
    /// 键索引:oxcache 0.3.2 sync API 不暴露键枚举,需并行维护以支持 find_keys_by_pattern
    key_index: Mutex<HashSet<String>>,
}

impl OxcacheSyncCache {
    /// 创建默认容量的同步缓存
    pub fn new() -> Self {
        Self {
            backend: oxcache::backend::DashMapMemoryBackend::new(),
            key_index: Mutex::new(HashSet::new()),
        }
    }

    /// 创建指定容量的同步缓存
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            backend: oxcache::backend::DashMapMemoryBackend::builder()
                .capacity(capacity)
                .build(),
            key_index: Mutex::new(HashSet::with_capacity(capacity)),
        }
    }

    /// 获取内部 backend 引用(供高级用途)
    pub fn inner(&self) -> &oxcache::backend::DashMapMemoryBackend {
        &self.backend
    }
}

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

impl std::fmt::Debug for OxcacheSyncCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let len = self.key_index.lock().map(|idx| idx.len()).unwrap_or(0);
        f.debug_struct("OxcacheSyncCache")
            .field("backend", &"DashMapMemoryBackend")
            .field("key_count", &len)
            .finish()
    }
}

impl SyncCache for OxcacheSyncCache {
    fn get(&self, key: &str) -> Option<Vec<u8>> {
        use oxcache::backend::SyncCacheReader;
        self.backend.get(key).ok().flatten()
    }

    fn set(&self, key: &str, value: Vec<u8>) {
        use oxcache::backend::SyncCacheWriter;
        // 持有 index 锁直到 backend 操作完成,保证 backend 与 index 一致性
        // (HIGH-001: 避免并发下 backend 有键但 index 缺失的竞态)
        let mut idx = match self.key_index.lock() {
            Ok(idx) => idx,
            Err(_) => {
                log::warn!(
                    "cache key_index poisoned; set falling back to backend-only for key={:?}",
                    key
                );
                if let Err(e) = self.backend.set(key, value, None) {
                    log::warn!("cache backend set failed for key={:?}: {}", key, e);
                }
                return;
            }
        };
        // HIGH-002: 不静默吞掉 backend 错误;失败时不更新 index 以保持一致
        if let Err(e) = self.backend.set(key, value, None) {
            log::warn!("cache backend set failed for key={:?}: {}", key, e);
            return;
        }
        idx.insert(key.to_string());
    }

    fn delete(&self, key: &str) -> bool {
        use oxcache::backend::{SyncCacheReader, SyncCacheWriter};
        let mut idx = match self.key_index.lock() {
            Ok(idx) => idx,
            Err(_) => {
                log::warn!(
                    "cache key_index poisoned; delete falling back to backend-only for key={:?}",
                    key
                );
                let existed = self.backend.exists(key).unwrap_or(false);
                if existed {
                    if let Err(e) = self.backend.delete(key) {
                        log::warn!("cache backend delete failed for key={:?}: {}", key, e);
                    }
                }
                return existed;
            }
        };
        let existed = self.backend.exists(key).unwrap_or(false);
        if existed {
            // HIGH-002: backend 失败时不更新 index,保持一致
            if let Err(e) = self.backend.delete(key) {
                log::warn!("cache backend delete failed for key={:?}: {}", key, e);
                return existed;
            }
            idx.remove(key);
        }
        existed
    }

    fn contains(&self, key: &str) -> bool {
        use oxcache::backend::SyncCacheReader;
        self.backend.exists(key).unwrap_or(false)
    }

    fn clear(&self) {
        use oxcache::backend::SyncCacheWriter;
        let mut idx = match self.key_index.lock() {
            Ok(idx) => idx,
            Err(_) => {
                log::warn!("cache key_index poisoned; clear falling back to backend-only");
                if let Err(e) = self.backend.clear() {
                    log::warn!("cache backend clear failed: {}", e);
                }
                return;
            }
        };
        if let Err(e) = self.backend.clear() {
            log::warn!("cache backend clear failed: {}", e);
            return;
        }
        idx.clear();
    }

    fn len(&self) -> usize {
        use oxcache::backend::SyncCacheReader;
        self.backend.len().unwrap_or(0) as usize
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn find_keys_by_pattern(&self, pattern: &str) -> Vec<String> {
        use oxcache::backend::SyncCacheReader;
        let mut idx = match self.key_index.lock() {
            Ok(idx) => idx,
            Err(_) => return Vec::new(),
        };
        // BUG-4 修复: oxcache backend 达到容量时会内部驱逐键,但 `key_index` 不会同步感知,
        // 导致 index 逐渐成为 backend 的超集,`find_keys_by_pattern` 返回已不存在的键。
        //
        // 修复策略:遍历 index 时通过 `backend.exists()` 过滤,并惰性清理已被驱逐的键,
        // 既保证返回结果与 backend 一致,又避免 index 无限增长(内存泄漏)。
        let mut result = Vec::new();
        let mut stale_keys = Vec::new();
        for k in idx.iter() {
            if !matches_pattern(pattern, k) {
                continue;
            }
            if self.backend.exists(k).unwrap_or(false) {
                result.push(k.clone());
            } else {
                // backend 已驱逐此键,标记为 stale 以便从 index 移除
                stale_keys.push(k.clone());
            }
        }
        // 惰性清理:移除已被 backend 驱逐的键,防止 index 内存泄漏
        for k in stale_keys {
            idx.remove(&k);
        }
        result
    }

    fn get_many(&self, keys: &[&str]) -> HashMap<String, Vec<u8>> {
        use oxcache::backend::SyncCacheReader;
        let mut results = HashMap::with_capacity(keys.len());
        for &key in keys {
            if let Ok(Some(value)) = self.backend.get(key) {
                results.insert(key.to_string(), value);
            }
        }
        results
    }

    fn set_many(&self, items: &[(String, Vec<u8>)]) {
        use oxcache::backend::SyncCacheWriter;
        let mut idx = match self.key_index.lock() {
            Ok(idx) => idx,
            Err(_) => {
                log::warn!(
                    "cache key_index poisoned; set_many falling back to backend-only for {} items",
                    items.len()
                );
                for (key, value) in items {
                    if let Err(e) = self.backend.set(key, value.clone(), None) {
                        log::warn!("cache backend set failed for key={:?}: {}", key, e);
                    }
                }
                return;
            }
        };
        // 先执行所有 backend 写入,成功后才更新 index,避免部分失败导致 index 与 backend 不一致
        let mut succeeded: Vec<&String> = Vec::with_capacity(items.len());
        for (key, value) in items {
            if let Err(e) = self.backend.set(key, value.clone(), None) {
                log::warn!("cache backend set failed for key={:?}: {}", key, e);
            } else {
                succeeded.push(key);
            }
        }
        for key in succeeded {
            idx.insert(key.clone());
        }
    }

    fn delete_many(&self, keys: &[&str]) -> usize {
        use oxcache::backend::{SyncCacheReader, SyncCacheWriter};
        let mut idx = match self.key_index.lock() {
            Ok(idx) => idx,
            Err(_) => {
                log::warn!(
                    "cache key_index poisoned; delete_many falling back to backend-only for {} keys",
                    keys.len()
                );
                let mut deleted = 0usize;
                for &key in keys {
                    let existed = self.backend.exists(key).unwrap_or(false);
                    if existed {
                        if let Err(e) = self.backend.delete(key) {
                            log::warn!("cache backend delete failed for key={:?}: {}", key, e);
                        } else {
                            deleted += 1;
                        }
                    }
                }
                return deleted;
            }
        };
        let mut deleted = 0usize;
        for &key in keys {
            let existed = self.backend.exists(key).unwrap_or(false);
            if existed {
                if let Err(e) = self.backend.delete(key) {
                    log::warn!("cache backend delete failed for key={:?}: {}", key, e);
                } else {
                    idx.remove(key);
                    deleted += 1;
                }
            }
        }
        deleted
    }

    fn get_stats(&self) -> HashMap<String, u64> {
        use oxcache::backend::SyncCacheReader;
        let mut stats = HashMap::new();
        let len = self.backend.len().unwrap_or(0);
        stats.insert("total_keys".to_string(), len);
        stats.insert(
            "capacity".to_string(),
            SyncCacheReader::capacity(&self.backend).unwrap_or(0),
        );
        // 透传 backend stats(命中数、未命中数、命中率等)
        //
        // BUG-5 修复: 原代码仅尝试 `v.parse::<u64>()`,对 float 类型统计
        // (如 hit_rate="0.85")静默丢弃,违反 Rule 12(失败必须显性化)。
        //
        // 修复策略:
        // 1. 先尝试 u64 解析(适用于 hits、misses 等整数统计)
        // 2. 失败则尝试 f64 解析:
        //    - 若键名含 "rate"/"ratio"/"pct",视为 0.0-1.0 的比率,×100 后四舍五入为百分比 u64
        //    - 否则直接四舍五入为 u64
        // 3. 两者均失败则 log::warn! 显性化(不再静默丢弃)
        if let Ok(backend_stats) = self.backend.stats() {
            for (k, v) in backend_stats {
                if let Ok(n) = v.parse::<u64>() {
                    stats.entry(k).or_insert(n);
                } else if let Ok(f) = v.parse::<f64>() {
                    let lower = k.to_lowercase();
                    let converted = if lower.contains("rate")
                        || lower.contains("ratio")
                        || lower.contains("pct")
                    {
                        (f * 100.0).round() as u64
                    } else {
                        f.round() as u64
                    };
                    log::debug!(
                        "cache stat {:?} parsed as f64={} converted to u64={}",
                        k,
                        f,
                        converted
                    );
                    stats.entry(k).or_insert(converted);
                } else {
                    log::warn!(
                        "cache stat {:?} value {:?} could not be parsed as u64 or f64; dropped",
                        k,
                        v
                    );
                }
            }
        }
        stats
    }
}

/// DashMapCache 类型别名 — 保持调用方兼容
///
/// 底层实现已切换为 `OxcacheSyncCache`(基于 oxcache `DashMapMemoryBackend`)。
/// 历史代码中的 `DashMapCache::new()` / `DashMapCache::with_capacity(n)` 调用
/// 自动指向新的 oxcache 实现,无需修改调用方。
pub type DashMapCache = OxcacheSyncCache;

// =============================================================================
// oxcache 异步缓存透传
// =============================================================================

// 直接透传 oxcache 库的异步缓存接口(用于需要 TTL、分层缓存等高级功能的场景)
pub use oxcache::cache::Cache;
pub use oxcache::traits::CacheKey;

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

    #[test]
    fn test_canonicalize_cache_key_trims_whitespace() {
        assert_eq!(canonicalize_cache_key("  user:123  "), "user:123");
        assert_eq!(canonicalize_cache_key("key"), "key");
        assert_eq!(canonicalize_cache_key("\tkey\n"), "key");
    }

    #[test]
    fn test_canonicalize_cache_key_lowercase() {
        assert_eq!(canonicalize_cache_key("USER:123"), "user:123");
        assert_eq!(canonicalize_cache_key("MixedCase"), "mixedcase");
        assert_eq!(canonicalize_cache_key("USER:ABC"), "user:abc");
    }

    #[test]
    fn test_canonicalize_cache_key_combined() {
        assert_eq!(canonicalize_cache_key("  USER:123  "), "user:123");
        assert_eq!(canonicalize_cache_key("\tMIXED_CASE\n"), "mixed_case");
    }

    // ========================================================================
    // matches_pattern 单元测试
    // ========================================================================

    #[test]
    fn test_matches_pattern_exact() {
        assert!(matches_pattern("user:1", "user:1"));
        assert!(!matches_pattern("user:1", "user:2"));
    }

    #[test]
    fn test_matches_pattern_prefix_wildcard() {
        assert!(matches_pattern("user:*", "user:1"));
        assert!(matches_pattern("user:*", "user:2"));
        assert!(matches_pattern("user:*", "user:"));
        assert!(!matches_pattern("user:*", "session:1"));
    }

    #[test]
    fn test_matches_pattern_suffix_wildcard() {
        assert!(matches_pattern("*:1", "user:1"));
        assert!(matches_pattern("*:1", "admin:1"));
        assert!(!matches_pattern("*:1", "user:2"));
    }

    #[test]
    fn test_matches_pattern_middle_wildcard() {
        assert!(matches_pattern("*session*", "my_session_data"));
        assert!(matches_pattern("*session*", "session"));
        assert!(!matches_pattern("*session*", "user:1"));
    }

    #[test]
    fn test_matches_pattern_single_char_wildcard() {
        assert!(matches_pattern("user:?", "user:1"));
        assert!(matches_pattern("user:?", "user:a"));
        assert!(!matches_pattern("user:?", "user:12"));
    }

    #[test]
    fn test_matches_pattern_combined_wildcards() {
        assert!(matches_pattern("?ser:*", "user:1"));
        assert!(matches_pattern("?ser:*", "aser:xyz"));
        assert!(!matches_pattern("?ser:*", "xser1:"));
    }

    #[test]
    fn test_matches_pattern_empty_pattern_and_text() {
        assert!(matches_pattern("", ""));
        assert!(matches_pattern("*", ""));
        assert!(!matches_pattern("", "a"));
        assert!(matches_pattern("*", "anything"));
    }

    // ========================================================================
    // SyncCache trait 测试(通过 DashMapCache 别名指向 OxcacheSyncCache)
    // ========================================================================

    #[test]
    fn test_synccache_trait_get_set() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("test_key", b"test_value".to_vec());
        assert_eq!(cache.get("test_key"), Some(b"test_value".to_vec()));
        assert!(cache.contains("test_key"));
    }

    #[test]
    fn test_synccache_trait_delete() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("key1", b"value1".to_vec());
        assert!(cache.delete("key1"));
        assert!(!cache.contains("key1"));
        assert_eq!(cache.get("key1"), None);
    }

    #[test]
    fn test_synccache_trait_clear() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("key1", b"v1".to_vec());
        cache.set("key2", b"v2".to_vec());
        cache.clear();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn test_synccache_trait_len_and_is_empty() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);

        cache.set("key1", b"v1".to_vec());
        cache.set("key2", b"v2".to_vec());
        assert!(!cache.is_empty());
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_synccache_trait_get_many() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("key1", b"v1".to_vec());
        cache.set("key2", b"v2".to_vec());
        cache.set("key3", b"v3".to_vec());

        let results = cache.get_many(&["key1", "key2", "nonexistent"]);
        assert_eq!(results.len(), 2);
        assert_eq!(results.get("key1"), Some(&b"v1".to_vec()));
    }

    #[test]
    fn test_synccache_trait_set_many() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        let items = vec![
            ("k1".to_string(), b"v1".to_vec()),
            ("k2".to_string(), b"v2".to_vec()),
        ];
        cache.set_many(&items);
        assert_eq!(cache.len(), 2);
        assert_eq!(cache.get("k1"), Some(b"v1".to_vec()));
        assert_eq!(cache.get("k2"), Some(b"v2".to_vec()));
    }

    #[test]
    fn test_synccache_trait_delete_many() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("k1", b"v1".to_vec());
        cache.set("k2", b"v2".to_vec());
        cache.set("k3", b"v3".to_vec());

        let deleted = cache.delete_many(&["k1", "k3", "nonexistent"]);
        assert_eq!(deleted, 2);
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_synccache_trait_invalidate() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("user:1", b"v1".to_vec());
        cache.set("user:2", b"v2".to_vec());
        cache.set("session:1", b"s1".to_vec());

        let deleted = cache.invalidate("user:*");
        assert_eq!(deleted, 2);
        assert_eq!(cache.len(), 1);
        assert!(cache.contains("session:1"));
    }

    #[test]
    fn test_synccache_trait_find_keys_by_pattern() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("user:1", b"v1".to_vec());
        cache.set("user:2", b"v2".to_vec());
        cache.set("admin:1", b"a1".to_vec());

        let keys = cache.find_keys_by_pattern("user:*");
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"user:1".to_string()));
        assert!(keys.contains(&"user:2".to_string()));
    }

    #[test]
    fn test_synccache_trait_find_keys_by_pattern_suffix() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("user:1", b"v1".to_vec());
        cache.set("admin:1", b"a1".to_vec());
        cache.set("user:2", b"v2".to_vec());

        let keys = cache.find_keys_by_pattern("*:1");
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"user:1".to_string()));
        assert!(keys.contains(&"admin:1".to_string()));
    }

    #[test]
    fn test_synccache_trait_find_keys_by_pattern_no_match() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("user:1", b"v1".to_vec());

        let keys = cache.find_keys_by_pattern("session:*");
        assert!(keys.is_empty());
    }

    #[test]
    fn test_synccache_trait_get_stats() {
        let cache: Box<dyn SyncCache> = Box::new(DashMapCache::new());
        cache.set("k1", b"v1".to_vec());
        cache.set("k2", b"v2".to_vec());

        let stats = cache.get_stats();
        assert!(stats.contains_key("total_keys"));
        assert_eq!(stats.get("total_keys"), Some(&2));
        assert!(stats.contains_key("capacity"));
    }

    #[test]
    fn test_shared_cache_type_alias() {
        let cache: SharedCache = Arc::new(DashMapCache::new());
        cache.set("key", b"value".to_vec());
        assert!(cache.contains("key"));
    }

    // ============================================================================
    // OxcacheSyncCache 专属测试
    // ============================================================================

    #[test]
    fn test_oxcache_sync_cache_with_capacity() {
        let cache = OxcacheSyncCache::with_capacity(100);
        cache.set("k1", b"v1".to_vec());
        assert_eq!(cache.get("k1"), Some(b"v1".to_vec()));
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_oxcache_sync_cache_default() {
        let cache = OxcacheSyncCache::default();
        assert!(cache.is_empty());
    }

    #[test]
    fn test_oxcache_sync_cache_inner() {
        let cache = OxcacheSyncCache::new();
        let _backend: &oxcache::backend::DashMapMemoryBackend = cache.inner();
    }

    #[test]
    fn test_oxcache_sync_cache_debug() {
        let cache = OxcacheSyncCache::new();
        let debug_str = format!("{:?}", cache);
        assert!(debug_str.contains("OxcacheSyncCache"));
    }

    #[test]
    fn test_oxcache_sync_cache_delete_nonexistent_returns_false() {
        let cache = OxcacheSyncCache::new();
        assert!(!cache.delete("nonexistent"));
    }

    // ============================================================================
    // Default trait method coverage tests
    //
    // OxcacheSyncCache overrides get_many/set_many/delete_many/get_stats, so the
    // default trait method implementations in SyncCache are not exercised by
    // the tests above. The MinimalCache below deliberately only implements
    // the required methods, leaving the default implementations in place so
    // they get coverage.
    // ============================================================================

    /// Minimal SyncCache implementation that relies on the default trait
    /// method implementations for batch operations and stats.
    struct MinimalCache {
        data: std::sync::Mutex<HashMap<String, Vec<u8>>>,
    }

    impl MinimalCache {
        fn new() -> Self {
            Self {
                data: std::sync::Mutex::new(HashMap::new()),
            }
        }
    }

    impl SyncCache for MinimalCache {
        fn get(&self, key: &str) -> Option<Vec<u8>> {
            self.data.lock().unwrap().get(key).cloned()
        }

        fn set(&self, key: &str, value: Vec<u8>) {
            self.data.lock().unwrap().insert(key.to_string(), value);
        }

        fn delete(&self, key: &str) -> bool {
            self.data.lock().unwrap().remove(key).is_some()
        }

        fn contains(&self, key: &str) -> bool {
            self.data.lock().unwrap().contains_key(key)
        }

        fn clear(&self) {
            self.data.lock().unwrap().clear();
        }

        fn len(&self) -> usize {
            self.data.lock().unwrap().len()
        }

        fn is_empty(&self) -> bool {
            self.len() == 0
        }

        fn find_keys_by_pattern(&self, pattern: &str) -> Vec<String> {
            self.data
                .lock()
                .unwrap()
                .keys()
                .filter(|k| matches_pattern(pattern, k))
                .cloned()
                .collect()
        }
    }

    #[test]
    fn test_default_get_many_implementation() {
        let cache = MinimalCache::new();
        cache.set("key1", b"v1".to_vec());
        cache.set("key2", b"v2".to_vec());

        let results = cache.get_many(&["key1", "key2", "missing"]);
        assert_eq!(results.len(), 2);
        assert_eq!(results.get("key1"), Some(&b"v1".to_vec()));
        assert_eq!(results.get("key2"), Some(&b"v2".to_vec()));
        assert!(!results.contains_key("missing"));
    }

    #[test]
    fn test_default_get_many_empty() {
        let cache = MinimalCache::new();
        let results = cache.get_many(&[]);
        assert!(results.is_empty());
    }

    #[test]
    fn test_default_set_many_implementation() {
        let cache = MinimalCache::new();
        let items = vec![
            ("k1".to_string(), b"v1".to_vec()),
            ("k2".to_string(), b"v2".to_vec()),
            ("k3".to_string(), b"v3".to_vec()),
        ];
        cache.set_many(&items);
        assert_eq!(cache.len(), 3);
        assert_eq!(cache.get("k1"), Some(b"v1".to_vec()));
        assert_eq!(cache.get("k2"), Some(b"v2".to_vec()));
        assert_eq!(cache.get("k3"), Some(b"v3".to_vec()));
    }

    #[test]
    fn test_default_set_many_empty() {
        let cache = MinimalCache::new();
        cache.set_many(&[]);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_default_delete_many_implementation() {
        let cache = MinimalCache::new();
        cache.set("k1", b"v1".to_vec());
        cache.set("k2", b"v2".to_vec());
        cache.set("k3", b"v3".to_vec());

        let deleted = cache.delete_many(&["k1", "k3", "missing"]);
        assert_eq!(deleted, 2);
        assert_eq!(cache.len(), 1);
        assert!(cache.contains("k2"));
    }

    #[test]
    fn test_default_delete_many_empty() {
        let cache = MinimalCache::new();
        let deleted = cache.delete_many(&[]);
        assert_eq!(deleted, 0);
    }

    #[test]
    fn test_default_get_stats_implementation() {
        let cache = MinimalCache::new();
        cache.set("k1", b"v1".to_vec());
        cache.set("k2", b"v2".to_vec());

        // Default get_stats returns an empty HashMap
        let stats = cache.get_stats();
        assert!(stats.is_empty());
    }

    #[test]
    fn test_default_invalidate_implementation() {
        let cache = MinimalCache::new();
        cache.set("user:1", b"v1".to_vec());
        cache.set("user:2", b"v2".to_vec());
        cache.set("session:1", b"s1".to_vec());

        // Default invalidate uses find_keys_by_pattern + delete_many
        let deleted = cache.invalidate("user:*");
        assert_eq!(deleted, 2);
        assert_eq!(cache.len(), 1);
        assert!(cache.contains("session:1"));
    }

    // ========================================================================
    // Mutex poison 分支测试
    //
    // OxcacheSyncCache 在 set/delete/clear/find_keys_by_pattern/set_many/
    // delete_many 中先获取 `key_index` 锁。若锁中毒(持有锁时 panic),
    // 各方法会降级为 backend-only 模式或返回安全默认值。这些测试通过
    // 故意 panic 让 Mutex 中毒,验证降级路径。
    // ========================================================================

    /// Helper: 让 cache 的 key_index Mutex 中毒。
    ///
    /// 持有锁时 panic 会导致 Mutex 中毒,后续 lock() 调用返回 Err。
    fn poison_key_index(cache: &OxcacheSyncCache) {
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _guard = cache.key_index.lock().unwrap();
            panic!("intentional panic to poison key_index mutex");
        }));
        assert!(result.is_err(), "poisoning panic should be caught");
    }

    #[test]
    fn test_set_with_poisoned_mutex_falls_back_to_backend() {
        let cache = OxcacheSyncCache::new();
        poison_key_index(&cache);

        // Mutex 中毒后,set 应降级为 backend-only 写入(不更新 index)
        cache.set("poisoned_key", b"poisoned_value".to_vec());

        // backend 仍然能读到值(通过 contains/get 验证)
        assert!(cache.contains("poisoned_key"));
        assert_eq!(cache.get("poisoned_key"), Some(b"poisoned_value".to_vec()));
    }

    #[test]
    fn test_delete_with_poisoned_mutex_falls_back_to_backend() {
        let cache = OxcacheSyncCache::new();
        // 先写入一个值(此时 Mutex 未中毒)
        cache.set("to_delete", b"value".to_vec());
        assert!(cache.contains("to_delete"));

        // 中毒 Mutex
        poison_key_index(&cache);

        // delete 应降级为 backend-only 删除
        let deleted = cache.delete("to_delete");
        assert!(deleted, "delete should report the key existed");
        assert!(!cache.contains("to_delete"));
    }

    #[test]
    fn test_delete_with_poisoned_mutex_nonexistent_key() {
        let cache = OxcacheSyncCache::new();
        poison_key_index(&cache);

        // 删除不存在的键,backend.exists 返回 false,应返回 false
        let deleted = cache.delete("nonexistent_key");
        assert!(!deleted);
    }

    #[test]
    fn test_clear_with_poisoned_mutex_falls_back_to_backend() {
        let cache = OxcacheSyncCache::new();
        cache.set("k1", b"v1".to_vec());
        cache.set("k2", b"v2".to_vec());
        assert_eq!(cache.len(), 2);

        poison_key_index(&cache);

        // clear 应降级为 backend-only 清空
        cache.clear();
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn test_find_keys_by_pattern_with_poisoned_mutex_returns_empty() {
        let cache = OxcacheSyncCache::new();
        cache.set("user:1", b"v1".to_vec());
        cache.set("user:2", b"v2".to_vec());

        poison_key_index(&cache);

        // Mutex 中毒时,find_keys_by_pattern 直接返回空 Vec
        let keys = cache.find_keys_by_pattern("user:*");
        assert!(keys.is_empty(), "poisoned mutex should yield empty result");
    }

    #[test]
    fn test_set_many_with_poisoned_mutex_falls_back_to_backend() {
        let cache = OxcacheSyncCache::new();
        poison_key_index(&cache);

        let items = vec![
            ("batch_k1".to_string(), b"v1".to_vec()),
            ("batch_k2".to_string(), b"v2".to_vec()),
        ];
        cache.set_many(&items);

        // backend 应能读到写入的值
        assert!(cache.contains("batch_k1"));
        assert!(cache.contains("batch_k2"));
        assert_eq!(cache.get("batch_k2"), Some(b"v2".to_vec()));
    }

    #[test]
    fn test_delete_many_with_poisoned_mutex_falls_back_to_backend() {
        let cache = OxcacheSyncCache::new();
        // 先写入值(Mutex 未中毒时)
        cache.set("del_k1", b"v1".to_vec());
        cache.set("del_k2", b"v2".to_vec());
        cache.set("del_k3", b"v3".to_vec());

        poison_key_index(&cache);

        // delete_many 应降级为 backend-only 删除
        let deleted = cache.delete_many(&["del_k1", "del_k3", "nonexistent"]);
        assert_eq!(deleted, 2, "should report 2 keys deleted");
        assert!(!cache.contains("del_k1"));
        assert!(cache.contains("del_k2"));
        assert!(!cache.contains("del_k3"));
    }

    #[test]
    fn test_delete_many_with_poisoned_mutex_all_nonexistent() {
        let cache = OxcacheSyncCache::new();
        poison_key_index(&cache);

        // 所有键都不存在时,deleted 应为 0
        let deleted = cache.delete_many(&["no_k1", "no_k2"]);
        assert_eq!(deleted, 0);
    }

    /// Helper: poison the key_index mutex while a key is already in the index.
    /// Used to verify that find_keys_by_pattern's stale-key cleanup path
    /// (lines 408-416) is not the only path exercised.
    #[test]
    fn test_get_with_poisoned_mutex_still_works() {
        // get() 不使用 key_index 锁,只读 backend,所以不受中毒影响
        let cache = OxcacheSyncCache::new();
        cache.set("k", b"v".to_vec());
        poison_key_index(&cache);

        // get 应仍然正常工作(不获取 key_index 锁)
        assert_eq!(cache.get("k"), Some(b"v".to_vec()));
        assert!(cache.contains("k"));
    }

    /// Verify that find_keys_by_pattern removes stale keys from the index
    /// when the backend has evicted them (lines 409-416).
    #[test]
    fn test_find_keys_by_pattern_removes_stale_keys() {
        let cache = OxcacheSyncCache::new();

        // Manually insert keys into the index without setting them in the backend
        {
            let mut idx = cache.key_index.lock().unwrap();
            idx.insert("stale_key_1".to_string());
            idx.insert("stale_key_2".to_string());
        }

        // Call find_keys_by_pattern with a glob pattern that matches
        let result = cache.find_keys_by_pattern("stale_*");

        // The stale keys should not be in the results (backend doesn't have them)
        assert!(
            result.is_empty(),
            "Stale keys should not appear in results"
        );

        // The stale keys should have been removed from the index
        let idx = cache.key_index.lock().unwrap();
        assert!(
            !idx.contains("stale_key_1"),
            "Stale key 1 should be removed from index"
        );
        assert!(
            !idx.contains("stale_key_2"),
            "Stale key 2 should be removed from index"
        );
    }
}