| Crates.io | rustycache |
| lib.rs | rustycache |
| version | 1.0.0 |
| created_at | 2025-05-17 16:11:45.15773+00 |
| updated_at | 2025-05-17 16:11:45.15773+00 |
| description | A simple and easy-to-use caching library for Rust. |
| homepage | https://github.com/Q300Z/rustycache |
| repository | https://github.com/Q300Z/rustycache |
| max_upload_size | |
| id | 1677971 |
| size | 49,716 |
A generic, thread-safe, asynchronous cache library in Rust implementing multiple cache eviction strategies with TTL and background cleaning.
Arc<Mutex<...>>CacheStrategy interface for easy extensionAdd this crate to your Cargo.toml:
rustycache = { path = "path/to/rustycache" }
Or if published on crates.io, replace with version:
rustycache = "1.0"
use easycache::LFUCache;
use std::time::Duration;
#[tokio::main]
async fn main() {
// Create LFU cache with capacity 100, TTL 60 seconds, cleaner interval 10 seconds
let mut cache = Easycache::new(100, Duration::from_secs(60), Duration::from_secs(10), easycache::strategy::StrategyType::LFU);
// Put some values
cache.put("key1".to_string(), "value1".to_string());
cache.put("key2".to_string(), "value2".to_string());
// Get a value
if let Some(val) = cache.get(&"key1".to_string()) {
println!("Got: {}", val);
} else {
println!("Key expired or not found");
}
}
use easycache::FIFOCache;
use std::time::Duration;
#[tokio::main]
async fn main() {
// Create FIFO cache with capacity 50, TTL 120 seconds, cleaner interval 15 seconds
let mut cache = Easycache::new(50, Duration::from_secs(120), Duration::from_secs(15), easycache::strategy::StrategyType::FIFO);
// Put some values
cache.put("foo".to_string(), 123);
cache.put("bar".to_string(), 456);
// Get a value
if let Some(val) = cache.get(&"foo".to_string()) {
println!("Got: {}", val);
} else {
println!("Key expired or not found");
}
}
To run the tests, use the following command:
cargo test
Tests cover cache insertion, eviction, TTL expiration, concurrency safety, and cleanup logic.
This project is licensed under the MIT License. See the LICENSE file for details.