# 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](https://github.com/facebook/folly/blob/main/folly/container/EvictingCacheMap.h) and intends to be more flexible than implementations which simply drop values on removal. ## [Changelog](CHANGELOG.md) ## Details This crate is powered by [ordered\_hash\_map](https://gitlab.com/kelderon/rs-collections/-/tree/main/ordered_hash_map), which is itself backed by [hashbrown](https://github.com/rust-lang/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. ```rust fn main() { let (tx, rx) = mpsc::channel(); let mut cachemap: EvictingCacheMap = 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(); } ```