1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! 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<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();
}