use priority_lfu::{Cache, CacheKey, CachePolicy, DeepSizeOf};
#[derive(Clone, Debug, PartialEq, DeepSizeOf)]
struct UserProfile {
name: String,
email: String,
}
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct PremiumUserId(u64);
impl CacheKey for PremiumUserId {
type Value = UserProfile;
fn policy(&self) -> CachePolicy {
CachePolicy::Critical }
}
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct FreeUserId(u64);
impl CacheKey for FreeUserId {
type Value = UserProfile;
fn policy(&self) -> CachePolicy {
CachePolicy::Volatile }
}
fn main() {
let cache = Cache::new(500);
cache.insert(
PremiumUserId(1),
UserProfile {
name: "Alice Premium".to_string(),
email: "alice@premium.com".to_string(),
},
);
cache.insert(
FreeUserId(2),
UserProfile {
name: "Bob Free".to_string(),
email: "bob@free.com".to_string(),
},
);
for i in 3..20 {
cache.insert(
FreeUserId(i),
UserProfile {
name: format!("User {}", i),
email: format!("user{}@free.com", i),
},
);
}
if cache.contains(&PremiumUserId(1)) {
println!("✓ Premium user (high priority) survived eviction");
} else {
println!("✗ Premium user was evicted (unexpected)");
}
if cache.contains(&FreeUserId(2)) {
println!("✓ Free user still in cache");
} else {
println!("✗ Free user was evicted (expected due to low priority)");
}
println!("\nCache stats:");
println!(" Entries: {}", cache.len());
println!(" Size: {} bytes", cache.size());
}