Code Monkey home page Code Monkey logo

timedmap-rs's Introduction

timedmap Crates.io docs.rs

A more or less port of my package timedmap - originally written in Go - but for Rust!

timedmap provides a thread-safe hash map with expiring key-value pairs and automatic cleanup mechnaisms for popular async runtimes.

Basic Example

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

Cleanup Example

You can use the start_cleaner function to automatically clean up expired key-value pairs in given time intervals using popular async runtimes.

Currently, only implementations for tokio and actix-rt are available. Implentations for other popular runtimes are planned in the future. If you want to contribute an implementation, feel free to create a 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();

timedmap-rs's People

Contributors

zekrotja avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

supercoolspy

timedmap-rs's Issues

Does the cleanup impl make sense?

I see two main issues:

  • Constant refetching of the time (implicit inside of is_expired)
  • Does collecting into a Vec even make sense compared to a single retain call?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.