| 97 | |
| 98 | #[tracing::instrument(level = "debug", skip(self, _url))] |
| 99 | pub fn get<T>(&self, _url: &str) -> Result<T> |
| 100 | where |
| 101 | T: for<'a> Deserialize<'a>, |
| 102 | { |
| 103 | let mut conn = self.pool.get().unwrap(); |
| 104 | |
| 105 | let data: CacheModel = cache.filter(url.eq(_url)).first::<CacheModel>(&mut conn)?; |
| 106 | let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?; |
| 107 | |
| 108 | let expires = Duration::from_secs(data.expires as u64); |
| 109 | if current_time > expires { |
| 110 | debug!("Cache expired for {}", _url); |
| 111 | return Err("Cache expired".into()); |
| 112 | } |
| 113 | |
| 114 | let parsed: T = serde_json::from_slice(&data.blob)?; |
| 115 | Ok(parsed) |
| 116 | } |
| 117 | } |