tetrio_api/models/
cache.rs1use std::time::{UNIX_EPOCH, SystemTime, Duration};
2
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub struct Cache {
10 pub status: String,
11 pub cached_at: u128,
12 pub cached_until: u128,
13}
14
15impl Cache {
16 pub fn cached_for(duration: Duration) -> Self {
17 let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("That can't be happening").as_millis();
18
19 Cache {
20 cached_at: now,
21 status: String::from("cached"),
22 cached_until: now + duration.as_millis()
23 }
24 }
25}
26
27impl Cache {
28 pub fn time_until_elapsed(&self) -> Duration {
29 Duration::from_millis((self.cached_until - SystemTime::now().duration_since(UNIX_EPOCH).expect("That can't be happening").as_millis()) as u64)
30 }
31}