Module memory

Module memory 

Source
Expand description

In-memory LRU cache implementation

This module provides a high-performance in-memory cache with:

  • LRU eviction using randomized sampling
  • TTL-based expiration
  • Thread-safe concurrent access via RwLock
  • Configurable capacity and max item size

§Eviction Strategy

The cache uses a probabilistic LRU approximation:

  • When eviction is needed, randomly sample 5 entries
  • Evict the oldest one (by last_accessed_at)
  • If an expired entry is found during sampling, evict it immediately

This approach is O(5) = O(1) instead of O(n) for scanning all entries, while still evicting items in the oldest ~20% on average.

§Performance Characteristics

  • Get: O(1) hash lookup + O(1) expiry check
  • Set: O(1) hash insertion + O(5) eviction sampling if needed
  • Thread-safe: Uses parking_lot::RwLock for better performance than std::Mutex
  • Zero-copy when possible: Returns references where applicable

§Example

use warpdrive::cache::memory::MemoryCache;
use warpdrive::cache::Cache;

// 32 MB cache, 1 MB max item size
let cache = MemoryCache::new(32 * 1024 * 1024, 1024 * 1024);

// Store data with 60 second TTL
cache.set("user:123", b"user_data", 60).await?;

// Retrieve data
if let Some(data) = cache.get("user:123").await? {
    println!("Found {} bytes", data.len());
}

Structs§

MemoryCache
In-memory LRU cache with TTL support