5 unstable releases
0.3.0 | Jan 6, 2024 |
---|---|
0.2.2 | Sep 24, 2023 |
0.2.1 | Aug 10, 2023 |
0.2.0 | Aug 9, 2023 |
0.1.0 | Aug 9, 2023 |
#236 in Caching
33 downloads per month
22KB
304 lines
EphemerOpt
An ephemeral Option
for Rust. When created, this EphemeralOption
takes an expiry time and a value, and the EphemeralOption
will revert to None
after the time runs out.
This can be useful for possibly caching values instead of rerunning an expensive computation to get them. See the examples for a real-world demonstration of this using CPU data and message passing.
lib.rs
:
An ephemeral Option
for Rust. When created, this EphemeralOption
takes an expiry time and a value,
and the EphemeralOption
will revert to None
after the time runs out.
Example
use ephemeropt::EphemeralOption;
let mut num_opt = EphemeralOption::new(0, std::time::Duration::from_secs(1));
// Will only go up to 10, because every other call will be cached
for _ in 0..=20 {
match num_opt.get() {
Some(&num) => println!("{num}"),
None => {
let prev_num = num_opt.get_expired().unwrap();
let num = num_opt.insert(prev_num + 1);
println!("{num}");
}
}
std::thread::sleep(std::time::Duration::from_millis(500));
}