Add cache_version flag to saved state
Can be used to determine whether the cache file format has been broken after an update. If so, the cache will be discarded and recreated.
This commit is contained in:
@@ -13,6 +13,7 @@ use crate::queue;
|
||||
use crate::serialization::{Serializer, CBOR, TOML};
|
||||
|
||||
pub const CLIENT_ID: &str = "d420a117a32841c2b3474932e49fb54b";
|
||||
pub const CACHE_VERSION: u16 = 1;
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug, Default)]
|
||||
pub struct ConfigValues {
|
||||
@@ -81,6 +82,7 @@ pub struct UserState {
|
||||
pub repeat: queue::RepeatSetting,
|
||||
pub queuestate: QueueState,
|
||||
pub playlist_orders: HashMap<String, SortingOrder>,
|
||||
pub cache_version: u16,
|
||||
}
|
||||
|
||||
impl Default for UserState {
|
||||
@@ -91,6 +93,7 @@ impl Default for UserState {
|
||||
repeat: queue::RepeatSetting::None,
|
||||
queuestate: QueueState::default(),
|
||||
playlist_orders: HashMap::new(),
|
||||
cache_version: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,6 +153,9 @@ impl Config {
|
||||
}
|
||||
|
||||
pub fn save_state(&self) {
|
||||
// update cache version number
|
||||
self.with_state_mut(|mut state| state.cache_version = CACHE_VERSION);
|
||||
|
||||
let path = config_path("userstate.cbor");
|
||||
debug!("saving user state to {}", path.display());
|
||||
if let Err(e) = CBOR.write(path, self.state().clone()) {
|
||||
|
||||
@@ -10,8 +10,8 @@ use rspotify::model::Id;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::config;
|
||||
use crate::config::Config;
|
||||
use crate::config::{self, CACHE_VERSION};
|
||||
use crate::events::EventManager;
|
||||
use crate::model::album::Album;
|
||||
use crate::model::artist::Artist;
|
||||
@@ -70,13 +70,22 @@ impl Library {
|
||||
}
|
||||
|
||||
fn load_cache<T: DeserializeOwned>(&self, cache_path: PathBuf, store: Arc<RwLock<Vec<T>>>) {
|
||||
let saved_cache_version = self.cfg.state().cache_version;
|
||||
if saved_cache_version < CACHE_VERSION {
|
||||
debug!(
|
||||
"Cache version for {:?} has changed from {} to {}, ignoring cache",
|
||||
cache_path, saved_cache_version, CACHE_VERSION
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if let Ok(contents) = std::fs::read_to_string(&cache_path) {
|
||||
debug!("loading cache from {}", cache_path.display());
|
||||
let parsed: Result<Vec<T>, _> = serde_json::from_str(&contents);
|
||||
match parsed {
|
||||
Ok(cache) => {
|
||||
debug!(
|
||||
"cache from {} loaded ({} lists)",
|
||||
"cache from {} loaded ({} items)",
|
||||
cache_path.display(),
|
||||
cache.len()
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user