| Crates.io | cache-ro |
| lib.rs | cache-ro |
| version | 0.3.1 |
| created_at | 2025-05-21 10:11:03.940433+00 |
| updated_at | 2025-08-14 05:25:24.792705+00 |
| description | A high-performance, thread-safe persistent cache with TTL support |
| homepage | |
| repository | https://github.com/kak-smko/cache-ro |
| max_upload_size | |
| id | 1683203 |
| size | 61,979 |
#cache_ro — Persistent, High-Performance, Thread-Safe Cache for Rust
cache_ro is a blazing fast, thread-safe Rust cache library designed for high concurrency and optional persistence. It supports automatic TTL expiration, key-level locking, and efficient binary serialization — making it perfect for applications needing reliable caching with disk-backed durability.
DashMap internally for lock-free concurrent access.bincode with big-endian and variable integer encoding for compact, fast binary serialization.Add cache_ro to your Cargo.toml:
[dependencies]
cache_ro = "0.2"
use cache_ro::{Cache, CacheConfig};
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a cache with default config (persistent, 2-char hash prefix)
let cache = Cache::new(CacheConfig::default())?;
// Store a key with a TTL of 60 seconds
cache.set("username", "alice".to_string(), Duration::from_secs(60))?;
// Retrieve and deserialize cached value
if let Some(username) = cache.get::<String>("username") {
println!("Cached username: {}", username);
} else {
println!("Cache miss or expired");
}
// This is matter for close graceful
Cache::drop();
Ok(())
}
Customize cache behavior with CacheConfig:
| Field | Description | Default |
|---|---|---|
persistent |
Enable or disable disk persistence | true |
hash_prefix_length |
Number of hash bytes for filename prefix | 2 |
cleanup_interval |
Cleanup the cache for expire keys | 10s |
dir_path |
Directory path to store cache files | "cache_data" |
Cache::set(key, value, ttl)Stores a serializable value under key with expiration after ttl.
Cache::get::<T>(key) -> Option<T>Retrieves and deserializes a value of type T if present and unexpired.
Cache::remove(key)Removes a key and its persisted data.
Cache::clear()Clears all in-memory and persistent cache entries.
bincode with big-endian and variable-length integer encoding for speed and compactness.Contributions are welcome! Please open an issue or submit a PR for:
New features
Performance improvements
Bug fixes
Dual-licensed under MIT or Apache 2.0 at your option.