#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.
Features
- Thread-Safe: Uses
DashMapinternally for lock-free concurrent access. - Persistent Storage: Optionally persists cached entries to disk for durability across restarts.
- TTL Support: Automatically expires entries based on time-to-live (TTL) durations.
- Key-Level Locking: Provides fine-grained per-key locks for atomic operations.
- Efficient Serialization: Uses
bincodewith big-endian and variable integer encoding for compact, fast binary serialization. - Background Cleanup: Runs a background thread to remove expired entries from memory and disk transparently.
- Simple API: Clean, intuitive methods for set/get/remove operations with expiration control.
Installation
Add cache_ro to your Cargo.toml:
[]
= "0.2"
Quick Start
use ;
use Duration;
Configuration
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" |
API Highlights
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.
Internal Mechanics
- Keys are hashed with SHA-256 and stored in files named with a hash prefix to distribute entries.
- Each cache operation acquires per-key locks for consistency.
- A background thread handles automatic expiration cleanup based on TTL.
- Serialization uses
bincodewith big-endian and variable-length integer encoding for speed and compactness.
Contributing
Contributions are welcome! Please open an issue or submit a PR for:
- New features
- Performance improvements
- Bug fixes
License
Dual-licensed under MIT or Apache 2.0 at your option.