v0.14.0
🎯 Conditional Caching with cache_if!
Version 0.14.0 introduces conditional caching, giving you fine-grained control over when results should be cached based on custom predicates:
New Features:
- 🎯 Conditional Caching - Control caching with custom
cache_ifpredicates - 🚫 Error Filtering - Automatically skip caching errors (default for
Resulttypes) - 📊 Value-Based Caching - Cache only results meeting specific criteria (non-empty, valid, etc.)
- 💡 Smart Defaults -
Result<T, E>types only cacheOkvalues by default - 🔧 Custom Logic - Use any Rust logic in your cache predicates
- ⚡ Zero Overhead - No performance penalty when predicates return
true - 🔒 Type-Safe - Compile-time validation of predicate functions
Quick Start:
use cachelito::cache;
// Only cache non-empty results
fn should_cache(_key: &String, result: &Vec<String>) -> bool {
!result.is_empty()
}
#[cache(scope = "global", limit = 100, cache_if = should_cache)]
fn fetch_items(category: String) -> Vec<String> {
// Empty results won't be cached
database.query(category)
}
// Default behavior: Result types only cache Ok values
#[cache(scope = "global", limit = 50)]
fn validate_email(email: String) -> Result<String, String> {
if email.contains('@') {
Ok(format!("Valid: {}", email)) // ✅ Cached
} else {
Err(format!("Invalid: {}", email)) // ❌ NOT cached
}
}
// Custom predicate for Result types
fn cache_only_ok(_key: &String, result: &Result<User, Error>) -> bool {
result.is_ok()
}
#[cache(scope = "global", cache_if = cache_only_ok)]
fn fetch_user(id: u32) -> Result<User, Error> {
// Only successful results are cached
api_client.get_user(id)
}Common Use Cases:
- ✅ Don't cache empty collections
- ✅ Skip caching
Nonevalues - ✅ Only cache successful HTTP responses
- ✅ Filter out invalid or temporary data
- ✅ Cache based on value characteristics
See also: examples/conditional_caching.rs
What's Changed
Full Changelog: 0.13.0...0.14.0