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
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
//! Copyright (c) 2025-2026, Kirky.X
//!
//! MIT License
//!
//! oxcache - 高性能多层缓存库
//!
//! 提供L1内存缓存和L2分布式缓存的两级缓存解决方案,
//! 支持缓存降级、故障恢复和优雅关闭等功能。
//!
//! # Modern API (Recommended)
//!
//! The new API (v0.2.0+) provides a type-safe, independent cache interface:
//!
//! ```rust,ignore
//! use oxcache::Cache;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct User {
//!     id: u64,
//!     name: String,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Simple memory cache
//!     let cache: Cache<String, User> = Cache::new().await?;
//!
//!     // Set a value
//!     let user = User { id: 1, name: "Alice".to_string() };
//!     cache.set(&"user:1".to_string(), &user).await?;
//!
//!     // Get a value
//!     let user: Option<User> = cache.get(&"user:1".to_string()).await?;
//!
//!     // Cache-aside pattern with fallback
//!     let user: User = cache.get_or(&"user:1".to_string(), || async {
//!         fetch_user_from_db(1).await
//!     }).await?;
//!
//!     Ok(())
//! }
//! ```
//!
//! # Cache Types
//!
//! ## Memory Cache
//!
//! ```rust,ignore
//! let cache: Cache<String, MyType> = Cache::new().await?;
//! // or
//! let cache: Cache<String, MyType> = Cache::memory().await?;
//! ```
//!
//! ## Redis Cache
//!
//! ```rust,ignore
//! let cache: Cache<String, MyType> = Cache::redis("redis://localhost:6379").await?;
//! ```
//!
//! ## Tiered Cache (L1 + L2)
//!
//! ```rust,ignore
//! let cache: Cache<String, MyType> = Cache::tiered(10000, "redis://localhost:6379").await?;
//! ```
//!
//! # Advanced Configuration
//!
//! ```rust,ignore
//! use oxcache::{Cache, builder::BackendBuilder};
//! use std::time::Duration;
//!
//! let cache: Cache<String, User> = Cache::builder()
//!     .backend(
//!         BackendBuilder::tiered()
//!             .l1_capacity(10000)
//!             .l2_connection_string("redis://localhost:6379")
//!             .auto_promote(true)
//!     )
//!     .ttl(Duration::from_secs(3600))
//!     .build()
//!     .await?;
//! ```
//!
//! # Key Types
//!
//! The new API supports any type implementing `CacheKey`:
//!
//! ```rust,ignore
//! // String keys (default)
//! let cache: Cache<String, User> = Cache::new().await?;
//!
//! // Numeric keys
//! let cache: Cache<u64, User> = Cache::new().await?;
//!
//! // Custom key type
//! impl oxcache::traits::CacheKey for UserId {
//!     fn to_key_string(&self) -> String {
//!         format!("user:{}", self.0)
//!     }
//! }
//!
//! let cache: Cache<UserId, User> = Cache::new().await?;
//! ```
//!
//! # Batch Operations
//!
//! ```rust,ignore
//! // Batch set
//! cache.set_many(vec![
//!     (&"key1".to_string(), &value1),
//!     (&"key2".to_string(), &value2),
//! ]).await?;
//!
//! // Batch get
//! let results: HashMap<String, User> = cache.get_many(vec![
//!     &"key1".to_string(),
//!     &"key2".to_string(),
//! ]).await?;
//!
//! // Batch delete
//! cache.delete_many(vec![
//!     &"key1".to_string(),
//!     &"key2".to_string(),
//! ]).await?;
//! ```
//!
//! # Migration from Old API
//!
//! If you're using the old API (v0.1.x), see the migration guide:
//! - [Migration Guide](https://docs.rs/oxcache/latest/oxcache/docs/migration/index.html)
//!
//! The old API is deprecated but still functional. To migrate:
//!
//! Old API:
//! ```rust,ignore
//! let config = oxcache_config()
//!     .with_service("default", ServiceConfig::two_level())
//!     .build();
//! oxcache::init(config).await?;
//! let client = oxcache::get_client("default")?;
//! ```
//!
//! New API:
//! ```rust,ignore
//! let cache: Cache<String, User> = Cache::tiered(10000, "redis://localhost:6379").await?;
//! ```
//!
//! # Features
//!
//! - `l1-moka`: Enable L1 memory cache (Moka)
//! - `l2-redis`: Enable L2 distributed cache (Redis)
//! - `serialization`: Enable JSON/Bincode serialization
//! - `metrics`: Enable OpenTelemetry metrics
//! - `wal-recovery`: Enable write-ahead log for recovery
//! - `batch-write`: Enable optimized batch writes
//! - `full`: Enable all features
//!
//! # Example
//!
//! ```rust,ignore
//! use oxcache::Cache;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create a tiered cache
//!     let cache: Cache<String, String> = Cache::tiered(
//!         10000,
//!         "redis://localhost:6379"
//!     ).await?;
//!
//!     // Set and get
//!     cache.set(&"key".to_string(), &"value".to_string()).await?;
//!     let value = cache.get(&"key".to_string()).await?;
//!
//!     println!("Got value: {:?}", value);
//!     Ok(())
//! }
//! ```

#![doc(html_root_url = "https://docs.rs/oxcache/0.2.0")]

// ============================================================================
// Feature Flags and Macros
// ============================================================================

/// 检查是否启用了指定的功能特性
#[macro_export]
macro_rules! has_feature {
    ($feature:expr) => {
        cfg!(feature = $feature)
    };
}

/// 编译时断言:确保功能依赖满足
///
/// # Example
///
/// ```rust,ignore
/// require_feature!(cfg!(feature = "l1-moka"), "bloom-filter");
/// ```
///
/// 如果启用了 `bloom-filter` 但没有启用 `l1-moka`,编译时会panic。
/// 这是为了在编译时捕获配置错误,而不是在运行时。
#[macro_export]
macro_rules! require_feature {
    ($required:expr, $dependent:expr) => {
        const _: fn() = || {
            if !$required && cfg!(feature = $dependent) {
                panic!(
                    "Feature '{}' requires feature '{}' to be enabled. \
                    Add '{}' to your Cargo.toml features or use the 'full' feature.",
                    $dependent,
                    stringify!($required),
                    stringify!($required)
                )
            }
        };
    };
}

/// 编译时断言:检查特性依赖关系(支持full特性)
///
/// # Example
///
/// ```rust,ignore
/// check_feature_dependence!("bloom-filter", cfg!(feature = "l1-moka"));
/// ```
///
/// 如果启用了 `bloom-filter` 但没有启用 `l1-moka` 或 `full`,编译时会panic。
#[macro_export]
macro_rules! check_feature_dependence {
    ($dependent:expr, $required:expr) => {
        const _: fn() = || {
            if cfg!(feature = $dependent) && !$required && !cfg!(feature = "full") {
                panic!(
                    "ERROR: '{}' feature requires '{}' feature.\n\
                    \n\
                    Solution 1: Enable required feature:\n\
                        oxcache = {{ version = \"0.1\", features = [\"{}\", \"{}\"] }}\n\
                    \n\
                    Solution 2: Enable all features:\n\
                        oxcache = {{ version = \"0.1\", features = [\"full\"] }}",
                    $dependent,
                    stringify!($required)
                        .replace("cfg!(feature = \\\"", "")
                        .replace("\\\")\"", ""),
                    $dependent,
                    stringify!($required)
                        .replace("cfg!(feature = \\\"", "")
                        .replace("\\\")\"", "")
                )
            }
        };
    };
}

/// 运行时检查特性是否启用(用于available_features等场景)
#[macro_export]
macro_rules! add_feature_if_enabled {
    ($features:ident, $name:expr) => {
        if cfg!(feature = $name) {
            $features.push($name);
        }
    };
}

/// 为禁用的特性生成空实现结构体及其基本实现
///
/// # Example
///
/// ```rust,ignore
/// empty_struct!(BatchWriter, Debug, Clone, Default);
/// ```
///
/// 生成:
/// - `#[cfg(not(feature = "batch-write"))]`
/// - `#[derive(Debug, Clone, Default)] pub struct BatchWriter;`
/// - `impl BatchWriter { pub fn new() -> Self { Self } }
#[macro_export]
macro_rules! empty_struct {
    ($name:ident, $($traits:ident),+ $(,)?) => {
        #[cfg(not(feature = "batch-write"))]
        #[derive($($traits),+)]
        pub struct $name;
        #[cfg(not(feature = "batch-write"))]
        impl $name {
            pub fn new() -> Self {
                Self
            }
        }
    };
}

/// 为禁用的特性生成空实现结构体(带泛型参数)
///
/// # Example
///
/// ```rust,ignore
/// empty_struct_generic!(HealthChecker<T: HealthCheckableBackend>, Debug);
/// ```
#[macro_export]
macro_rules! empty_struct_generic {
    ($name:ident $(<$($generics:tt),+>)?, $($traits:ident),+ $(,)?) => {
        #[cfg(not(feature = "wal-recovery"))]
        #[derive($($traits),+)]
        pub struct $name $(<$($generics),+>)?;
        #[cfg(not(feature = "wal-recovery"))]
        impl $(<$($generics),+>)? $name $(<$($generics),+>)? {
            pub fn new() -> Self {
                Self
            }
        }
    };
}

/// 为禁用的特性生成带有 async 方法的空实现
///
/// # Example
///
/// ```rust,ignore
/// empty_async_methods!(MyStruct, {
///     pub async fn start(&self) {}
///     pub async fn shutdown(&self) {}
/// });
/// ```
#[macro_export]
macro_rules! empty_async_methods {
    ($name:ident, { $(pub async fn $fn_name:ident(&self $(, $param:ident: $param_type: ty)* $(,)? ) -> Result<()> { $($body:stmt)* })+ }) => {
        #[cfg(not(feature = "batch-write"))]
        #[derive(Debug, Clone, Default)]
        pub struct $name;
        #[cfg(not(feature = "batch-write"))]
        impl $name {
            $(
                pub async fn $fn_name(&self $(, $param: $param_type)*) -> Result<()> {
                    $($body)*
                    Ok(())
                }
            )+
        }
    };
}

/// 为禁用的特性生成空 trait 定义
///
/// # Example
///
/// ```rust,ignore
/// empty_trait!(HealthCheckableBackend, Clone + Send + Sync + 'static {
///     async fn ping(&self) -> Result<()>;
///     fn command_timeout_ms(&self) -> u64;
/// });
/// ```
#[macro_export]
macro_rules! empty_trait {
    ($name:ident, $($bounds:tt)*) => {
        #[cfg(not(feature = "wal-recovery"))]
        #[async_trait::async_trait]
        pub trait $name: $($bounds)* {}
    };
}

/// 生成空的 Result 返回方法
///
/// # Example
///
/// ```rust,ignore
/// empty_async_fn!(pub async fn foo(&self) -> Result<()>);
/// empty_async_fn!(pub async fn bar(&self, key: &str) -> Result<()>);
/// ```
#[macro_export]
macro_rules! empty_async_fn {
    (pub async fn $fn_name:ident (&self $(, $param:ident: $param_type: ty)*) -> Result<()>) => {
        #[cfg(not(feature = "batch-write"))]
        pub async fn $fn_name(&self $(, $param: $param_type)*) -> Result<()> {
            Ok(())
        }
    };
}

/// 为禁用功能的模块生成占位符模块声明
///
/// # Example
///
/// ```rust,ignore
/// placeholder_module!(batch_writer, "batch-write");
/// ```
#[macro_export]
macro_rules! placeholder_module {
    ($module:ident, $feature:expr) => {
        #[cfg(not(feature = $feature))]
        pub(crate) mod $module;
    };
}

/// Initialize cache configuration from a function.
///
/// This macro generates code that calls the provided function to get configuration,
/// then initializes all caches from that configuration.
///
/// # Arguments
/// * `path` (optional) - Path to a TOML config file. If provided, uses confers_load.
/// * `config` (optional) - A function that returns `OxcacheConfig`.
///
/// Either `path` or `config` must be provided, but not both.
///
/// # Example
///
/// ```rust,ignore
/// #[oxcache::init_config]
/// fn load_config() -> oxcache::OxcacheConfig {
///     oxcache::oxcache_config()
///         .with_service("default", oxcache::ServiceConfig::two_level())
///         .build()
/// }
/// ```
// ============================================================================
// Core Modules (Always Available)
// ============================================================================
pub mod client;
pub mod config;
pub mod error;

// Internal module for macro support (deprecated, for internal use only)
#[deprecated(
    since = "0.2.0",
    note = "Internal module, will be made private in v0.3.0"
)]
pub mod manager;

// Internal module for #[cached] macro support
#[doc(hidden)]
pub mod internal;

// New modernized API modules
pub mod builder;
pub mod cache;
pub mod traits;

// ============================================================================
// Optional Feature-Gated Modules
// ============================================================================

// Backend module (L1/L2 cache implementation)
#[cfg(any(
    feature = "l1-moka",
    feature = "l2-redis",
    feature = "minimal",
    feature = "core",
    feature = "full"
))]
pub mod backend;

// Bloom Filter Module
#[cfg(any(feature = "bloom-filter", feature = "full"))]
pub mod bloom_filter;

// Metrics Module - also needed for L1-only mode
#[cfg(any(
    feature = "metrics",
    feature = "l1-moka",
    feature = "l2-redis",
    feature = "minimal",
    feature = "core",
    feature = "full",
    feature = "batch-write",
    feature = "cli"
))]
pub mod metrics;

// Rate Limiting Module
#[cfg(any(feature = "rate-limiting", feature = "full"))]
pub mod rate_limiting;

// WAL Recovery Module
#[cfg(any(feature = "wal-recovery", feature = "l2-redis", feature = "full"))]
pub mod recovery;

// Sync writer
#[cfg(any(
    feature = "batch-write",
    feature = "l2-redis",
    feature = "sync",
    feature = "full"
))]
pub mod sync;

// Database Module
#[cfg(any(feature = "database", feature = "full"))]
pub mod database;

// OpenTelemetry Module
#[cfg(any(feature = "opentelemetry", feature = "full"))]
pub mod telemetry;

// Serialization Module
#[cfg(any(feature = "serialization", feature = "full"))]
pub mod serialization;

// Utils Module
#[cfg(any(feature = "full", feature = "minimal", feature = "core"))]
pub mod utils;

// Smart Strategy Module
#[cfg(any(feature = "smart-strategy", feature = "full"))]
pub mod smart_strategy;

// HTTP Cache Module
#[cfg(any(feature = "http-cache", feature = "full"))]
pub mod http;

// Security Module (Always available for public use)
// 安全验证函数在所有场景下都可用,不仅限于 L2 Redis
pub mod security;

// ============================================================================
// Public API Re-exports
// ============================================================================

// Re-export macros when the feature is enabled
#[cfg(feature = "macros")]
pub use oxcache_macros::cached;

#[cfg(feature = "macros")]
pub mod macros {
    pub use oxcache_macros::*;
}

pub use client::{CacheExt, CacheOps};
pub use config::legacy_config::{
    CacheStrategy as LegacyCacheStrategy, DynamicConfig as LegacyDynamicConfig,
};
#[allow(deprecated)]
pub use config::Config;
pub use config::{
    CacheStrategy, CacheType, DynamicConfig, GlobalConfig, OxcacheConfig, OxcacheConfigBuilder,
    RedisMode, SerializationType, ServiceConfig,
};

#[cfg(feature = "confers")]
pub use config::ConfigSource;

#[cfg(feature = "l1-moka")]
pub use config::LayerConfig;
// Use LegacyEvictionPolicy from legacy_config to avoid type conflict
pub use config::LegacyEvictionPolicy as EvictionPolicy;
pub use error::{CacheError, Result};

// ============================================================================
// New API (Recommended)
// ============================================================================

// New API exports
pub use builder::{BackendBuilder, CacheBuilder, TieredCacheBuilder};
pub use cache::Cache;
pub use traits::{CacheKey, Cacheable};

// Custom tiered backend configuration exports
#[cfg(any(
    feature = "l1-moka",
    feature = "l2-redis",
    feature = "core",
    feature = "full"
))]
pub use backend::custom_tiered::{
    AutoFixConfig, BackendType, ConfigFix, ConfigValidationResult, CustomTieredConfig,
    CustomTieredConfigBuilder, FixedConfigResult, Layer, LayerBackendConfig, LayerRestriction,
};

#[cfg(any(feature = "l2-redis", feature = "core", feature = "full"))]
pub use sync::warmup::{WarmupManager, WarmupResult, WarmupStatus};

pub use config::oxcache_config;

#[cfg(test)]
pub use config::L1Config;
#[cfg(any(feature = "l2-redis", feature = "full"))]
pub use config::L2Config;
#[cfg(test)]
pub use config::TwoLevelConfig;

#[cfg(any(feature = "full", feature = "minimal", feature = "core"))]
pub use utils::key_generator::KeyGenerator;

// Smart Strategy exports
#[cfg(any(feature = "smart-strategy", feature = "full"))]
pub use smart_strategy::{
    CompressibilityChecker, CompressionDecider, HitRateCollector, HitRateStats, PrefetchDecider,
    SmartStrategyConfig, SmartStrategyManager,
};

// Enhanced Stats exports
#[cfg(any(feature = "enhanced-stats", feature = "metrics", feature = "full"))]
pub use metrics::{export_json_format, export_prometheus_format, get_enhanced_stats, CacheStats};

// HTTP Cache exports
#[cfg(any(feature = "http-cache", feature = "full"))]
pub use http::{
    CacheMiddlewareConfig, CacheMiddlewareState, HttpCacheAdapter, HttpCacheKeyGenerator,
    HttpCachePolicy, HttpCacheResponse, HttpRequest,
};

// Internal module exports (for #[cached] macro)
#[doc(hidden)]
pub use internal::{__internal_get_cache, __internal_register_cache};

// Feature info exports
pub use internal::{
    get_all_feature_info, get_l1_feature_info, get_l2_feature_info, is_l1_enabled, is_l2_enabled,
};

// ============================================================================
// Configuration Macros (Feature-Gated)
// ============================================================================

/// oxcache 版本号
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

// ============================================================================
// Compile-time Feature Validation
// ============================================================================
// 编译时特性验证
//
// 这些静态断言确保功能依赖在编译时就被正确配置。
// 如果启用了不兼容的特性组合,编译将会失败并显示清晰的错误消息。
//
// # 示例
//
// ```toml,ignore
// [dependencies]
// oxcache = { version = "0.1", features = ["bloom-filter"] }
// ```
//
// 上面的配置会导致编译错误,因为 `bloom-filter` 需要 `l1-moka` 特性。
// 使用 `full` 特性可以启用所有功能:
//
// ```toml,ignore
// [dependencies]
// oxcache = { version = "0.1", features = ["full"] }
// ```

const _: fn() = || {
    // 使用统一的宏检查特性依赖
    check_feature_dependence!("bloom-filter", cfg!(feature = "l1-moka"));
    check_feature_dependence!("rate-limiting", cfg!(feature = "l1-moka"));
    check_feature_dependence!("wal-recovery", cfg!(feature = "l2-redis"));
    check_feature_dependence!("batch-write", cfg!(feature = "l2-redis"));
    check_feature_dependence!("cli", cfg!(feature = "confers"));
    check_feature_dependence!("opentelemetry", cfg!(feature = "metrics"));
    check_feature_dependence!("database", cfg!(feature = "l2-redis"));
};