#cache #hash-map #expiration #control #mem-cache

simple-cache-rs

A simple rust implementation of HashMap with expiration control

5 releases (3 breaking)

0.4.0 Dec 5, 2020
0.3.1 Dec 3, 2020
0.3.0 Dec 3, 2020
0.2.0 Dec 2, 2020
0.1.0 Nov 30, 2020

#16 in #expiration

Apache-2.0

10KB
132 lines

A simple in-mem cache for Rust

Docs Apache-2 licensed CI

A simple rust implementation of HashMap with expiration control.

Example

Without expiration:

use simple_cache_rs::SimpleCache;

let mut scache: SimpleCache<i32, String> = SimpleCache::new(None);

scache.insert(1, String::from("test"));
println!("{:?}", scache.get(&1));

With expiration:

use simple_cache_rs::SimpleCache;
use std::time::Duration;
use std::thread; // For example purposes only

let timeout = Duration::new(1, 0);
let mut scache: SimpleCache<i32, String> = SimpleCache::new(Some(timeout));

scache.insert(1, String::from("test"));
assert_eq!(Some(String::from("test")), scache.get(&1));

thread::sleep(Duration::new(1, 1)); // For example purposes only
assert_ne!(Some(String::from("test")), scache.get(&1));

No runtime deps