#lru-cache #hash-map #hook #prune #element #evicting

no-std evicting_cache_map

An Evicting LRU cache supporting prune hooks

2 unstable releases

0.4.0 Nov 12, 2023
0.3.1 Nov 10, 2023

#1963 in Data structures

Download history 12/week @ 2024-03-27 26/week @ 2024-04-03 2/week @ 2024-05-15 57/week @ 2024-05-22 115/week @ 2024-05-29 188/week @ 2024-06-05 97/week @ 2024-06-12 208/week @ 2024-06-19 219/week @ 2024-06-26

736 downloads per month

BSD-2-Clause

115KB
2.5K SLoC

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();
}

Dependencies

~1.5MB
~24K SLoC