fix(cache): clear artist cache on language change
Artist biographies were being cached per locale but not refreshed when user changed language settings. Now automatically clears artist cache when language is changed to force refetch in new language. cp
This commit is contained in:
1 parent
dd1df5bfee
commit
80ad1d45e2
6 files changed
+53
-12
No files matched your search
@@ -230,6 +230,32 @@ impl ApiCache {
|
|||||||
|
|
||||||
// ============ Maintenance ============
|
// ============ Maintenance ============
|
||||||
|
|
||||||
|
/// Clear all cached artists for a specific locale
|
||||||
|
/// This is useful when user changes language and wants fresh data in the new language
|
||||||
|
pub fn clear_artists_by_locale(&self, locale: &str) -> Result<usize, String> {
|
||||||
|
let deleted = self
|
||||||
|
.conn
|
||||||
|
.execute(
|
||||||
|
"DELETE FROM cached_artists WHERE locale = ?",
|
||||||
|
params![locale],
|
||||||
|
)
|
||||||
|
.map_err(|e| format!("Failed to clear cached artists by locale: {}", e))?;
|
||||||
|
|
||||||
|
log::info!("Cleared {} cached artist(s) for locale '{}'", deleted, locale);
|
||||||
|
Ok(deleted)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all cached artists (useful for forcing refresh across all languages)
|
||||||
|
pub fn clear_all_artists(&self) -> Result<usize, String> {
|
||||||
|
let deleted = self
|
||||||
|
.conn
|
||||||
|
.execute("DELETE FROM cached_artists", [])
|
||||||
|
.map_err(|e| format!("Failed to clear all cached artists: {}", e))?;
|
||||||
|
|
||||||
|
log::info!("Cleared {} cached artist(s)", deleted);
|
||||||
|
Ok(deleted)
|
||||||
|
}
|
||||||
|
|
||||||
/// Clear expired entries from all tables
|
/// Clear expired entries from all tables
|
||||||
pub fn cleanup_expired(&self, ttl_secs: Option<i64>) -> Result<usize, String> {
|
pub fn cleanup_expired(&self, ttl_secs: Option<i64>) -> Result<usize, String> {
|
||||||
let ttl = ttl_secs.unwrap_or(DEFAULT_TTL_SECS);
|
let ttl = ttl_secs.unwrap_or(DEFAULT_TTL_SECS);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use tauri::State;
|
use tauri::State;
|
||||||
|
|
||||||
|
use crate::api_cache::ApiCacheState;
|
||||||
use crate::cache::CacheStats;
|
use crate::cache::CacheStats;
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
|
|
||||||
@@ -19,3 +20,11 @@ pub fn clear_cache(state: State<'_, AppState>) -> Result<(), String> {
|
|||||||
state.audio_cache.clear();
|
state.audio_cache.clear();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Clear all cached artists (useful when changing language settings)
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn clear_artist_cache(cache_state: State<'_, ApiCacheState>) -> Result<usize, String> {
|
||||||
|
log::info!("Command: clear_artist_cache");
|
||||||
|
let cache = cache_state.cache.lock().await;
|
||||||
|
cache.clear_all_artists()
|
||||||
|
}
|
||||||
@@ -616,7 +616,6 @@ pub async fn start_legacy_migration(
|
|||||||
let download_root = cache_state.get_cache_path();
|
let download_root = cache_state.get_cache_path();
|
||||||
let qobuz_client = state.client.clone();
|
let qobuz_client = state.client.clone();
|
||||||
let library_db = library_state.db.clone();
|
let library_db = library_state.db.clone();
|
||||||
let app_progress = app_handle.clone();
|
|
||||||
let app_complete = app_handle.clone();
|
let app_complete = app_handle.clone();
|
||||||
|
|
||||||
// Spawn migration task
|
// Spawn migration task
|
||||||
@@ -627,9 +626,6 @@ pub async fn start_legacy_migration(
|
|||||||
download_root,
|
download_root,
|
||||||
qobuz_client,
|
qobuz_client,
|
||||||
library_db,
|
library_db,
|
||||||
move |status| {
|
|
||||||
let _ = app_progress.emit("migration:progress", status);
|
|
||||||
},
|
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
let _ = app_complete.emit("migration:complete", status);
|
let _ = app_complete.emit("migration:complete", status);
|
||||||
|
|||||||
@@ -163,7 +163,6 @@ pub async fn migrate_legacy_downloads(
|
|||||||
download_root: String,
|
download_root: String,
|
||||||
qobuz_client: Arc<Mutex<QobuzClient>>,
|
qobuz_client: Arc<Mutex<QobuzClient>>,
|
||||||
library_db: Arc<Mutex<LibraryDatabase>>,
|
library_db: Arc<Mutex<LibraryDatabase>>,
|
||||||
progress_callback: impl Fn(MigrationStatus) + Send + 'static,
|
|
||||||
) -> MigrationStatus {
|
) -> MigrationStatus {
|
||||||
let total = track_ids.len();
|
let total = track_ids.len();
|
||||||
let mut status = MigrationStatus {
|
let mut status = MigrationStatus {
|
||||||
@@ -173,8 +172,6 @@ pub async fn migrate_legacy_downloads(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
progress_callback(status.clone());
|
|
||||||
|
|
||||||
for track_id in track_ids {
|
for track_id in track_ids {
|
||||||
let legacy_path = tracks_dir.join(format!("{}.flac", track_id));
|
let legacy_path = tracks_dir.join(format!("{}.flac", track_id));
|
||||||
|
|
||||||
@@ -215,7 +212,6 @@ pub async fn migrate_legacy_downloads(
|
|||||||
drop(client_guard);
|
drop(client_guard);
|
||||||
|
|
||||||
status.processed += 1;
|
status.processed += 1;
|
||||||
progress_callback(status.clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
status.in_progress = false;
|
status.in_progress = false;
|
||||||
@@ -228,6 +224,5 @@ pub async fn migrate_legacy_downloads(
|
|||||||
status.failed
|
status.failed
|
||||||
);
|
);
|
||||||
|
|
||||||
progress_callback(status.clone());
|
|
||||||
status
|
status
|
||||||
}
|
}
|
||||||
@@ -331,6 +331,7 @@ pub fn run() {
|
|||||||
// Cache commands
|
// Cache commands
|
||||||
commands::get_cache_stats,
|
commands::get_cache_stats,
|
||||||
commands::clear_cache,
|
commands::clear_cache,
|
||||||
|
commands::clear_artist_cache,
|
||||||
// Last.fm commands
|
// Last.fm commands
|
||||||
commands::lastfm_has_embedded_credentials,
|
commands::lastfm_has_embedded_credentials,
|
||||||
commands::lastfm_has_credentials,
|
commands::lastfm_has_credentials,
|
||||||
|
|||||||
@@ -555,19 +555,33 @@
|
|||||||
localStorage.setItem('qbz-prefer-highest', String(enabled));
|
localStorage.setItem('qbz-prefer-highest', String(enabled));
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleLanguageChange(lang: string) {
|
async function handleLanguageChange(lang: string) {
|
||||||
language = lang;
|
language = lang;
|
||||||
const localeCode = languageToLocale[lang];
|
const localeCode = languageToLocale[lang];
|
||||||
if (localeCode) {
|
if (localeCode) {
|
||||||
// Set specific locale
|
// Set specific locale
|
||||||
setLocale(localeCode);
|
await setLocale(localeCode);
|
||||||
|
// Clear artist cache to force refetch in new language
|
||||||
|
try {
|
||||||
|
await invoke('clear_artist_cache');
|
||||||
|
console.log('Artist cache cleared after language change');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to clear artist cache:', error);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 'Auto' - use browser locale, defaulting to 'en'
|
// 'Auto' - use browser locale, defaulting to 'en'
|
||||||
const browserLocale = navigator.language.split('-')[0];
|
const browserLocale = navigator.language.split('-')[0];
|
||||||
const supportedLocale = ['en', 'es'].includes(browserLocale) ? browserLocale : 'en';
|
const supportedLocale = ['en', 'es'].includes(browserLocale) ? browserLocale : 'en';
|
||||||
setLocale(supportedLocale);
|
await setLocale(supportedLocale);
|
||||||
// Clear the stored locale so it uses browser detection on next load
|
// Clear the stored locale so it uses browser detection on next load
|
||||||
localStorage.removeItem('qbz-locale');
|
localStorage.removeItem('qbz-locale');
|
||||||
|
// Also clear artist cache
|
||||||
|
try {
|
||||||
|
await invoke('clear_artist_cache');
|
||||||
|
console.log('Artist cache cleared after language change');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to clear artist cache:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user