1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::time::{Instant, Duration};
use std::ops::{Deref, DerefMut};


pub struct TimedCache<T> {
    value: T,
    last_update: Instant,
    lifetime: Duration
}

impl<T> TimedCache<T> {

    pub fn new(value: T, lifetime: Duration) -> Self {
        Self {
            value,
            last_update: Instant::now(),
            lifetime
        }
    }

    pub fn cache_update(&mut self) -> &mut Self {
        self.last_update = Instant::now();
        self
    }

    pub fn is_cache_timed_out(&self) -> bool {
        self.last_update.elapsed() >= self.lifetime
    }

}

impl<T> Deref for TimedCache<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.value
    }
}

impl<T> DerefMut for TimedCache<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.value
    }
}