use cachekit::policy::slru::SlruCore;
fn main() {
println!("=== SLRU Cache Example ===\n");
let mut cache = SlruCore::new(10, 0.25);
println!("Created SLRU cache: capacity={}\n", cache.capacity());
for i in 1..=5 {
cache.insert(i, format!("value-{}", i));
}
println!("Inserted keys 1-5 (all in probationary segment)");
println!(" len: {}", cache.len());
cache.get(&1);
cache.get(&2);
println!("\nAccessed keys 1 and 2 (promoted to protected segment)");
for i in 6..=10 {
cache.insert(i, format!("value-{}", i));
}
println!("Inserted keys 6-10");
println!(" len: {}", cache.len());
cache.insert(11, "value-11".to_string());
cache.insert(12, "value-12".to_string());
println!("\nAfter inserting keys 11, 12:");
println!(
" contains 1? {} (was promoted to protected)",
cache.contains(&1)
);
println!(
" contains 2? {} (was promoted to protected)",
cache.contains(&2)
);
println!(" len: {}", cache.len());
println!("\n=== Scan Resistance Demo ===\n");
let mut cache = SlruCore::new(10, 0.3);
cache.insert(1, "hot-1".to_string());
cache.insert(2, "hot-2".to_string());
cache.insert(3, "hot-3".to_string());
cache.get(&1);
cache.get(&2);
cache.get(&3);
println!("Inserted and promoted hot items: 1, 2, 3");
println!("Simulating scan with items 100-120...");
for i in 100..=120 {
cache.insert(i, format!("scan-{}", i));
}
println!("\nAfter scan (21 one-time insertions):");
println!(" contains hot-1? {}", cache.contains(&1));
println!(" contains hot-2? {}", cache.contains(&2));
println!(" contains hot-3? {}", cache.contains(&3));
println!(" len: {}", cache.len());
let scan_items_remaining: Vec<_> = (100..=120).filter(|&i| cache.contains(&i)).collect();
println!(" scan items remaining: {:?}", scan_items_remaining);
println!("\n=== SLRU vs LRU Comparison ===\n");
println!("Key differences:");
println!(" • SLRU: New items enter probationary segment (LRU eviction)");
println!(" • SLRU: Re-accessed items move to protected segment (LRU eviction)");
println!(" • LRU: All items in single list (LRU eviction from tail)");
println!();
println!("Benefits of SLRU:");
println!(" • Scan resistance: one-time accesses don't pollute protected segment");
println!(" • Simple: just two LRU lists, no complex frequency tracking");
println!(" • Tunable: adjust probationary/protected ratio for workload");
}