ai_lib_rust/cache/mod.rs
1//! 响应缓存模块:提供可插拔的缓存后端以减少重复 API 调用。
2//!
3//! # Response Caching Module
4//!
5//! This module provides flexible response caching capabilities with pluggable
6//! backends, reducing API calls and improving response times for repeated requests.
7//!
8//! ## Overview
9//!
10//! Caching is valuable for:
11//! - Reducing API costs by avoiding duplicate requests
12//! - Improving response latency for repeated queries
13//! - Enabling offline or degraded mode operation
14//! - Supporting development and testing workflows
15//!
16//! ## Key Components
17//!
18//! | Component | Description |
19//! |-----------|-------------|
20//! | [`CacheManager`] | High-level cache management with TTL and statistics |
21//! | [`CacheConfig`] | Configuration for cache behavior and limits |
22//! | [`CacheBackend`] | Trait for implementing custom cache backends |
23//! | [`MemoryCache`] | In-memory LRU cache implementation |
24//! | [`NullCache`] | No-op cache for disabling caching |
25//! | [`CacheKey`] | Cache key generation from request parameters |
26//!
27//! ## Example
28//!
29//! ```rust
30//! use ai_lib_rust::cache::{CacheManager, CacheConfig, MemoryCache};
31//! use std::time::Duration;
32//!
33//! // Create an in-memory cache with 1-hour TTL
34//! let backend = MemoryCache::new(1000); // max 1000 entries
35//! let config = CacheConfig {
36//! ttl: Duration::from_secs(3600),
37//! enabled: true,
38//! ..Default::default()
39//! };
40//! let cache = CacheManager::new(Box::new(backend), config);
41//! ```
42//!
43//! ## Cache Key Generation
44//!
45//! Cache keys are generated from:
46//! - Model identifier
47//! - Message content (hashed)
48//! - Request parameters (temperature, max_tokens, etc.)
49//!
50//! This ensures identical requests return cached responses while different
51//! parameters generate new cache entries.
52
53mod backend;
54mod key;
55mod manager;
56
57pub use backend::{CacheBackend, MemoryCache, NullCache};
58pub use key::{CacheKey, CacheKeyGenerator};
59pub use manager::{CacheConfig, CacheManager, CacheStats};