oxcache 0.1.4

A high-performance multi-level cache library for Rust with L1 (memory) and L2 (Redis) caching.
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
//! Copyright (c) 2025-2026, Kirky.X
//!
//! MIT License
//!
//! 配置验证模块
//!
//! 提供 feature-gated 的配置验证。

use secrecy::ExposeSecret;
use std::collections::HashMap;

use crate::config::{GlobalConfig, OxcacheConfig, ServiceConfig};

// ============================================================================
// 常量定义
// ============================================================================

/// 默认 TTL(秒)
pub const DEFAULT_TTL: u64 = 3600;

/// 最大 TTL(30天,秒)
pub const MAX_TTL: u64 = 86400 * 30;

/// 最小 TTL(秒)
pub const MIN_TTL: u64 = 1;

/// 服务名称最大长度
pub const MAX_SERVICE_NAME_LENGTH: usize = 64;

/// 健康检查间隔最小值(秒)
pub const MIN_HEALTH_CHECK_INTERVAL: u64 = 1;

/// 健康检查间隔最大值(秒)
pub const MAX_HEALTH_CHECK_INTERVAL: u64 = 3600;

/// L1 缓存最大容量
pub const MAX_L1_CAPACITY: usize = 10_000_000;

/// 批量写入最大大小
pub const MAX_BATCH_SIZE: usize = 10000;

/// 批量写入间隔最大值(毫秒)
pub const MAX_BATCH_INTERVAL_MS: u64 = 60000;

/// 最大键长度(字节)
pub const MAX_KEY_LENGTH: usize = 1024;

/// 最大值大小(10MB,字节)
pub const MAX_VALUE_SIZE: usize = 10 * 1024 * 1024;

/// L2 连接超时最小值(毫秒)
pub const MIN_L2_CONNECTION_TIMEOUT_MS: u64 = 100;

/// L2 连接超时最大值(毫秒)
pub const MAX_L2_CONNECTION_TIMEOUT_MS: u64 = 30000;

/// L2 命令超时最小值(毫秒)
pub const MIN_L2_COMMAND_TIMEOUT_MS: u64 = 100;

/// L2 命令超时最大值(毫秒)
pub const MAX_L2_COMMAND_TIMEOUT_MS: u64 = 60000;

/// 生产环境关键词列表
pub const PRODUCTION_KEYWORDS: &[&str] = &["production", "prod"];

/// 默认重试间隔(毫秒)
pub const DEFAULT_RETRY_INTERVAL_MS: u64 = 100;

/// 默认最大重试次数
pub const DEFAULT_MAX_RETRIES: u32 = 3;

/// 默认 WAL 条目最大 TTL(30天,秒)
pub const MAX_WAL_ENTRY_TTL: u64 = 30 * 24 * 3600;

/// 默认内存大小(100MB)
pub const DEFAULT_MAX_MEMORY_BYTES: usize = 100 * 1024 * 1024;

/// 配置验证trait
pub trait ConfigValidation {
    /// 验证配置
    fn validate(&self) -> Result<(), String>;
}

/// 验证逻辑实现
impl ConfigValidation for OxcacheConfig {
    fn validate(&self) -> Result<(), String> {
        // 验证全局配置
        self.validate_global()?;

        // 验证服务配置
        for (name, service) in &self.services {
            self.validate_service(name, service)?;
        }

        Ok(())
    }
}

impl OxcacheConfig {
    /// 验证全局配置
    pub fn validate_global(&self) -> Result<(), String> {
        let global = &self.global;

        if global.default_ttl == 0 {
            return Err("Global default_ttl cannot be zero".to_string());
        }

        if global.default_ttl > MAX_TTL {
            return Err(format!(
                "Global default_ttl cannot exceed {} days",
                MAX_TTL / 86400
            ));
        }

        if global.health_check_interval == 0 {
            return Err("Global health_check_interval cannot be zero".to_string());
        }

        if global.health_check_interval < MIN_HEALTH_CHECK_INTERVAL
            || global.health_check_interval > MAX_HEALTH_CHECK_INTERVAL
        {
            return Err(format!(
                "Global health_check_interval must be between {} and {} seconds",
                MIN_HEALTH_CHECK_INTERVAL, MAX_HEALTH_CHECK_INTERVAL
            ));
        }

        Ok(())
    }

    /// 验证单个服务配置
    pub fn validate_service(&self, name: &str, service: &ServiceConfig) -> Result<(), String> {
        // 验证服务名称
        if name.is_empty() {
            return Err("Service name cannot be empty".to_string());
        }

        if name.len() > MAX_SERVICE_NAME_LENGTH {
            return Err(format!(
                "Service name '{}' exceeds maximum length of {} characters",
                name, MAX_SERVICE_NAME_LENGTH
            ));
        }

        let global = &self.global;
        let service_ttl = service.ttl.unwrap_or(global.default_ttl);

        // 验证 TTL
        if service_ttl == 0 {
            return Err(format!("Service '{}' TTL cannot be zero", name));
        }

        if service_ttl > MAX_TTL {
            return Err(format!(
                "Service '{}' TTL cannot exceed {} days",
                name,
                MAX_TTL / 86400
            ));
        }

        // 验证 L1 配置(需要 l1-moka feature)
        #[cfg(feature = "l1-moka")]
        if let Some(l1_config) = &service.l1 {
            Self::validate_l1_config(name, l1_config, service_ttl)?;
        }

        // 验证 L2 配置(需要 l2-redis feature)
        #[cfg(feature = "l2-redis")]
        if let Some(l2_config) = &service.l2 {
            Self::validate_l2_config(name, l2_config, service_ttl)?;
        }

        // 验证双层缓存配置(需要 l2-redis feature)
        #[cfg(feature = "l2-redis")]
        if let Some(two_level_config) = &service.two_level {
            Self::validate_two_level_config(name, two_level_config)?;
        }

        Ok(())
    }

    /// 验证 L1 配置(需要 l1-moka feature)
    #[cfg(feature = "l1-moka")]
    fn validate_l1_config(
        name: &str,
        l1_config: &crate::config::L1Config,
        service_ttl: u64,
    ) -> Result<(), String> {
        if l1_config.max_capacity == 0 {
            return Err(format!("Service '{}' L1 max_capacity cannot be zero", name));
        }

        if l1_config.max_capacity > MAX_L1_CAPACITY as u64 {
            return Err(format!(
                "Service '{}' L1 max_capacity cannot exceed {}",
                name, MAX_L1_CAPACITY
            ));
        }

        if l1_config.cleanup_interval_secs > 0 && l1_config.cleanup_interval_secs > service_ttl {
            return Err(format!(
                "Service '{}' L1 cleanup_interval_secs ({}) must be <= service TTL ({})",
                name, l1_config.cleanup_interval_secs, service_ttl
            ));
        }

        Ok(())
    }

    /// 验证 L2 配置(需要 l2-redis feature)
    #[cfg(feature = "l2-redis")]
    fn validate_l2_config(
        name: &str,
        l2_config: &crate::config::L2Config,
        service_ttl: u64,
    ) -> Result<(), String> {
        // 验证 L1 TTL <= L2 TTL
        if let Some(l2_specific_ttl) = l2_config.default_ttl {
            if l2_specific_ttl == 0 {
                return Err(format!("Service '{}' L2 TTL cannot be zero", name));
            }

            if service_ttl > l2_specific_ttl {
                return Err(format!(
                    "Service '{}' L1 TTL ({}) must be <= L2 TTL ({})",
                    name, service_ttl, l2_specific_ttl
                ));
            }
        }

        // 验证连接超时
        let timeout = l2_config.connection_timeout_ms;
        if !(MIN_L2_CONNECTION_TIMEOUT_MS..=MAX_L2_CONNECTION_TIMEOUT_MS).contains(&timeout) {
            return Err(format!(
                "Service '{}' connection_timeout_ms must be between {} and {} ms",
                name, MIN_L2_CONNECTION_TIMEOUT_MS, MAX_L2_CONNECTION_TIMEOUT_MS
            ));
        }

        // 验证命令超时
        let timeout = l2_config.command_timeout_ms;
        if !(MIN_L2_COMMAND_TIMEOUT_MS..=MAX_L2_COMMAND_TIMEOUT_MS).contains(&timeout) {
            return Err(format!(
                "Service '{}' command_timeout_ms must be between {} and {} ms",
                name, MIN_L2_COMMAND_TIMEOUT_MS, MAX_L2_COMMAND_TIMEOUT_MS
            ));
        }

        // 生产环境安全检查
        let conn_str = l2_config.connection_string.expose_secret();

        // 更精确的生产环境检测
        let is_production = conn_str.contains("production")
            || conn_str.contains("prod")
            || (!conn_str.contains("localhost")
                && !conn_str.contains("127.0.0.1")
                && !conn_str.contains("192.168.")
                && !conn_str.contains("10.")
                && !conn_str.contains("172.16.")
                && !conn_str.contains("172.17.")
                && !conn_str.contains("172.18.")
                && !conn_str.contains("172.19.")
                && !conn_str.contains("172.20.")
                && !conn_str.contains("172.21.")
                && !conn_str.contains("172.22.")
                && !conn_str.contains("172.23.")
                && !conn_str.contains("172.24.")
                && !conn_str.contains("172.25.")
                && !conn_str.contains("172.26.")
                && !conn_str.contains("172.27.")
                && !conn_str.contains("172.28.")
                && !conn_str.contains("172.29.")
                && !conn_str.contains("172.30.")
                && !conn_str.contains("172.31.")
                && !conn_str.contains(".local")
                && !conn_str.contains(".dev")
                && !conn_str.contains(".test")
                && !conn_str.contains(".staging")
                && !conn_str.contains(".internal"));

        if is_production {
            // 检查密码
            if l2_config.password.is_none() {
                return Err(format!(
                    "Service '{}' is in production but Redis password is not configured. \
                     Production deployments require authentication.",
                    name
                ));
            }

            // 检查密码复杂度
            if let Some(password) = &l2_config.password {
                let password = password.expose_secret();

                // 检查密码长度(最少 16 字符)
                if password.len() < 16 {
                    return Err(format!(
                        "Service '{}' is in production but Redis password is too weak ({} chars, minimum 16 required). \
                         Production deployments require strong passwords.",
                        name,
                        password.len()
                    ));
                }

                // 检查密码复杂度
                let has_upper = password.chars().any(|c| c.is_uppercase());
                let has_lower = password.chars().any(|c| c.is_lowercase());
                let has_digit = password.chars().any(|c| c.is_ascii_digit());
                let has_special = password
                    .chars()
                    .any(|c| "!@#$%^&*()_+-=[]{}|;:,.<>?/~`".contains(c));

                if !has_upper || !has_lower || !has_digit || !has_special {
                    let missing = vec![
                        if !has_upper { "uppercase letter" } else { "" },
                        if !has_lower { "lowercase letter" } else { "" },
                        if !has_digit { "digit" } else { "" },
                        if !has_special {
                            "special character"
                        } else {
                            ""
                        },
                    ]
                    .into_iter()
                    .filter(|s| !s.is_empty())
                    .collect::<Vec<_>>()
                    .join(", ");

                    return Err(format!(
                        "Service '{}' is in production but Redis password does not meet complexity requirements. \
                         Missing: {}. Password must contain uppercase, lowercase, digit, and special character.",
                        name, missing
                    ));
                }

                // 检查常见弱密码
                const WEAK_PASSWORDS: &[&str] = &[
                    "password",
                    "Password123",
                    "Admin123",
                    "Root123",
                    "Redis123",
                    "Cache123",
                    "Welcome123",
                    "P@ssw0rd",
                    "Admin@123",
                    "Root@123",
                    "Redis@123",
                ];

                if WEAK_PASSWORDS.iter().any(|weak| password == *weak) {
                    return Err(format!(
                        "Service '{}' is in production but Redis password is too weak (matches common weak password list). \
                         Please use a strong, unique password.",
                        name
                    ));
                }
            }

            // 检查 TLS
            if !l2_config.enable_tls {
                return Err(format!(
                    "Service '{}' is in production but TLS is not enabled. \
                     Production deployments require TLS encryption.",
                    name
                ));
            }

            // 检查 TLS 证书验证
            #[cfg(feature = "l2-redis")]
            if l2_config.enable_tls {
                // 检查是否禁用了证书验证(不安全)
                // 注意:这里假设 L2Config 有 tls_insecure 字段
                // 如果没有,需要添加该字段或使用其他方式检查
                let tls_insecure = false; // 默认值,需要根据实际配置调整

                if tls_insecure {
                    return Err(format!(
                        "Service '{}' is in production but TLS certificate verification is disabled. \
                         This is a security risk. Please enable certificate verification.",
                        name
                    ));
                }
            }
        }

        Ok(())
    }

    /// 验证双层缓存配置(需要 l2-redis feature)
    #[cfg(feature = "l2-redis")]
    fn validate_two_level_config(
        name: &str,
        two_level_config: &crate::config::TwoLevelConfig,
    ) -> Result<(), String> {
        // 验证批量写入配置
        if two_level_config.enable_batch_write {
            if two_level_config.batch_size == 0 {
                return Err(format!(
                    "Service '{}' batch_size cannot be zero when batch_write is enabled",
                    name
                ));
            }

            if two_level_config.batch_size > MAX_BATCH_SIZE {
                return Err(format!(
                    "Service '{}' batch_size cannot exceed {}",
                    name, MAX_BATCH_SIZE
                ));
            }

            if two_level_config.batch_interval_ms == 0 {
                return Err(format!(
                    "Service '{}' batch_interval_ms cannot be zero when batch_write is enabled",
                    name
                ));
            }

            if two_level_config.batch_interval_ms > MAX_BATCH_INTERVAL_MS {
                return Err(format!(
                    "Service '{}' batch_interval_ms cannot exceed {} ms",
                    name, MAX_BATCH_INTERVAL_MS
                ));
            }
        }

        // 验证键大小限制
        if let Some(max_key_length) = two_level_config.max_key_length {
            if max_key_length == 0 || max_key_length > MAX_KEY_LENGTH {
                return Err(format!(
                    "Service '{}' max_key_length must be between 1 and {}",
                    name, MAX_KEY_LENGTH
                ));
            }
        }

        // 验证值大小限制
        if let Some(max_value_size) = two_level_config.max_value_size {
            if max_value_size == 0 || max_value_size > MAX_VALUE_SIZE {
                return Err(format!(
                    "Service '{}' max_value_size must be between 1 and {}MB",
                    name,
                    MAX_VALUE_SIZE / (1024 * 1024)
                ));
            }
        }

        Ok(())
    }
}

/// 从旧配置迁移验证逻辑(向后兼容)
#[deprecated(since = "0.2.0", note = "请使用 OxcacheConfig 的验证方法")]
pub fn validate_service(
    name: &str,
    service: &ServiceConfig,
    global: &GlobalConfig,
) -> Result<(), String> {
    let config = OxcacheConfig {
        config_version: None,
        global: global.clone(),
        services: HashMap::new(),
        #[cfg(feature = "l1-moka")]
        layer: None,
        #[cfg(feature = "confers")]
        extensions: HashMap::new(),
        #[cfg(feature = "confers")]
        source: None,
    };

    config.validate_service(name, service)
}

#[cfg(test)]
mod tests {
    use crate::config::{oxcache_config, GlobalConfig, ServiceConfig};

    #[test]
    fn test_validate_empty_service_name() {
        let config = oxcache_config()
            .with_global(GlobalConfig::default())
            .build();

        let service = ServiceConfig::l1_only();
        assert!(config.validate_service("", &service).is_err());
    }

    #[test]
    fn test_validate_valid_service() {
        let config = oxcache_config()
            .with_global(GlobalConfig::default())
            .build();

        let service = ServiceConfig::l1_only();
        assert!(config.validate_service("valid_service", &service).is_ok());
    }

    #[test]
    fn test_validate_zero_ttl() {
        let config = oxcache_config()
            .with_global(GlobalConfig::default())
            .build();

        let service = ServiceConfig::l1_only().with_ttl(0);
        assert!(config.validate_service("test", &service).is_err());
    }

    #[test]
    #[cfg(feature = "l1-moka")]
    fn test_validate_l1_capacity() {
        let config = oxcache_config()
            .with_global(GlobalConfig::default())
            .build();

        let l1 = crate::config::L1Config {
            max_capacity: 0,
            ..Default::default()
        };
        let service = ServiceConfig::l1_only().with_l1(l1);

        assert!(config.validate_service("test", &service).is_err());
    }

    #[test]
    fn test_validate_global_config() {
        let global = GlobalConfig {
            default_ttl: 0,
            ..Default::default()
        };

        let config = oxcache_config().with_global(global).build();

        assert!(config.validate_global().is_err());
    }

    #[test]
    fn test_validate_service_name_too_long() {
        let config = oxcache_config()
            .with_global(GlobalConfig::default())
            .build();

        let long_name = "a".repeat(65);
        let service = ServiceConfig::l1_only();

        assert!(config.validate_service(&long_name, &service).is_err());
    }
}