//! Example of having a cleanup thread. //! //! In some instances objects may be expensive to delete. Sending objects to a dedicated cleanup //! thread can alleviate the load on the workers. //! //! The sender thread writes key/value pairs from 0 through 19 with a max cachemap size of 10. This //! will result in elements 0 through 9 being pushed out and sent on the channel where they are //! written to stdout. use evicting_cache_map::EvictingCacheMap; use std::sync::mpsc; use std::thread; 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(); }