evicting_cache_map 0.6.0

An Evicting LRU cache supporting prune hooks
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented0 out of 0 items with examples
  • Size
  • Source code size: 29.84 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.01 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 19s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • will459

EvictingCacheMap

An Evicting Cache Map

This crate provides a no_std implementation of an EvictingCacheMap, like a HashMap but has a maximum size and removes the least recently used element to maintain it.

What sets this cache map apart is the registration of a closure with which is run on element removal. This API design is borrowed from Meta's Folly and intends to be more flexible than implementations which simply drop values on removal.

Changelog

Details

This crate is powered by ordered_hash_map, which is itself backed by hashbrown to leverage the well vetted and fast HashMap. The EvictingCacheMap is a light wrapper around an ordered_hash_map which enforces max sizes, promotes elements upon re-insertion, and calls the closure on removed elements.

Usage

Send evicted elements to a channel. The receiver could be in a different thread, for instance.

fn main() {
    let (tx, rx) = mpsc::channel();
    let mut cachemap: EvictingCacheMap<String, u8, 10, _> =
        EvictingCacheMap::with_prune_hook(move |k, v| tx.send((k, v)).unwrap());
    let send = thread::spawn(move || {
        for x in 0..20 {
            cachemap.insert(x.to_string(), x)
        }
    });

    let recv = thread::spawn(move || {
        while let Ok((k, v)) = rx.recv() {
            println!("{k}:{v}")
        }
    });

    let _ = send.join();
    let _ = recv.join();
}