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
Mgeneric 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 consists of:
key: K- User’s key typevalue: V- User’s value typemetadata: CacheMetadata<M>- Contains size, timestamps, and algorithm-specific data
CacheMetadata<M> contains:
size: u64- 8 bytes (content size tracking)last_accessed: u64- 8 bytes (timestamps for monitoring)create_time: u64- 8 bytes (timestamps for TTL)algorithm: M- Algorithm-specific metadata (0-16 bytes depending on algorithm)
§Usage Examples
use cache_rs::entry::{CacheEntry, CacheMetadata};
// 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::lfu::LfuMeta;
let entry = CacheEntry::with_algorithm_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§
- Cache
Entry - Unified cache entry holding key, value, and metadata.
- Cache
Metadata - Metadata associated with a cache entry.