crate_activity/
read_cache.rs

1crate::ix!();
2
3// Cache the response to a file
4pub async fn cache_response(config_dir: &Path, crate_name: &str, date: NaiveDate, response: &CrateResponse) 
5    -> io::Result<()> 
6{
7    let cache_dir = config_dir.join("cache");
8    fs::create_dir_all(&cache_dir).await?;
9
10    let cache_file = cache_dir.join(format!("{}_{}.json", crate_name, date));
11    let json = serde_json::to_string(response)?; // Serialize to string
12    fs::write(cache_file, json).await // Write as bytes
13}
14
15// Load a cached response if available
16pub async fn load_cached_response(config_dir: &Path, crate_name: &str, date: NaiveDate) -> Option<CrateResponse> {
17    let cache_file = config_dir.join("cache").join(format!("{}_{}.json", crate_name, date));
18
19    if cache_file.exists() {
20        if let Ok(json) = fs::read_to_string(&cache_file).await {
21            if let Ok(response) = serde_json::from_str::<CrateResponse>(&json) {
22                return Some(response);
23            }
24        }
25    }
26    None
27}