Expand description
§MiniCache
A fast, lightweight, async-compatible in-memory cache for Rust with TTL (Time-To-Live) support and automatic cleanup. Perfect for async applications that need efficient caching without the complexity.
§Features
- 🔥 High Performance: Millions of operations per second
- ⚡ Async/Await Ready: Built for
tokioand async applications - ⏰ TTL Support: Automatic expiration with background cleanup
- 🔒 Thread-Safe: Concurrent access with
Arc+RwLock - 💾 Memory Efficient: Minimal overhead per cache entry
- 🛠 Easy to Use: Simple API with comprehensive examples
§Quick Start
use minicache::MiniCache;
use std::time::Duration;
#[tokio::main]
async fn main() {
// Create cache with 60-second cleanup interval
let cache = MiniCache::new(Duration::from_secs(60));
// Set a value (no expiration)
cache.set("user:123", "John Doe", None).await;
// Set a value with TTL
cache.set("session:abc", "temp_data", Some(Duration::from_secs(300))).await;
// Get values
if let Some(user) = cache.get(&"user:123").await {
println!("User: {}", user);
}
// Check if key exists
if cache.contains(&"session:abc").await {
println!("Session is active");
}
println!("Cache size: {}", cache.len().await);
}§TTL Usage
use minicache::MiniCache;
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() {
let cache = MiniCache::new(Duration::from_millis(100));
// Set with 200ms TTL
cache.set("temp", "expires soon", Some(Duration::from_millis(200))).await;
// Value exists immediately
assert_eq!(cache.get(&"temp").await, Some("expires soon"));
// Wait for expiration
sleep(Duration::from_millis(250)).await;
// Value has expired
assert_eq!(cache.get(&"temp").await, None);
}§Concurrent Usage
use minicache::MiniCache;
use std::sync::Arc;
use std::time::Duration;
#[tokio::main]
async fn main() {
let cache = Arc::new(MiniCache::new(Duration::from_secs(60)));
let mut handles = vec![];
// Spawn multiple tasks
for i in 0..10 {
let cache_clone = cache.clone();
let handle = tokio::spawn(async move {
cache_clone.set(format!("key_{}", i), format!("value_{}", i), None).await;
});
handles.push(handle);
}
// Wait for all tasks
for handle in handles {
handle.await.unwrap();
}
println!("Total entries: {}", cache.len().await);
}§Performance
Based on benchmarks (MacBook Pro M1):
- Basic Reads: ~13.7M operations/second
- Basic Writes: ~9.6M operations/second
- Concurrent Access: ~1.7M operations/second
- Memory Overhead: ~162 bytes per entry
Re-exports§
pub use core::MiniCache;
Modules§
- core
- Core implementation of MiniCache - an async-compatible in-memory cache with TTL support.