Expand description
Cache configuration structures.
Provides configuration structures for all cache algorithm implementations. 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:
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_000entries- 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:
capacity = target_cache_size / average_object_sizeExample: 1GB disk cache with 50KB average objects:
max_size = 1024 * 1024 * 1024(1GB)capacity = 1GB / 50KB ≈ 20_000entries
§Single-Threaded Cache Configs
| Config | Cache | Description |
|---|---|---|
LruCacheConfig | LruCache | Least Recently Used |
LfuCacheConfig | LfuCache | Least Frequently Used |
LfudaCacheConfig | LfudaCache | LFU with Dynamic Aging |
SlruCacheConfig | SlruCache | Segmented LRU |
GdsfCacheConfig | 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);Re-exports§
pub use gdsf::GdsfCacheConfig;pub use lfu::LfuCacheConfig;pub use lfuda::LfudaCacheConfig;pub use lru::LruCacheConfig;pub use slru::SlruCacheConfig;
Modules§
- gdsf
- Configuration for the Greedy Dual-Size Frequency (GDSF) cache.
- lfu
- Configuration for the Least Frequently Used (LFU) cache.
- lfuda
- Configuration for the Least Frequently Used with Dynamic Aging (LFUDA) cache.
- lru
- Configuration for the Least Recently Used (LRU) cache.
- slru
- Configuration for the Segmented Least Recently Used (SLRU) cache.
Structs§
- Concurrent
Cache Config - Generic configuration wrapper for concurrent caches.
Type Aliases§
- Concurrent
Gdsf Cache Config - Configuration for a concurrent GDSF cache.
Type alias for
ConcurrentCacheConfig<GdsfCacheConfig>. - Concurrent
LfuCache Config - Configuration for a concurrent LFU cache.
Type alias for
ConcurrentCacheConfig<LfuCacheConfig>. - Concurrent
Lfuda Cache Config - Configuration for a concurrent LFUDA cache.
Type alias for
ConcurrentCacheConfig<LfudaCacheConfig>. - Concurrent
LruCache Config - Configuration for a concurrent LRU cache.
Type alias for
ConcurrentCacheConfig<LruCacheConfig>. - Concurrent
Slru Cache Config - Configuration for a concurrent SLRU cache.
Type alias for
ConcurrentCacheConfig<SlruCacheConfig>.