Expand description
§In-Memory Cache
A fast, thread-safe, in-memory cache library for Rust with TTL support and LRU eviction.
§Features
- Thread-safe: Share across threads with
Clone(usesArcinternally) - TTL support: Entries can expire after a configurable duration
- LRU eviction: Automatic eviction of least-recently-used entries when at capacity
- Statistics: Track cache hits, misses, evictions, and more
- Zero unsafe code: Built entirely with safe Rust
§Quick Start
use in_memory_cache::{Cache, CacheConfig};
use std::time::Duration;
// Create a cache with configuration
let config = CacheConfig::new()
.max_capacity(10_000)
.default_ttl(Duration::from_secs(300))
.build();
let cache = Cache::new(config);
// Store and retrieve values
cache.set("user:123", "Alice");
if let Some(value) = cache.get("user:123") {
println!("Found: {:?}", value);
}
// Set with custom TTL
cache.set_with_ttl("session:abc", "session_data", Duration::from_secs(60));
// Check statistics
let stats = cache.stats();
println!("Hit rate: {:.1}%", stats.hit_rate);§Thread Safety
The cache is safe to share across threads. Cloning a Cache creates a new
handle to the same underlying data:
use in_memory_cache::Cache;
use std::thread;
let cache = Cache::default();
let handles: Vec<_> = (0..4).map(|i| {
let cache = cache.clone();
thread::spawn(move || {
cache.set(format!("key_{}", i), format!("value_{}", i));
})
}).collect();
for handle in handles {
handle.join().unwrap();
}Re-exports§
pub use cache::Cache;pub use config::CacheConfig;pub use error::CacheError;pub use error::CacheResult;pub use stats::CacheStats;pub use stats::StatsSnapshot;pub use utils::buffer_to_array;pub use command::Command;pub use cli::Cli;pub use cli::ClientCommand;
Modules§
- cache
- The main cache interface.
- cli
- Command-line interface definitions.
- command
- Command types for the cache protocol.
- config
- Configuration for the in-memory cache.
- error
- Error types for the in-memory cache library.
- stats
- Statistics and metrics for the cache.
- utils
- Utility functions for buffer parsing and manipulation.