timedmap/lib.rs
1//! `timedmap` provides a thread-safe hash map with expiring key-value pairs and
2//! automatic cleanup mechnaisms for popular async runtimes.
3//!
4//! # Basic Example
5//! ```
6//! use timedmap::TimedMap;
7//! use std::time::Duration;
8//!
9//! let tm = TimedMap::new();
10//! tm.insert("foo", 1, Duration::from_millis(100));
11//! tm.insert("bar", 2, Duration::from_millis(200));
12//! tm.insert("baz", 3, Duration::from_millis(300));
13//! assert_eq!(tm.get(&"foo"), Some(1));
14//! assert_eq!(tm.get(&"bar"), Some(2));
15//! assert_eq!(tm.get(&"baz"), Some(3));
16//!
17//! std::thread::sleep(Duration::from_millis(120));
18//! assert_eq!(tm.get(&"foo"), None);
19//! assert_eq!(tm.get(&"bar"), Some(2));
20//! assert_eq!(tm.get(&"baz"), Some(3));
21//!
22//! std::thread::sleep(Duration::from_millis(100));
23//! assert_eq!(tm.get(&"foo"), None);
24//! assert_eq!(tm.get(&"bar"), None);
25//! assert_eq!(tm.get(&"baz"), Some(3));
26//! ```
27//!
28//! # Cleanup Example
29//!
30//! You can use the `start_cleaner` function to automatically clean up
31//! expired key-value pairs in given time intervals using popular
32//! async runtimes.
33//!
34//! > Currently, only implementations for `tokio` and `actix-rt`
35//! are available. Implentations for other popular runtimes are
36//! planned in the future. If you want to contribute an implementation,
37//! feel free to create a
38//! [pull request](https://github.com/zekroTJA/timedmap-rs). 😄
39//!
40//! ```
41//! use timedmap::{TimedMap, start_cleaner};
42//! use std::time::Duration;
43//! use std::sync::Arc;
44//!
45//! let tm = Arc::new(TimedMap::new());
46//! tm.insert("foo", 1, Duration::from_secs(60));
47//!
48//! # #[cfg(feature = "tokio")]
49//! # tokio_test::block_on(async {
50//! let cancel = start_cleaner(tm, Duration::from_secs(10));
51//!
52//! cancel();
53//! # });
54//! ```
55
56mod timedmap;
57pub use crate::timedmap::*;
58
59mod value;
60pub use crate::value::*;
61
62mod cleanup;
63pub use crate::cleanup::*;
64
65pub mod time;