#timed #set #ttl

timed_set

A simple timed set in Rust to store elements for a given time period

4 releases

0.0.4 Jun 7, 2022
0.0.3 Jun 7, 2022
0.0.2 Jun 7, 2022
0.0.1 Jun 7, 2022

#10 in #timed

Custom license

11KB
124 lines

Timed Set

Linux Arm7 Linux x86_64 macOS intel Windows

A simple timed set in Rust to store elements for a given time period.

Usage

[dependencies]
timed_set = "0.0.4"

Example

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

No runtime deps