| Crates.io | simple-cacher |
| lib.rs | simple-cacher |
| version | 0.1.1 |
| created_at | 2025-07-17 22:59:38.916597+00 |
| updated_at | 2025-07-18 00:02:38.645957+00 |
| description | A high-performance, flexible caching library with custom matching capabilities and automatic expiration |
| homepage | https://github.com/prizzledev/simple-cacher |
| repository | https://github.com/prizzledev/simple-cacher |
| max_upload_size | |
| id | 1758292 |
| size | 76,650 |
A high-performance, flexible caching library for Rust with custom matching capabilities and automatic expiration.
Matcher<T> traitAdd this to your Cargo.toml:
[dependencies]
simple_cacher = "0.1.0"
# Optional: Enable regex support
simple_cacher = { version = "0.1.0", features = ["regex_support"] }
use simple_cacher::*;
use std::time::Duration;
// Create a cache with 5-minute TTL
let mut cache = SimpleCacher::new(Duration::from_secs(300));
// Insert data
cache.insert("user:123".to_string(), "Alice".to_string());
// Retrieve data
match cache.get(&"user:123".to_string()) {
Ok(entry) => println!("Found: {}", entry.value()),
Err(SimpleCacheError::NotFound) => println!("Not found"),
Err(SimpleCacheError::Expired) => println!("Expired"),
}
// Check if expired
if let Ok(entry) = cache.get(&"user:123".to_string()) {
println!("Entry age: {:?}", entry.age());
println!("Is expired: {}", entry.is_expired());
}
The library provides powerful pattern matching capabilities:
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_secs(300));
// Insert data
cache.insert("user:alice".to_string(), "Alice Johnson".to_string());
cache.insert("user:bob".to_string(), "Bob Smith".to_string());
cache.insert("admin:charlie".to_string(), "Charlie Admin".to_string());
// Find by prefix
let user_matcher = PrefixMatcher::new("user:");
let users = cache.get_all_by_matcher(&user_matcher);
println!("Found {} users", users.len());
// Find first match
if let Ok(user) = cache.get_by_matcher(&user_matcher) {
println!("First user: {}", user.value());
}
PrefixMatcher - Match strings by prefixSuffixMatcher - Match strings by suffixContainsMatcher - Match strings containing substringRangeMatcher<T> - Match numeric values in rangeFnMatcher<T, F> - Custom function-based matchingExactMatcher<T> - Exact matching (useful in generic code)Implement the Matcher<T> trait for domain-specific matching:
struct DomainMatcher {
domain: String,
}
impl Matcher<String> for DomainMatcher {
fn matches(&self, email: &String) -> bool {
email.ends_with(&format!("@{}", self.domain))
}
}
// Usage
let company_matcher = DomainMatcher { domain: "company.com".to_string() };
let company_emails = cache.get_all_by_matcher(&company_matcher);
use simple_cacher::*;
use std::time::Duration;
// Cache with max 1000 entries, oldest removed first
let mut cache = SimpleCacher::with_max_size(
Duration::from_secs(300),
1000
);
// Fill beyond capacity - oldest entries automatically removed
for i in 0..1500 {
cache.insert(format!("key_{}", i), format!("value_{}", i));
}
assert_eq!(cache.len(), 1000); // Only newest 1000 entries remain
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_secs(300)); // Default TTL
// Insert with custom TTL
cache.insert_with_ttl(
"short_lived".to_string(),
"data".to_string(),
Duration::from_secs(60) // 1 minute TTL
);
use simple_cacher::*;
match cache.get(&"some_key".to_string()) {
Ok(entry) => {
// Cache hit
println!("Value: {}", entry.value());
println!("Age: {:?}", entry.age());
}
Err(SimpleCacheError::NotFound) => {
// Key doesn't exist
println!("Cache miss");
}
Err(SimpleCacheError::Expired) => {
// Entry existed but expired (automatically removed)
println!("Entry expired");
}
}
use simple_cacher::*;
let mut cache = SimpleCacher::new(Duration::from_secs(300));
// Manual cleanup
let removed = cache.cleanup_expired();
println!("Removed {} expired entries", removed);
// Cache statistics
let stats = cache.stats();
println!("Total: {}, Active: {}, Expired: {}",
stats.total_entries, stats.active_entries, stats.expired_entries);
// Iterate over active entries only
for (key, entry) in cache.iter_active() {
println!("{}: {} (age: {:?})", key, entry.value(), entry.age());
}
// Clear all entries
cache.clear();
The library includes several examples demonstrating different use cases:
basic_usage.rs - Basic cache operations and expirationregex_matching.rs - Advanced pattern matching with regex (requires regex_support feature)file_cache.rs - File content caching with directory matchingRun examples with:
# Basic usage
cargo run --example basic_usage
# Regex matching (requires regex_support feature)
cargo run --example regex_matching --features regex_support
# File caching
cargo run --example file_cache
Enable regex-based matching by adding the regex_support feature:
[dependencies]
simple_cacher = { version = "0.1.0", features = ["regex_support"] }
This provides RegexMatcher for complex pattern matching.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Debug & Clone for SimpleCacher & SimpleCacheObject