https://github.com/zekrotja/timedmap-rs
A thread-safe hash map with expiring key-value pairs.
https://github.com/zekrotja/timedmap-rs
cache cache-map crate hacktoberfest hashmap library map rust time timemap
Last synced: 11 months ago
JSON representation
A thread-safe hash map with expiring key-value pairs.
- Host: GitHub
- URL: https://github.com/zekrotja/timedmap-rs
- Owner: zekroTJA
- License: mit
- Created: 2023-05-19T13:01:31.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-03-26T14:15:14.000Z (about 2 years ago)
- Last Synced: 2024-12-06T18:53:51.086Z (over 1 year ago)
- Topics: cache, cache-map, crate, hacktoberfest, hashmap, library, map, rust, time, timemap
- Language: Rust
- Homepage: https://docs.rs/timedmap
- Size: 22.5 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# timedmap [](https://crates.io/crates/timedmap) [](https://docs.rs/timedmap)
A *more or less* port of my package [timedmap](https://github.com/zekrotja/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
```rust
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](https://github.com/zekroTJA/timedmap-rs). 😄
```rust
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();
```