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
//! Cache Configuration Module
//!
//! This module provides configuration structures for all cache algorithm implementations.
//! Each cache type has its own dedicated configuration struct with public fields.
//!
//! # Design Philosophy
//!
//! Configuration structs have all public fields for simple instantiation:
//!
//! - **Simple**: Just create the struct with all fields set
//! - **Type safety**: All parameters must be provided at construction
//! - **No boilerplate**: No constructors or builder methods needed
//!
//! # Sizing Guidelines
//!
//! ## Understanding `max_size` and `capacity`
//!
//! All cache configurations include two key sizing parameters:
//!
//! - **`max_size`**: Maximum total size in bytes for cached *values*. Set this to your
//! memory/disk budget for the actual cache data.
//! - **`capacity`**: Maximum number of entries. Each entry incurs memory overhead
//! (~64-128 bytes) beyond the value size for keys, pointers, and metadata.
//!
//! ## For In-Memory Caches
//!
//! Set `max_size` based on how much memory you want to allocate for cached values:
//!
//! ```text
//! Total Memory ≈ max_size + (capacity × overhead_per_entry)
//! overhead_per_entry ≈ 64-128 bytes (keys, pointers, metadata)
//! ```
//!
//! **Example**: 100MB cache with ~10KB average values:
//! - `max_size = 100 * 1024 * 1024` (100MB for values)
//! - `capacity = 10_000` entries
//! - Overhead ≈ 10,000 × 100 bytes = ~1MB additional
//!
//! ## For Disk-Based or External Caches
//!
//! When caching references to external storage, size based on your target cache size:
//!
//! ```text
//! capacity = target_cache_size / average_object_size
//! ```
//!
//! **Example**: 1GB disk cache with 50KB average objects:
//! - `max_size = 1024 * 1024 * 1024` (1GB)
//! - `capacity = 1GB / 50KB ≈ 20_000` entries
//!
//! # Single-Threaded Cache Configs
//!
//! | Config | Cache | Description |
//! |--------|-------|-------------|
//! | `LruCacheConfig` | [`LruCache`](crate::LruCache) | Least Recently Used |
//! | `LfuCacheConfig` | [`LfuCache`](crate::LfuCache) | Least Frequently Used |
//! | `LfudaCacheConfig` | [`LfudaCache`](crate::LfudaCache) | LFU with Dynamic Aging |
//! | `SlruCacheConfig` | [`SlruCache`](crate::SlruCache) | Segmented LRU |
//! | `GdsfCacheConfig` | [`GdsfCache`](crate::GdsfCache) | Greedy Dual-Size Frequency |
//!
//! # Concurrent Cache Configs (requires `concurrent` feature)
//!
//! Use `ConcurrentCacheConfig<C>` wrapper around any base config:
//!
//! | Type Alias | Base Config | Description |
//! |------------|-------------|-------------|
//! | `ConcurrentLruCacheConfig` | `LruCacheConfig` | Thread-safe LRU |
//! | `ConcurrentLfuCacheConfig` | `LfuCacheConfig` | Thread-safe LFU |
//! | `ConcurrentLfudaCacheConfig` | `LfudaCacheConfig` | Thread-safe LFUDA |
//! | `ConcurrentSlruCacheConfig` | `SlruCacheConfig` | Thread-safe SLRU |
//! | `ConcurrentGdsfCacheConfig` | `GdsfCacheConfig` | Thread-safe GDSF |
//!
//! # Examples
//!
//! ```
//! use cache_rs::config::LruCacheConfig;
//! use cache_rs::LruCache;
//! use core::num::NonZeroUsize;
//!
//! // 10MB in-memory cache for ~1KB average values
//! let config = LruCacheConfig {
//! capacity: NonZeroUsize::new(10_000).unwrap(),
//! max_size: 10 * 1024 * 1024, // 10MB
//! };
//!
//! // Create cache from config
//! let cache: LruCache<String, Vec<u8>> = LruCache::init(config, None);
//! ```
// Single-threaded cache configs
// Re-exports for convenience - single-threaded
pub use GdsfCacheConfig;
pub use LfuCacheConfig;
pub use LfudaCacheConfig;
pub use LruCacheConfig;
pub use SlruCacheConfig;
/// Generic configuration wrapper for concurrent caches.
///
/// Wraps any base cache configuration and adds the `segments` field
/// for controlling the number of independent segments used for sharding.
///
/// # Type Parameter
///
/// - `C`: The base cache configuration type (e.g., `LruCacheConfig`, `LfuCacheConfig`)
///
/// # Fields
///
/// - `base`: The underlying single-threaded cache configuration. See the base
/// config docs for sizing guidance on `capacity` and `max_size`.
/// - `segments`: Number of independent segments for sharding (more = less contention)
///
/// # Sizing Note
///
/// The `capacity` and `max_size` in the base config apply to the **entire cache**
/// (distributed across all segments), not per-segment.
///
/// # Example
///
/// ```ignore
/// use cache_rs::config::{ConcurrentCacheConfig, LruCacheConfig, ConcurrentLruCacheConfig};
/// use core::num::NonZeroUsize;
///
/// // 100MB concurrent cache with 16 segments
/// let config: ConcurrentLruCacheConfig = ConcurrentCacheConfig {
/// base: LruCacheConfig {
/// capacity: NonZeroUsize::new(10_000).unwrap(),
/// max_size: 100 * 1024 * 1024, // 100MB total
/// },
/// segments: 16,
/// };
/// ```
// Type aliases for concurrent cache configs
/// Configuration for a concurrent LRU cache.
/// Type alias for `ConcurrentCacheConfig<LruCacheConfig>`.
pub type ConcurrentLruCacheConfig = ;
/// Configuration for a concurrent LFU cache.
/// Type alias for `ConcurrentCacheConfig<LfuCacheConfig>`.
pub type ConcurrentLfuCacheConfig = ;
/// Configuration for a concurrent LFUDA cache.
/// Type alias for `ConcurrentCacheConfig<LfudaCacheConfig>`.
pub type ConcurrentLfudaCacheConfig = ;
/// Configuration for a concurrent SLRU cache.
/// Type alias for `ConcurrentCacheConfig<SlruCacheConfig>`.
pub type ConcurrentSlruCacheConfig = ;
/// Configuration for a concurrent GDSF cache.
/// Type alias for `ConcurrentCacheConfig<GdsfCacheConfig>`.
pub type ConcurrentGdsfCacheConfig = ;