Skip to main content

Module entry

Module entry 

Source
Expand description

Unified cache entry type.

Provides a generic CacheEntry<K, V, M> structure that holds key, value, timestamps, and algorithm-specific metadata. This is the foundation for the dual-limit capacity management system. Unified Cache Entry Type

This module provides a unified CacheEntry<K, V, M> structure that can be used across all cache algorithm implementations. The generic M parameter allows each algorithm to store its own metadata without affecting the core entry structure.

§Design Philosophy

The unified entry design provides several benefits:

  • Consistency: All cache algorithms use the same core entry structure
  • Extensibility: Algorithm-specific metadata via the M generic parameter
  • Timestamps: Built-in creation and last-accessed timestamps for TTL and monitoring
  • Size-awareness: Explicit size field for dual-limit capacity management

§Memory Layout

Each entry has the following overhead:

  • key: K - User’s key type
  • value: V - User’s value type
  • size: u64 - 8 bytes (content size tracking)
  • last_accessed: u64 - 8 bytes (timestamps for monitoring)
  • create_time: u64 - 8 bytes (timestamps for TTL)
  • metadata: Option<M> - 0-24 bytes depending on algorithm

Base overhead (all algorithms): ~24 bytes + key + value + optional metadata

§Usage Examples

use cache_rs::entry::CacheEntry;

// Simple entry without algorithm-specific metadata (e.g., for LRU)
let entry: CacheEntry<String, Vec<u8>, ()> = CacheEntry::new(
    "image.png".to_string(),
    vec![0u8; 1024],
    1024, // size in bytes
);

// Entry with frequency metadata (e.g., for LFU)
use cache_rs::meta::LfuMeta;
let entry = CacheEntry::with_metadata(
    "key".to_string(),
    "value".to_string(),
    5, // size
    LfuMeta { frequency: 0 },
);

§Thread Safety

The single-threaded cache implementations use plain u64 for timestamps. For concurrent access, wrap the cache in appropriate synchronization primitives, or use the concurrent cache implementations which handle synchronization internally.

Structs§

CacheEntry
Unified cache entry holding key, value, timestamps, and algorithm-specific metadata.