Crates.io | timed_set |
lib.rs | timed_set |
version | 0.0.4 |
source | src |
created_at | 2022-06-07 04:11:48.08177 |
updated_at | 2022-06-07 09:40:10.183961 |
description | A simple timed set in Rust to store elements for a given time period. |
homepage | https://github.com/marirs/timed-set-rs |
repository | https://github.com/marirs/timed-set-rs |
max_upload_size | |
id | 601231 |
size | 13,941 |
A simple timed set in Rust to store elements for a given time period.
[dependencies]
timed_set = "0.0.4"
use timed_set::TimedSet;
use std::{time::Duration, thread::sleep};
fn main() {
let mut ts = TimedSet::new(Duration::from_secs(3));
ts.add("element_1");
assert!(ts.contains(&"element_1"));
sleep(Duration::from_secs(3));
assert!(!ts.contains(&"element_1"));
}
Custom ttl
for specifically 1 element
use timed_set::TimedSet;
use std::{time::Duration, thread::sleep};
fn main() {
let mut ts = TimedSet::new(Duration::from_secs(3));
ts.add("element_1");
ts.add("element_2", Duration::from_secs(10)); // element with custom ttl
assert!(ts.contains(&"element_1"));
assert!(ts.contains(&"element_2"));
sleep(Duration::from_secs(3));
assert!(!ts.contains(&"element_1")); // expired
assert!(ts.contains(&"element_2"));
sleep(Duration::from_secs(8));
assert!(!ts.contains(&"element_2")); // expired
}
License: MIT