Skip to main content

Module config

Module config 

Source
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_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:

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

ConfigCacheDescription
LruCacheConfigLruCacheLeast Recently Used
LfuCacheConfigLfuCacheLeast Frequently Used
LfudaCacheConfigLfudaCacheLFU with Dynamic Aging
SlruCacheConfigSlruCacheSegmented LRU
GdsfCacheConfigGdsfCacheGreedy Dual-Size Frequency

§Concurrent Cache Configs (requires concurrent feature)

Use ConcurrentCacheConfig<C> wrapper around any base config:

Type AliasBase ConfigDescription
ConcurrentLruCacheConfigLruCacheConfigThread-safe LRU
ConcurrentLfuCacheConfigLfuCacheConfigThread-safe LFU
ConcurrentLfudaCacheConfigLfudaCacheConfigThread-safe LFUDA
ConcurrentSlruCacheConfigSlruCacheConfigThread-safe SLRU
ConcurrentGdsfCacheConfigGdsfCacheConfigThread-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§

ConcurrentCacheConfig
Generic configuration wrapper for concurrent caches.

Type Aliases§

ConcurrentGdsfCacheConfig
Configuration for a concurrent GDSF cache. Type alias for ConcurrentCacheConfig<GdsfCacheConfig>.
ConcurrentLfuCacheConfig
Configuration for a concurrent LFU cache. Type alias for ConcurrentCacheConfig<LfuCacheConfig>.
ConcurrentLfudaCacheConfig
Configuration for a concurrent LFUDA cache. Type alias for ConcurrentCacheConfig<LfudaCacheConfig>.
ConcurrentLruCacheConfig
Configuration for a concurrent LRU cache. Type alias for ConcurrentCacheConfig<LruCacheConfig>.
ConcurrentSlruCacheConfig
Configuration for a concurrent SLRU cache. Type alias for ConcurrentCacheConfig<SlruCacheConfig>.