oxcache 0.2.0

A high-performance multi-level cache library for Rust with L1 (memory) and L2 (Redis) caching.
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
//! Copyright (c) 2025-2026, Kirky.X
//!
//! MIT License
//!
//! 该模块定义了缓存系统的错误类型和处理机制。

use thiserror::Error;

#[cfg(feature = "redis")]
/// Configuration error type for cache initialization
///
/// This error type is used during the configuration phase (factory functions, builders)
/// and is separate from runtime errors. It represents errors that occur when setting up
/// a cache instance, such as invalid configuration values or missing required fields.
///
/// # Example
///
/// ```rust,ignore
/// use oxcache::error::CacheConfigError;
///
/// fn validate_config(config: &CacheConfig) -> ConfigResult<()> {
///     if config.l1.capacity == 0 {
///         return Err(CacheConfigError::InvalidValue {
///             field: "capacity".to_string(),
///             reason: "capacity must be greater than 0".to_string(),
///         });
///     }
///     Ok(())
/// }
/// ```
#[derive(Debug, Error)]
pub enum CacheConfigError {
    /// Missing required configuration field
    #[error("Missing required field: {0}")]
    MissingField(String),

    /// Invalid value for a configuration field
    #[error("Invalid value for field '{field}': {reason}")]
    InvalidValue { field: String, reason: String },

    /// Unsupported backend combination
    #[error("Unsupported backend combination: {0}")]
    UnsupportedBackend(String),

    /// Connection failed during initialization
    #[error("Connection failed during initialization: {0}")]
    ConnectionFailed(String),
}

/// Result type for configuration operations
pub type ConfigResult<T> = std::result::Result<T, CacheConfigError>;

/// 缓存系统错误类型枚举
///
/// 定义了缓存系统中可能发生的各种错误类型。
/// 所有错误都实现了std::error::Error trait,可以使用?操作符传播。
///
/// # 错误分类
///
/// - **序列化错误** ([`CacheError::Serialization`]): 数据序列化/反序列化失败
/// - **后端错误** ([`CacheError::BackendError`]): L1/L2缓存后端操作失败
/// - **连接错误** ([`CacheError::ConnectionError`]): �络连接问题
/// - **超时错误** ([`CacheError::TimeoutError`]): 操作超时
/// - **数据库错误** ([`CacheError::DatabaseError`]): 数据库相关错误
/// - **未找到错误** ([`CacheError::NotFound`]): 请求的键不存在
/// - **降级错误** ([`CacheError::Degraded`]): 缓存处于降级模式
/// - **操作错误** ([`CacheError::Operation`]): 一般操作错误
///
/// # 配置阶段错误
///
/// 配置阶段的错误(如缺少必需字段、无效值等)使用 [`CacheConfigError`] 类型,
/// 通过 [`ConfigResult`] 类型别名返回。
///
/// # 示例
///
/// ```rust,ignore
/// use oxcache::error::{CacheError, CacheConfigError, ConfigResult};
///
/// // 运行时错误
/// async fn safe_cache_operation() -> Result<String, CacheError> {
///     let result = cache.get("key").await?;
///     match result {
///         Some(value) => Ok(value),
///         None => Err(CacheError::NotFound("Key not found".to_string()))
///     }
/// }
///
/// // 配置阶段错误
/// fn validate_config(config: &CacheConfig) -> ConfigResult<()> {
///     if config.capacity == 0 {
///         return Err(CacheConfigError::InvalidValue {
///             field: "capacity".to_string(),
///             reason: "must be greater than 0".to_string(),
///         });
///     }
///     Ok(())
/// }
/// ```
#[derive(Error, Debug)]
pub enum CacheError {
    /// 序列化错误
    ///
    /// 通常发生在:
    /// - 尝试序列化不支持的数据类型
    /// - 序列化器配置不兼容
    /// - 数据在传输过程中被损坏
    #[error("Serialization error: {0}. Please check the data format and ensure the serializer is compatible.")]
    Serialization(String),

    /// 操作错误
    ///
    /// 一般的缓存操作失败
    #[error("Operation failed: {0}. Please retry or check your request.")]
    Operation(String),

    /// 连接错误
    ///
    /// 网络连接失败或断开
    #[error("Connection error: {0}. Please check network connectivity and server availability.")]
    Connection(String),

    /// 未找到错误
    ///
    /// 请求的键在缓存中不存在
    #[error("Key not found: {0}. The requested key does not exist in the cache.")]
    NotFound(String),

    /// 降级错误
    ///
    /// 缓存处于降级模式,某些功能不可用
    #[error("Cache degraded: {0}. The cache is operating in degraded mode with limited functionality.")]
    Degraded(String),

    /// L1缓存操作失败
    ///
    /// L1缓存是进程内的内存缓存,可能因以下原因失败:
    /// - 内存不足导致缓存被逐出
    /// - 缓存容量达到上限
    /// - 缓存项过期
    #[error("L1 cache operation failed: {0}. This may indicate memory pressure or configuration issues.")]
    L1Error(String),

    /// L2缓存操作失败
    #[error("L2 cache operation failed: {0}. Please check Redis connection and server status.")]
    L2Error(String),

    /// 操作不支持
    #[error("Operation not supported: {0}. This feature may not be available for the current cache type.")]
    NotSupported(String),

    /// WAL(预写日志)操作失败
    #[error("WAL (Write-Ahead Log) operation failed: {0}. Check disk space and file permissions.")]
    WalError(String),

    /// 数据库错误
    #[error("Database error: {0}. Please check database connectivity and query syntax.")]
    DatabaseError(String),

    /// Redis错误
    #[cfg(feature = "redis")]
    #[error("Redis connection failed: {0}")]
    RedisError(#[from] redis::RedisError),

    /// Redis错误(占位符,当 redis feature 禁用时)
    #[cfg(not(feature = "redis"))]
    #[error("Redis connection failed: {0}. Please enable redis feature and ensure Redis server is running.")]
    RedisError(String),

    /// IO错误
    #[error("I/O error: {0}. Check file permissions and disk space.")]
    IoError(std::io::Error),

    /// 后端错误
    #[error("Backend error: {0}. This may be a transient issue, please retry.")]
    BackendError(String),

    /// 超时错误
    #[error("Operation timed out: {0}. Consider increasing the timeout value or check system performance.")]
    Timeout(String),

    /// 关闭错误
    #[error("Shutdown error: {0}. Some resources may not have been properly released.")]
    ShutdownError(String),

    /// 键过长错误
    #[error("Key too long: {0}. Maximum key length is {1} bytes.")]
    KeyTooLong(usize, usize),

    /// 值过大错误
    #[error("Value too large: {0}. Maximum value size is {1} bytes.")]
    ValueTooLarge(usize, usize),

    /// 缓冲区已满错误
    #[error(
        "Buffer full: {0}. The batch write buffer has reached capacity. Please retry later or increase buffer size."
    )]
    BufferFull(String),

    /// 无效输入错误
    #[error("Invalid input: {0}. The provided input does not meet the required format or constraints.")]
    InvalidInput(String),

    /// 无效键错误
    #[error("Invalid key: {0}. The provided key does not meet the required format or contains forbidden characters.")]
    InvalidKey(String),

    /// 锁错误
    ///
    /// 互斥锁获取失败,通常发生在锁被毒害(之前的持有者 panicked)
    #[error("Lock error: {0}. The lock may have been poisoned by a previous panic.")]
    LockError(String),

    /// 服务配置未找到错误
    ///
    /// 请求的服务配置在 UnifiedConfig 中不存在
    #[error("Service not found: {0}. The requested service configuration does not exist in the UnifiedConfig.")]
    ServiceNotFound(String),

    /// 内部错误
    ///
    /// 内部组件错误,通常表示不可恢复的内部状态异常
    #[error("Internal error: {0}")]
    Internal(String),
}

/// 缓存操作结果类型别名
///
/// 简化错误处理,所有缓存操作都返回此类型
pub type Result<T> = std::result::Result<T, CacheError>;

impl From<std::io::Error> for CacheError {
    fn from(e: std::io::Error) -> Self {
        CacheError::IoError(e)
    }
}

impl From<serde_json::Error> for CacheError {
    fn from(e: serde_json::Error) -> Self {
        CacheError::Serialization(e.to_string())
    }
}

impl CacheError {
    /// 获取错误码
    ///
    /// 返回一个唯一的错误码字符串,便于日志记录和错误追踪。
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use oxcache::error::CacheError;
    ///
    /// let err = CacheError::NotFound("key".to_string());
    /// assert_eq!(err.code(), "CACHE_001");
    ///
    /// let err = CacheError::Connection("failed".to_string());
    /// assert_eq!(err.code(), "CACHE_002");
    /// ```
    pub fn code(&self) -> &'static str {
        match self {
            CacheError::NotFound(_) => "CACHE_001",
            CacheError::Connection(_) => "CACHE_002",
            CacheError::Serialization(_) => "CACHE_003",
            CacheError::Operation(_) => "CACHE_004",
            CacheError::Degraded(_) => "CACHE_005",
            CacheError::L1Error(_) => "CACHE_006",
            CacheError::L2Error(_) => "CACHE_007",
            CacheError::NotSupported(_) => "CACHE_009",
            CacheError::WalError(_) => "CACHE_010",
            CacheError::DatabaseError(_) => "CACHE_011",
            CacheError::RedisError(_) => "CACHE_012",
            CacheError::IoError(_) => "CACHE_013",
            CacheError::BackendError(_) => "CACHE_014",
            CacheError::Timeout(_) => "CACHE_015",
            CacheError::ShutdownError(_) => "CACHE_016",
            CacheError::KeyTooLong(_, _) => "CACHE_017",
            CacheError::ValueTooLarge(_, _) => "CACHE_018",
            CacheError::BufferFull(_) => "CACHE_019",
            CacheError::InvalidInput(_) => "CACHE_020",
            CacheError::InvalidKey(_) => "CACHE_021",
            CacheError::LockError(_) => "CACHE_022",
            CacheError::ServiceNotFound(_) => "CACHE_023",
            CacheError::Internal(_) => "CACHE_024",
        }
    }

    /// 检查错误是否可恢复
    ///
    /// 返回 true 表示错误可能是暂时的,可以重试。
    ///
    /// `Internal` 错误代表内部状态损坏(如锁中毒、数据不一致),不可恢复。
    /// 调用者不应重试此类错误,而应停止操作或重新初始化缓存实例。
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use oxcache::error::CacheError;
    ///
    /// let err = CacheError::Connection("failed".to_string());
    /// assert!(err.is_recoverable());
    ///
    /// let err = CacheError::NotFound("key".to_string());
    /// assert!(!err.is_recoverable());
    /// ```
    pub fn is_recoverable(&self) -> bool {
        matches!(
            self,
            CacheError::Connection(_)
                | CacheError::Timeout(_)
                | CacheError::L2Error(_)
                | CacheError::BackendError(_)
                | CacheError::BufferFull(_)
        )
    }

    /// Check if this error is a "not found" error
    ///
    /// Returns true if the error indicates that a requested key was not found.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use oxcache::error::CacheError;
    ///
    /// let err = CacheError::NotFound("key".to_string());
    /// assert!(err.is_not_found());
    ///
    /// let other_err = CacheError::Connection("failed".to_string());
    /// assert!(!other_err.is_not_found());
    /// ```
    pub fn is_not_found(&self) -> bool {
        matches!(self, CacheError::NotFound(_))
    }

    /// Check if this error is a connection error
    ///
    /// Returns true if the error indicates a connection-related failure.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use oxcache::error::CacheError;
    ///
    /// let err = CacheError::Connection("failed".to_string());
    /// assert!(err.is_connection_error());
    ///
    /// let other_err = CacheError::NotFound("key".to_string());
    /// assert!(!other_err.is_connection_error());
    /// ```
    pub fn is_connection_error(&self) -> bool {
        matches!(
            self,
            CacheError::Connection(_) | CacheError::RedisError(_) | CacheError::L2Error(_)
        )
    }

    /// Check if this error is a degraded mode error
    ///
    /// Returns true if the error indicates the cache is operating in degraded mode.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use oxcache::error::CacheError;
    ///
    /// let err = CacheError::Degraded("L2 unavailable".to_string());
    /// assert!(err.is_degraded());
    ///
    /// let other_err = CacheError::NotFound("key".to_string());
    /// assert!(!other_err.is_degraded());
    /// ```
    pub fn is_degraded(&self) -> bool {
        matches!(self, CacheError::Degraded(_))
    }
}

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

    // ============================================================================
    // CacheConfigError Display tests
    // ============================================================================

    #[test]
    fn test_cache_config_error_missing_field_display() {
        let err = CacheConfigError::MissingField("host".to_string());
        assert_eq!(err.to_string(), "Missing required field: host");
    }

    #[test]
    fn test_cache_config_error_invalid_value_display() {
        let err = CacheConfigError::InvalidValue {
            field: "capacity".to_string(),
            reason: "must be > 0".to_string(),
        };
        assert_eq!(err.to_string(), "Invalid value for field 'capacity': must be > 0");
    }

    #[test]
    fn test_cache_config_error_unsupported_backend_display() {
        let err = CacheConfigError::UnsupportedBackend("unknown".to_string());
        assert_eq!(err.to_string(), "Unsupported backend combination: unknown");
    }

    #[test]
    fn test_cache_config_error_connection_failed_display() {
        let err = CacheConfigError::ConnectionFailed("timeout".to_string());
        assert_eq!(err.to_string(), "Connection failed during initialization: timeout");
    }

    // ============================================================================
    // CacheError Display tests - all variants
    // ============================================================================

    #[test]
    fn test_cache_error_serialization_display() {
        let err = CacheError::Serialization("bad data".to_string());
        let s = err.to_string();
        assert!(s.contains("Serialization error: bad data"));
    }

    #[test]
    fn test_cache_error_operation_display() {
        let err = CacheError::Operation("fail".to_string());
        let s = err.to_string();
        assert!(s.contains("Operation failed: fail"));
    }

    #[test]
    fn test_cache_error_connection_display() {
        let err = CacheError::Connection("refused".to_string());
        let s = err.to_string();
        assert!(s.contains("Connection error: refused"));
    }

    #[test]
    fn test_cache_error_not_found_display() {
        let err = CacheError::NotFound("key1".to_string());
        let s = err.to_string();
        assert!(s.contains("Key not found: key1"));
    }

    #[test]
    fn test_cache_error_degraded_display() {
        let err = CacheError::Degraded("L2 down".to_string());
        let s = err.to_string();
        assert!(s.contains("Cache degraded: L2 down"));
    }

    #[test]
    fn test_cache_error_l1_error_display() {
        let err = CacheError::L1Error("oom".to_string());
        let s = err.to_string();
        assert!(s.contains("L1 cache operation failed: oom"));
    }

    #[test]
    fn test_cache_error_l2_error_display() {
        let err = CacheError::L2Error("redis down".to_string());
        let s = err.to_string();
        assert!(s.contains("L2 cache operation failed: redis down"));
    }

    #[test]
    fn test_cache_error_not_supported_display() {
        let err = CacheError::NotSupported("scan".to_string());
        let s = err.to_string();
        assert!(s.contains("Operation not supported: scan"));
    }

    #[test]
    fn test_cache_error_wal_error_display() {
        let err = CacheError::WalError("disk full".to_string());
        let s = err.to_string();
        assert!(s.contains("WAL (Write-Ahead Log) operation failed: disk full"));
    }

    #[test]
    fn test_cache_error_database_error_display() {
        let err = CacheError::DatabaseError("query failed".to_string());
        let s = err.to_string();
        assert!(s.contains("Database error: query failed"));
    }

    #[test]
    fn test_cache_error_redis_error_display() {
        #[cfg(feature = "redis")]
        {
            let err = CacheError::RedisError(redis::RedisError::from(std::io::Error::new(
                std::io::ErrorKind::Other,
                "auth failed",
            )));
            let s = err.to_string();
            assert!(s.contains("Redis connection failed"));
        }
        #[cfg(not(feature = "redis"))]
        {
            let err = CacheError::RedisError("auth failed".to_string());
            let s = err.to_string();
            assert!(s.contains("Redis connection failed: auth failed"));
        }
    }

    #[test]
    fn test_cache_error_io_error_display() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file missing");
        let err = CacheError::IoError(io_err);
        let s = err.to_string();
        assert!(s.contains("I/O error:"));
    }

    #[test]
    fn test_cache_error_backend_error_display() {
        let err = CacheError::BackendError("transient".to_string());
        let s = err.to_string();
        assert!(s.contains("Backend error: transient"));
    }

    #[test]
    fn test_cache_error_timeout_display() {
        let err = CacheError::Timeout("5s".to_string());
        let s = err.to_string();
        assert!(s.contains("Operation timed out: 5s"));
    }

    #[test]
    fn test_cache_error_shutdown_error_display() {
        let err = CacheError::ShutdownError("leak".to_string());
        let s = err.to_string();
        assert!(s.contains("Shutdown error: leak"));
    }

    #[test]
    fn test_cache_error_key_too_long_display() {
        let err = CacheError::KeyTooLong(600, 512);
        let s = err.to_string();
        assert!(s.contains("Key too long: 600. Maximum key length is 512 bytes."));
    }

    #[test]
    fn test_cache_error_value_too_large_display() {
        let err = CacheError::ValueTooLarge(2048, 1024);
        let s = err.to_string();
        assert!(s.contains("Value too large: 2048. Maximum value size is 1024 bytes."));
    }

    #[test]
    fn test_cache_error_buffer_full_display() {
        let err = CacheError::BufferFull("batch".to_string());
        let s = err.to_string();
        assert!(s.contains("Buffer full: batch"));
    }

    #[test]
    fn test_cache_error_invalid_input_display() {
        let err = CacheError::InvalidInput("bad".to_string());
        let s = err.to_string();
        assert!(s.contains("Invalid input: bad"));
    }

    #[test]
    fn test_cache_error_invalid_key_display() {
        let err = CacheError::InvalidKey("bad key".to_string());
        let s = err.to_string();
        assert!(s.contains("Invalid key: bad key"));
    }

    #[test]
    fn test_cache_error_lock_error_display() {
        let err = CacheError::LockError("poisoned".to_string());
        let s = err.to_string();
        assert!(s.contains("Lock error: poisoned"));
    }

    #[test]
    fn test_cache_error_service_not_found_display() {
        let err = CacheError::ServiceNotFound("svc".to_string());
        let s = err.to_string();
        assert!(s.contains("Service not found: svc"));
    }

    #[test]
    fn test_cache_error_internal_display() {
        let err = CacheError::Internal("boom".to_string());
        let s = err.to_string();
        assert_eq!(s, "Internal error: boom");
    }

    // ============================================================================
    // From conversions
    // ============================================================================

    #[test]
    fn test_from_io_error() {
        let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied");
        let cache_err: CacheError = io_err.into();
        assert!(matches!(cache_err, CacheError::IoError(_)));
    }

    #[test]
    fn test_from_serde_json_error() {
        let serde_err = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
        let cache_err: CacheError = serde_err.into();
        assert!(matches!(cache_err, CacheError::Serialization(_)));
    }

    // ============================================================================
    // code() method tests
    // ============================================================================

    #[test]
    fn test_error_code_not_found() {
        assert_eq!(CacheError::NotFound("k".to_string()).code(), "CACHE_001");
    }

    #[test]
    fn test_error_code_connection() {
        assert_eq!(CacheError::Connection("c".to_string()).code(), "CACHE_002");
    }

    #[test]
    fn test_error_code_serialization() {
        assert_eq!(CacheError::Serialization("s".to_string()).code(), "CACHE_003");
    }

    #[test]
    fn test_error_code_operation() {
        assert_eq!(CacheError::Operation("o".to_string()).code(), "CACHE_004");
    }

    #[test]
    fn test_error_code_degraded() {
        assert_eq!(CacheError::Degraded("d".to_string()).code(), "CACHE_005");
    }

    #[test]
    fn test_error_code_l1() {
        assert_eq!(CacheError::L1Error("l1".to_string()).code(), "CACHE_006");
    }

    #[test]
    fn test_error_code_l2() {
        assert_eq!(CacheError::L2Error("l2".to_string()).code(), "CACHE_007");
    }

    #[test]
    fn test_error_code_not_supported() {
        assert_eq!(CacheError::NotSupported("ns".to_string()).code(), "CACHE_009");
    }

    #[test]
    fn test_error_code_wal() {
        assert_eq!(CacheError::WalError("w".to_string()).code(), "CACHE_010");
    }

    #[test]
    fn test_error_code_database() {
        assert_eq!(CacheError::DatabaseError("db".to_string()).code(), "CACHE_011");
    }

    #[test]
    fn test_error_code_redis() {
        #[cfg(feature = "redis")]
        {
            let err = CacheError::RedisError(redis::RedisError::from(std::io::Error::new(
                std::io::ErrorKind::Other,
                "r",
            )));
            assert_eq!(err.code(), "CACHE_012");
        }
        #[cfg(not(feature = "redis"))]
        {
            assert_eq!(CacheError::RedisError("r".to_string()).code(), "CACHE_012");
        }
    }

    #[test]
    fn test_error_code_io() {
        let io_err = std::io::Error::new(std::io::ErrorKind::Other, "x");
        assert_eq!(CacheError::IoError(io_err).code(), "CACHE_013");
    }

    #[test]
    fn test_error_code_backend() {
        assert_eq!(CacheError::BackendError("b".to_string()).code(), "CACHE_014");
    }

    #[test]
    fn test_error_code_timeout() {
        assert_eq!(CacheError::Timeout("t".to_string()).code(), "CACHE_015");
    }

    #[test]
    fn test_error_code_shutdown() {
        assert_eq!(CacheError::ShutdownError("s".to_string()).code(), "CACHE_016");
    }

    #[test]
    fn test_error_code_key_too_long() {
        assert_eq!(CacheError::KeyTooLong(1, 2).code(), "CACHE_017");
    }

    #[test]
    fn test_error_code_value_too_large() {
        assert_eq!(CacheError::ValueTooLarge(1, 2).code(), "CACHE_018");
    }

    #[test]
    fn test_error_code_buffer_full() {
        assert_eq!(CacheError::BufferFull("b".to_string()).code(), "CACHE_019");
    }

    #[test]
    fn test_error_code_invalid_input() {
        assert_eq!(CacheError::InvalidInput("i".to_string()).code(), "CACHE_020");
    }

    #[test]
    fn test_error_code_invalid_key() {
        assert_eq!(CacheError::InvalidKey("k".to_string()).code(), "CACHE_021");
    }

    #[test]
    fn test_error_code_lock_error() {
        assert_eq!(CacheError::LockError("l".to_string()).code(), "CACHE_022");
    }

    #[test]
    fn test_error_code_service_not_found() {
        assert_eq!(CacheError::ServiceNotFound("s".to_string()).code(), "CACHE_023");
    }

    #[test]
    fn test_error_code_internal() {
        assert_eq!(CacheError::Internal("i".to_string()).code(), "CACHE_024");
    }

    // ============================================================================
    // is_recoverable() tests
    // ============================================================================

    #[test]
    fn test_is_recoverable_connection() {
        assert!(CacheError::Connection("c".to_string()).is_recoverable());
    }

    #[test]
    fn test_is_recoverable_timeout() {
        assert!(CacheError::Timeout("t".to_string()).is_recoverable());
    }

    #[test]
    fn test_is_recoverable_l2() {
        assert!(CacheError::L2Error("l2".to_string()).is_recoverable());
    }

    #[test]
    fn test_is_recoverable_backend() {
        assert!(CacheError::BackendError("b".to_string()).is_recoverable());
    }

    #[test]
    fn test_is_recoverable_buffer_full() {
        assert!(CacheError::BufferFull("b".to_string()).is_recoverable());
    }

    #[test]
    fn test_is_not_recoverable_not_found() {
        assert!(!CacheError::NotFound("k".to_string()).is_recoverable());
    }

    #[test]
    fn test_is_not_recoverable_internal() {
        assert!(!CacheError::Internal("i".to_string()).is_recoverable());
    }

    #[test]
    fn test_is_not_recoverable_serialization() {
        assert!(!CacheError::Serialization("s".to_string()).is_recoverable());
    }

    // ============================================================================
    // is_not_found() tests
    // ============================================================================

    #[test]
    fn test_is_not_found_true() {
        assert!(CacheError::NotFound("key".to_string()).is_not_found());
    }

    #[test]
    fn test_is_not_found_false() {
        assert!(!CacheError::Connection("c".to_string()).is_not_found());
    }

    // ============================================================================
    // is_connection_error() tests
    // ============================================================================

    #[test]
    fn test_is_connection_error_connection() {
        assert!(CacheError::Connection("c".to_string()).is_connection_error());
    }

    #[test]
    fn test_is_connection_error_redis() {
        #[cfg(feature = "redis")]
        {
            let err = CacheError::RedisError(redis::RedisError::from(std::io::Error::new(
                std::io::ErrorKind::ConnectionRefused,
                "r",
            )));
            assert!(err.is_connection_error());
        }
        #[cfg(not(feature = "redis"))]
        {
            assert!(CacheError::RedisError("r".to_string()).is_connection_error());
        }
    }

    #[test]
    fn test_is_connection_error_l2() {
        assert!(CacheError::L2Error("l2".to_string()).is_connection_error());
    }

    #[test]
    fn test_is_connection_error_false() {
        assert!(!CacheError::NotFound("k".to_string()).is_connection_error());
    }

    // ============================================================================
    // is_degraded() tests
    // ============================================================================

    #[test]
    fn test_is_degraded_true() {
        assert!(CacheError::Degraded("d".to_string()).is_degraded());
    }

    #[test]
    fn test_is_degraded_false() {
        assert!(!CacheError::NotFound("k".to_string()).is_degraded());
    }

    // ============================================================================
    // Debug trait test
    // ============================================================================

    #[test]
    fn test_cache_error_debug() {
        let err = CacheError::NotFound("key".to_string());
        let debug_str = format!("{:?}", err);
        assert!(debug_str.contains("NotFound"));
    }

    #[test]
    fn test_cache_config_error_debug() {
        let err = CacheConfigError::MissingField("f".to_string());
        let debug_str = format!("{:?}", err);
        assert!(debug_str.contains("MissingField"));
    }

    // ============================================================================
    // std::error::Error trait test
    // ============================================================================

    #[test]
    fn test_cache_error_is_std_error() {
        let err = CacheError::NotFound("key".to_string());
        let _: &dyn std::error::Error = &err;
    }

    #[test]
    fn test_cache_config_error_is_std_error() {
        let err = CacheConfigError::MissingField("f".to_string());
        let _: &dyn std::error::Error = &err;
    }
}