use quickleaf::{Quickleaf, ListProps, Order, Filter, Duration};
use std::thread;
fn main() {
println!("๐ Quickleaf TTL Cache Example");
println!("================================\n");
let mut cache = Quickleaf::with_default_ttl(5, Duration::from_secs(2));
println!("๐ Inserting data...");
cache.insert("persistent", "This won't expire"); cache.insert_with_ttl("short_lived", "This expires in 1 second", Duration::from_secs(1));
cache.insert_with_ttl("medium_lived", "This expires in 3 seconds", Duration::from_secs(3));
println!(" - persistent: {}", cache.get("persistent").unwrap());
println!(" - short_lived: {}", cache.get("short_lived").unwrap());
println!(" - medium_lived: {}", cache.get("medium_lived").unwrap());
println!(" Cache size: {}\n", cache.len());
println!("โฑ๏ธ Waiting 1.5 seconds...");
thread::sleep(Duration::from_millis(1500));
println!("๐ Checking cache after 1.5 seconds:");
println!(" - persistent: {:?}", cache.get("persistent"));
println!(" - short_lived: {:?}", cache.get("short_lived")); println!(" - medium_lived: {:?}", cache.get("medium_lived"));
println!(" Cache size: {}\n", cache.len());
println!("โฑ๏ธ Waiting another 2 seconds...");
thread::sleep(Duration::from_secs(2));
println!("๐งน Manual cleanup:");
let removed_count = cache.cleanup_expired();
println!(" Removed {} expired items", removed_count);
println!(" Cache size: {}\n", cache.len());
println!("๐ Remaining items:");
let result = cache.list(ListProps::default()).unwrap();
for (key, value) in result {
println!(" - {}: {}", key, value);
}
println!("\n๐ง Adding more test data...");
cache.insert_with_ttl("apple_pie", "Delicious!", Duration::from_secs(10));
cache.insert_with_ttl("apple_juice", "Refreshing!", Duration::from_secs(10));
cache.insert_with_ttl("banana_split", "Sweet!", Duration::from_secs(10));
println!("๐ Filtering items starting with 'apple':");
let filtered = cache.list(
ListProps::default()
.filter(Filter::StartWith("apple".to_string()))
.order(Order::Asc)
).unwrap();
for (key, value) in filtered {
println!(" - {}: {}", key, value);
}
println!("\nโ
Example completed!");
}