Skip to main content

Module lfuda

Module lfuda 

Source
Expand description

Configuration for the Least Frequently Used with Dynamic Aging (LFUDA) cache.

This module provides configuration for LFUDA caches.

§Sizing Guidelines

§Understanding max_size and capacity

  • max_size: The maximum total size in bytes for cached values. This should reflect your memory budget for the cache data itself.
  • capacity: The maximum number of entries. Each entry has memory overhead beyond the value size (approximately 64-128 bytes per entry for keys, pointers, and metadata).

§For In-Memory Caches

Set max_size to the amount of 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: For a 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: For a 1GB disk cache with 50KB average objects:

  • max_size = 1024 * 1024 * 1024 (1GB)
  • capacity = 1GB / 50KB ≈ 20,000 entries

§Examples

use cache_rs::config::LfudaCacheConfig;
use cache_rs::LfudaCache;
use core::num::NonZeroUsize;

// In-memory cache: 50MB budget for values, ~5KB average size
let config = LfudaCacheConfig {
    capacity: NonZeroUsize::new(10_000).unwrap(),
    initial_age: 0,
    max_size: 50 * 1024 * 1024,  // 50MB
};
let cache: LfudaCache<String, Vec<u8>> = LfudaCache::init(config, None);

// Small fixed-size value cache with initial age
let config = LfudaCacheConfig {
    capacity: NonZeroUsize::new(1000).unwrap(),
    initial_age: 100,
    max_size: 1024 * 1024,  // 1MB is plenty for small values
};
let cache: LfudaCache<String, i32> = LfudaCache::init(config, None);

Structs§

LfudaCacheConfig
Configuration for an LFUDA (Least Frequently Used with Dynamic Aging) cache.