unistore_cache/
lib.rs

1//! # unistore-cache
2//!
3//! 内存缓存能力 - UniStore 能力生态的一部分。
4//!
5//! ## 功能特性
6//!
7//! - **LRU 淘汰**: 基于最近最少使用策略自动淘汰
8//! - **TTL 支持**: 可设置条目过期时间
9//! - **线程安全**: 使用 parking_lot 高性能锁
10//! - **统计信息**: 命中率、大小等缓存统计
11//! - **泛型支持**: 支持任意 Key-Value 类型
12//!
13//! ## 快速开始
14//!
15//! ```rust
16//! use unistore_cache::{Cache, CacheConfig};
17//! use std::time::Duration;
18//!
19//! // 创建缓存
20//! let cache: Cache<String, String> = Cache::new(CacheConfig::default()
21//!     .max_capacity(1000)
22//!     .default_ttl(Duration::from_secs(300)));
23//!
24//! // 存取数据
25//! cache.insert("key1".to_string(), "value1".to_string());
26//! let value = cache.get(&"key1".to_string());
27//! assert_eq!(value, Some("value1".to_string()));
28//! ```
29//!
30//! ## 自定义 TTL
31//!
32//! ```rust
33//! use unistore_cache::{Cache, CacheConfig};
34//! use std::time::Duration;
35//!
36//! let cache: Cache<&str, i32> = Cache::with_defaults();
37//!
38//! // 使用默认 TTL
39//! cache.insert("key1", 100);
40//!
41//! // 使用自定义 TTL
42//! cache.insert_with_ttl("key2", 200, Some(Duration::from_secs(60)));
43//!
44//! // 永不过期
45//! cache.insert_with_ttl("key3", 300, None);
46//! ```
47//!
48//! ## Get or Insert
49//!
50//! ```rust
51//! use unistore_cache::Cache;
52//!
53//! let cache: Cache<&str, String> = Cache::with_defaults();
54//!
55//! // 如果不存在则计算并插入
56//! let value = cache.get_or_insert_with("expensive_key", || {
57//!     // 昂贵的计算...
58//!     "computed_value".to_string()
59//! });
60//! ```
61//!
62//! ## 统计信息
63//!
64//! ```rust
65//! use unistore_cache::Cache;
66//!
67//! let cache: Cache<&str, i32> = Cache::with_defaults();
68//! cache.insert("key1", 42);
69//! cache.get(&"key1");
70//! cache.get(&"key2"); // miss
71//!
72//! let stats = cache.stats();
73//! println!("命中率: {:.2}%", stats.hit_rate() * 100.0);
74//! ```
75
76// === 内部模块 ===
77mod cache;
78mod config;
79mod deps;
80mod entry;
81mod error;
82mod stats;
83
84// === 对外接口(SDK)===
85pub use cache::Cache;
86pub use config::CacheConfig;
87pub use entry::CacheEntry;
88pub use error::CacheError;
89pub use stats::{CacheStats, CacheStatsSnapshot};