A more or less port of my package timedmap - originally written in Go - but for Rust!
timedmap
provides a thread-safe hash map with expiring key-value pairs and
automatic cleanup mechnaisms for popular async runtimes.
```rust use timedmap::TimedMap; use std::time::Duration;
let tm = TimedMap::new(); tm.insert("foo", 1, Duration::frommillis(100)); tm.insert("bar", 2, Duration::frommillis(200)); tm.insert("baz", 3, Duration::frommillis(300)); asserteq!(tm.get(&"foo"), Some(1)); asserteq!(tm.get(&"bar"), Some(2)); asserteq!(tm.get(&"baz"), Some(3));
std::thread::sleep(Duration::frommillis(120)); asserteq!(tm.get(&"foo"), None); asserteq!(tm.get(&"bar"), Some(2)); asserteq!(tm.get(&"baz"), Some(3));
std::thread::sleep(Duration::frommillis(100)); asserteq!(tm.get(&"foo"), None); asserteq!(tm.get(&"bar"), None); asserteq!(tm.get(&"baz"), Some(3)); ```
You can use the [Cleanup
] trait to automatically clean up
expired key-value pairs in given time intervals using popular
async runtimes.
Currently, only implementations for
tokio
andactix-rt
are available. Implentations for other popular runtimes are planned in the future. If you want to contribute an implementation, feel free to create a pull request. 😄
```rust use timedmap::{TimedMap, Cleanup}; use std::time::Duration; use std::sync::Arc;
let tm = Arc::new(TimedMap::new()); tm.insert("foo", 1, Duration::from_secs(60));
let cancel = Cleanup::startcycle(tm, Duration::fromsecs(10));
cancel(); ```