3 个稳定版本

1.0.2 2024年3月26日
1.0.1 2023年5月19日

#1 in #tm

Download history 465/week @ 2024-04-08 228/week @ 2024-04-15 294/week @ 2024-04-22 154/week @ 2024-04-29 252/week @ 2024-05-06 216/week @ 2024-05-13 94/week @ 2024-05-20 101/week @ 2024-05-27 248/week @ 2024-06-03 106/week @ 2024-06-10 3/week @ 2024-06-17 174/week @ 2024-07-01 102/week @ 2024-07-08 102/week @ 2024-07-15 107/week @ 2024-07-22

485 每月下载量
用于 2 crates

MIT 许可证

26KB
502

timedmap Crates.io docs.rs

这是我用 Go 编写的包 timedmap 的一个“更多或更少”的移植版本——但针对 Rust!

timedmap 提供了一个具有过期键值对的线程安全哈希表,并自动清理机制,适用于流行的异步运行时。

基本示例

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));

清理示例

您可以使用 start_cleaner 函数,使用流行的异步运行时,在给定的时间间隔内自动清理过期键值对。

目前,只有对 tokioactix-rt 的实现。计划在未来实现其他流行运行时。如果您想贡献一个实现,请随意创建 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();

依赖项

~0–8MB
~62K SLoC