Skip to main content

SIZE_UNIT

Constant SIZE_UNIT 

Source
pub const SIZE_UNIT: u64 = 1;
Expand description

Size value for entry-count mode where actual size doesn’t matter.

Use this when you only want to limit the number of entries, not total size. Each entry will be counted as having size 1, making current_size() equal to len().

§Example

use cache_rs::{LruCache, SIZE_UNIT};
use cache_rs::config::LruCacheConfig;
use core::num::NonZeroUsize;

let config = LruCacheConfig {
    capacity: NonZeroUsize::new(100).unwrap(),
    max_size: u64::MAX,  // No size limit
};
let mut cache = LruCache::init(config, None);

// Using SIZE_UNIT for count-based caching
cache.put("key1", "value1", SIZE_UNIT);
cache.put("key2", "value2", SIZE_UNIT);
assert_eq!(cache.len(), 2);
assert_eq!(cache.current_size(), 2);  // Each entry counts as 1

§When to Use

  • Entry-count mode: When you only care about limiting the number of entries
  • Fixed-size objects: When all cached items are the same size
  • Simple caching: When size tracking isn’t important for your use case

§When NOT to Use

  • Size-constrained caches: When max_size is set to a real memory limit
  • Variable-sized objects: When objects have significantly different sizes
  • Size-aware algorithms: GDSF and LFUDA work better with actual sizes