Only overwrite old cache if an update was made

This commit is contained in:
Love 2025-01-08 19:20:05 +01:00
parent e5684a0fc0
commit 9d7d70ef00
3 changed files with 26 additions and 7 deletions

View File

@ -9,7 +9,7 @@ use crate::Hollidays;
const CET: FixedOffset = unsafe { FixedOffset::east_opt(3600).unwrap_unchecked() };
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct CacheLine {
#[serde(with = "ts_seconds")]
date: DateTime<Utc>,
@ -86,6 +86,13 @@ impl CacheLine {
}
}
}
impl PartialEq<Vec<ExchangeRateResult>> for CacheLine {
fn eq(&self, other: &Vec<ExchangeRateResult>) -> bool {
&self.exchange_rate_results == other
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -65,12 +65,24 @@ async fn main() -> ExitCode {
return ExitCode::FAILURE;
}
};
if !cache_ok {
if let Some(cache_safe) = cache.as_mut() {
let cache_line = CacheLine::new(parsed.clone());
cache_safe.set_cache_line(cli.resolution, cache_line);
if let Err(e) = cache_safe.save() {
eprintln!("Failed to save to cache with: {:?}", e);
let not_equal_cache = cache.as_ref().map_or_else(
|| true,
|cache_local| {
cache_local
.get_cache_line(cli.resolution)
.map_or_else(|| true, |cache_line| cache_line == &parsed)
},
);
if not_equal_cache {
if let Some(cache_safe) = cache.as_mut() {
let cache_line = CacheLine::new(parsed.clone());
cache_safe.set_cache_line(cli.resolution, cache_line);
if let Err(e) = cache_safe.save() {
eprintln!("Failed to save to cache with: {:?}", e);
}
}
}
}

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ExchangeRateResult {
pub time: String,
pub rates: HashMap<String, f64>,