Rust · LRU cache in Rust · no dependency but clap
Flashvault is an LRU cache in Rust with a capacity bound, TTL expiry, and live hit-rate stats, all of it readable end to end. Set some keys below and watch the eviction order move.
most-recently-used first · red row is what the next insert would evict · state resets with the button, not on reload
A HashMap for lookup and an intrusive doubly linked list for order, so both get and put stay O(1).
Entries live in a flat Vec of slots. A free list lets removed slots be reused instead of leaking space.
Each node carries prev and next indices, so moving a key to the front on a hit is a pointer swap, not a scan.
A put that would exceed capacity evicts the tail, the least-recently-used entry, before inserting.
Expiry is checked lazily at get time. An expired entry counts as a miss and an expiration, then is removed.
A Clock trait separates real time from a settable TestClock, so TTL tests are deterministic, no sleeping.
Hits, misses, evictions, expirations, and size are counted as they happen and read back with one call.
A canned workload at capacity 3: fill the cache, touch a key so it survives, insert past the bound so eviction fires, and let a short-lived entry expire. The final line is the exact stats the crate produces.
There are excellent cache crates. Flashvault is the other option: one file you can read, own, and drop in when a large dependency is more than the job needs.
A solid, widely used LRU implementation. It does not carry TTL expiry or a stats surface, so those are yours to add on top.
Full-featured, concurrent, production caches. Powerful, and far more code and surface than a single-threaded readable cache calls for.
No recency, no bound, no expiry. Grows without limit and never evicts, which is exactly what a cache is supposed to do for you.
HashMap lookup, an intrusive list for O(1) recency, a hard capacity bound, lazy TTL expiry, an injectable clock, and live stats. Small enough to read end to end.
The same cache ships three ways: call it from Rust, run one-shot commands, or poke at it interactively.
Build a Cache with a capacity and a Clock, then put with an optional TTL and get. Read stats() for hits, misses, evictions, expirations, size, and hit rate.
One-shot set and get, or demo to run the canned workload and print the stats it produces.
cargo run -- repl gives an interactive prompt: set, get, del, keys, stats, quit.
cargo test covers capacity-bound eviction, recency refresh on get, TTL expiry as a miss plus removal, stats correctness, and zero-capacity safety, using the settable TestClock so nothing sleeps.
# run the canned workload and print the stats it produces cargo run -- --capacity 3 demo # interactive repl: set, get, del, keys, stats cargo run -- repl