Skip to content

Commit

Permalink
recache if unable to serialize existing cache
Browse files Browse the repository at this point in the history
  • Loading branch information
conaticus committed Jun 29, 2023
1 parent a6c4732 commit ded79ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src-tauri/src/filesystem/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ fn save_to_cache(state: &mut MutexGuard<AppState>) {
}

/// Reads and decodes the cache file and stores it in memory for quick access.
pub fn load_system_cache(state_mux: &StateSafe) {
/// Returns false if the cache was unable to deserialize.
pub fn load_system_cache(state_mux: &StateSafe) -> bool {
let state = &mut state_mux.lock().unwrap();
let file_contents = fs::read_to_string(CACHE_FILE_PATH).unwrap();
state.system_cache = serde_json::from_str(&file_contents).unwrap();
}

let deserialize_result = serde_json::from_str(&file_contents);
if let Ok(system_cache) = deserialize_result {
state.system_cache = system_cache;
return true;
}

false
}
4 changes: 2 additions & 2 deletions src-tauri/src/filesystem/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ pub fn get_volumes(state_mux: State<StateSafe>) -> Vec<Volume> {
let mut sys = System::new_all();
sys.refresh_all();

let cache_exists = fs::metadata(CACHE_FILE_PATH).is_ok();
let mut cache_exists = fs::metadata(CACHE_FILE_PATH).is_ok();
if cache_exists {
load_system_cache(&state_mux);
cache_exists = load_system_cache(&state_mux);
} else {
File::create(CACHE_FILE_PATH).unwrap();
}
Expand Down

0 comments on commit ded79ba

Please sign in to comment.