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
// Copyright (c) 2025-2026 Kirky.X
// SPDX-License-Identifier: MIT
//! oxcache - 高性能多层缓存库
//!
//! # Example
//!
//! ```rust,ignore
//! use oxcache::Cache;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct User { id: u64, name: String }
//!
//! #[tokio::main]
//! async fn main() -> OxCacheResult<(), Box<dyn std::error::Error>> {
//! let cache: Cache<String, User> = Cache::builder().build().await?;
//! cache.set(&"user:1".to_string(), &User { id: 1, name: "Alice".into() }).await?;
//! let user = cache.get(&"user:1".to_string()).await?;
//! Ok(())
//! }
//! ```
//!
//! # Tiered Cache
//!
//! ```rust,ignore
//! use oxcache::cache::{ChainCache, ChainLink};
//! use oxcache::backend::MokaMemoryBackend;
//!
//! let l1 = MokaMemoryBackend::builder().capacity(10000).build();
//! let l2 = oxcache::backend::RedisBackend::new("redis://localhost:6379").await?;
//!
//! let chain = ChainCache::builder()
//! .link(ChainLink::from_backend(l1))
//! .link(ChainLink::from_backend(l2))
//! .enable_backfill()
//! .build();
//! ```
//!
//! # Sync API (0.3.0)
//!
//! Enable `sync_mode(true)` on the builder to get synchronous methods
//! (`get_sync` / `set_sync` / `set_with_ttl_sync` / `delete_sync` /
//! `exists_sync` / `get_or_sync` / `clear_sync`) alongside the async API.
//! Requires `multi_thread` tokio runtime for Moka-backed caches.
//!
//! ```rust,ignore
//! # #[tokio::main(flavor = "multi_thread")]
//! # async fn main() -> OxCacheResult<(), Box<dyn std::error::Error>> {
//! let cache: Cache<String, String> = Cache::builder().sync_mode(true).build().await?;
//! cache.set_sync(&"k".to_string(), &"v".to_string())?;
//! let v = cache.get_sync(&"k".to_string())?;
//! # Ok(()) }
//! ```
//!
//! # Bloom Filter (0.3.0)
//!
//! Enable the `bloom-filter` feature (not in `full`) for negative-query
//! filtering. [`BloomFilterBackend`] wraps any [`CacheBackend`] and skips
//! the inner backend on BF miss.
//!
//! ```rust,ignore
//! use oxcache::backend::MokaMemoryBackend;
//! use oxcache::features::BloomFilterBackend;
//! let backend = BloomFilterBackend::new(MokaMemoryBackend::new());
//! ```
//!
//! # Universal per-entry TTL (0.3.0)
//!
//! All backends (Moka / DashMap / Redis / Mock / Chain / Bloom) honor
//! per-entry `set(key, value, Some(ttl))`. Moka uses the `moka::Expiry`
//! trait for real per-entry TTL (overriding the global TTL set on the
//! builder).
//!
//! # Features
//!
//! ## Tiered Feature Sets
//!
//! - `minimal`: L1 memory cache only (memory + tracing + metrics + serialization + chrono)
//! - `core`: L1 + L2 Redis (minimal + redis + futures)
//! - `full`: All features enabled (opt-in via features = ["full"])
//!
//! ## Core Component Features
//!
//! - `memory`: L1 memory cache (Moka + DashMap)
//! - `redis`: L2 distributed cache (Redis + regex)
//! - `macros`: Proc macros for `#[cached]`
//! - `serialization`: JSON serialization (serde + serde_json)
//! - `compression`: Flate2 compression
//! - `tracing`: Tracing support
//! - `metrics`: OpenTelemetry metrics & observability
//! - `batch-write`: Buffered L2 writes (tokio-util)
//! - `lua-script`: Lua script execution (requires redis)
//! - `cli`: CLI tools (clap)
//! - `testing`: Testing support (exposes internal functions)
//! - `bloom-filter`: Negative-query filtering (not in `full`)
//! - `i18n`: ICU4X-backed locale-aware formatting (not in `full`)
//! - `kit`: trait-kit AsyncKit integration (OxcacheModule) (not in `full`)
// Many constants/types in core::constants and core::command are reference
// data only consumed by specific sub-features (lua-script, cli, batch-write,
// etc.). Only `full` enables all sub-features, so we allow dead_code in any
// non-full feature combination rather than gating each constant individually.
// ============================================================================
// Feature Flags and Macros
// ============================================================================
/// 编译时特性依赖检查(支持 full 特性)
///
/// 注意:$required 应为特性名称字符串,而非 cfg 表达式。
///
/// # Example
///
/// ```rust,ignore
/// check_feature_dependence!("moka", "bloom-filter");
/// ```
///
/// 如果启用了 `bloom-filter` 但没有启用 `moka` 或 `full`,编译时会报错。
// ============================================================================
// Core Modules (Always Available)
// ============================================================================
// Internal module for #[cached] macro support
// Must be `pub` (not `pub(crate)`) so the #[cached] macro can access
// __internal_get_cache from external crates. #[doc(hidden)] keeps it out of public docs.
// 依赖 crate::Cache,须与 cache 模块门控一致
// ============================================================================
// Primary Modules (Feature-Gated)
// ============================================================================
// Cache module (modern Cache<K,V> API)
// Gated behind backend-enabling features because cache depends on backend + infra modules.
// memory-only is supported: serde is included in the memory feature for trait bounds,
// and serde_json usage is internally gated behind serialization/full.
// Backend module (L1/L2 cache implementation)
// Features module (optional capabilities)
// Infrastructure module (metrics, serialization, telemetry, etc.)
// Mock Module (For testing only)
// Registry module for #[cached] macro support
// 需要 backend (CacheBackend trait) 和 dashmap,仅在 memory 及其超集下可用
// Traits module: CacheKey
// Config module: confers-based configuration
// Utils module: key generation utilities
// Integrations module: optional adapters for external frameworks (trait-kit, etc.)
// Each integration is feature-gated and pulls no deps unless explicitly enabled.
// i18n module: ICU4X-backed locale-aware formatting for cache keys, statistics,
// expiry display, and collation. Optional, feature-gated via `i18n`.
// Security module: Redis security validation
pub
// ============================================================================
// Public API Re-exports
// ============================================================================
// Re-export macros when the feature is enabled
pub use cached;
pub use ;
pub use ;
// Re-export internal functions needed by #[cached] macro at crate root
// The macro generates code calling ::oxcache::__internal_get_cache()
// internal 模块依赖 cache::Cache,须与 cache 模块门控一致
pub use crate__internal_get_cache;
// ============================================================================
// New API (Recommended)
// ============================================================================
// New API exports
// cache 模块仅在 memory/redis/minimal/core/full feature 下编译,re-export 须同步门控
pub use Cache;
pub use CacheBuilder;
// Re-exports from infra module
pub use ;
// Re-exports from security module (new brick architecture)
pub use crate;
// Public API re-exports (after features re-exports)
// cache 模块 re-export 须与 cache 模块门控一致
pub use UnifiedCache;
pub use ;
pub use CacheKey;
// Type-safe enum exports
pub use ;
// Key generator export
pub use crateKeyGenerator;
// Events module re-export
pub use ;
// Backend exports
// backend 模块仅在 memory/redis/minimal/core/full feature 下编译,re-export 须同步门控
pub use ;
pub use ;
// ============================================================================
// Factory Functions (Brick Architecture Standard)
// ============================================================================
/// oxcache 版本号
pub const VERSION: &str = env!;