Store a resolution with an explicit TTL (tests use negative TTLs).
(
&mut self,
server_url: &str,
name: &str,
public_key: &str,
status: &str,
ttl: chrono::Duration,
)
| 156 | |
| 157 | /// Store a resolution with an explicit TTL (tests use negative TTLs). |
| 158 | pub fn put_with_ttl( |
| 159 | &mut self, |
| 160 | server_url: &str, |
| 161 | name: &str, |
| 162 | public_key: &str, |
| 163 | status: &str, |
| 164 | ttl: chrono::Duration, |
| 165 | ) { |
| 166 | let now = Utc::now(); |
| 167 | let entry = CacheEntry { |
| 168 | server_url: server_url.to_string(), |
| 169 | public_key: public_key.to_string(), |
| 170 | status: status.to_string(), |
| 171 | resolved_at: now, |
| 172 | expires_at: now + ttl, |
| 173 | }; |
| 174 | self.entries.insert(name.to_string(), entry); |
| 175 | |
| 176 | // Best-effort persist. A read-only home dir or a full disk must not |
| 177 | // fail verification. |
| 178 | if let Err(e) = std::fs::create_dir_all(self.dir.as_path()) { |
| 179 | log::debug!("could not create key cache dir: {e}"); |
| 180 | return; |
| 181 | } |
| 182 | let file = CacheFile { |
| 183 | entries: self.entries.clone(), |
| 184 | }; |
| 185 | match serde_json::to_string_pretty(&file) { |
| 186 | Ok(json) => { |
| 187 | if let Err(e) = std::fs::write(self.path.as_path(), json.into_bytes()) { |
| 188 | log::debug!("could not write key cache: {e}"); |
| 189 | } |
| 190 | } |
| 191 | Err(e) => { |
| 192 | log::debug!("could not serialize key cache: {e}"); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | #[cfg(test)] |