Skip to main content

embedded_cache/
embedded-cache.rs

1//! Embedded LRU cache with a hard memory ceiling — the canonical
2//! "in-process Redis cache" use case. Stores 10 000 entries against a
3//! 200 KiB ceiling; the LRU policy evicts the oldest to make room.
4//!
5//! Run with: `cargo run -p kevy-embedded --example embedded-cache`
6
7use kevy_embedded::{Config, EvictionPolicy, Store};
8
9fn main() -> std::io::Result<()> {
10    let s = Store::open(
11        Config::default()
12            .with_max_memory(200 * 1024)
13            .with_eviction(EvictionPolicy::AllKeysLru),
14    )?;
15
16    for i in 0..10_000 {
17        let key = format!("user:{i:05}");
18        let val = format!("user-payload-{i}");
19        s.set(key.as_bytes(), val.as_bytes())?;
20    }
21
22    println!("dbsize after insert flood: {}", s.dbsize());
23    println!("used_memory: {} bytes (limit 200 KiB)", s.used_memory());
24    println!("evictions_total: {}", s.evictions_total());
25
26    // Touch a recent key — it should still be live.
27    let recent = format!("user:0{}", 9999);
28    println!(
29        "user:09999 → {:?}",
30        s.get(recent.as_bytes())?.as_deref().map(String::from_utf8_lossy)
31    );
32
33    Ok(())
34}