tetrio_api/models/
cache.rs

1use std::time::{UNIX_EPOCH, SystemTime, Duration};
2
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7/// A cache object sent by the tetr.io api,
8/// only the cached_until field is actually used by the cached http client.
9pub 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}