3 stable releases

1.0.2 Mar 26, 2024
1.0.1 May 19, 2023

#357 in Concurrency

Download history 525/week @ 2024-08-02 290/week @ 2024-08-09 321/week @ 2024-08-16 402/week @ 2024-08-23 175/week @ 2024-08-30 174/week @ 2024-09-06 198/week @ 2024-09-13 443/week @ 2024-09-20 402/week @ 2024-09-27 639/week @ 2024-10-04 373/week @ 2024-10-11 277/week @ 2024-10-18 164/week @ 2024-10-25 164/week @ 2024-11-01 309/week @ 2024-11-08 398/week @ 2024-11-15

1,078 downloads per month
Used in 2 crates

MIT license

26KB
502 lines

timedmap Crates.io docs.rs

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.

Basic Example

use timedmap::TimedMap;
use std::time::Duration;

let tm = TimedMap::new();
tm.insert("foo", 1, Duration::from_millis(100));
tm.insert("bar", 2, Duration::from_millis(200));
tm.insert("baz", 3, Duration::from_millis(300));
assert_eq!(tm.get(&"foo"), Some(1));
assert_eq!(tm.get(&"bar"), Some(2));
assert_eq!(tm.get(&"baz"), Some(3));

std::thread::sleep(Duration::from_millis(120));
assert_eq!(tm.get(&"foo"), None);
assert_eq!(tm.get(&"bar"), Some(2));
assert_eq!(tm.get(&"baz"), Some(3));

std::thread::sleep(Duration::from_millis(100));
assert_eq!(tm.get(&"foo"), None);
assert_eq!(tm.get(&"bar"), None);
assert_eq!(tm.get(&"baz"), Some(3));

Cleanup Example

You can use the start_cleaner function to automatically clean up expired key-value pairs in given time intervals using popular async runtimes.

Currently, only implementations for tokio and actix-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. 😄

use timedmap::{TimedMap, start_cleaner};
use std::time::Duration;
use std::sync::Arc;

let tm = Arc::new(TimedMap::new());
tm.insert("foo", 1, Duration::from_secs(60));

let cancel = start_cleaner(tm, Duration::from_secs(10));

cancel();

Dependencies

~0–8MB
~63K SLoC