timed_set

Crates.iotimed_set
lib.rstimed_set
version0.0.4
sourcesrc
created_at2022-06-07 04:11:48.08177
updated_at2022-06-07 09:40:10.183961
descriptionA simple timed set in Rust to store elements for a given time period.
homepagehttps://github.com/marirs/timed-set-rs
repositoryhttps://github.com/marirs/timed-set-rs
max_upload_size
id601231
size13,941
SG (marirs)

documentation

README

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

Commit count: 30

cargo fmt