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:
vicrodh committed 2026-01-15 10:53:49 -06:00
1 parent dd1df5bfee
commit 80ad1d45e2
6 files changed
+53 -12

No files matched your search

+26
View File
@@ -230,6 +230,32 @@ impl ApiCache {
// ============ 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
pub fn cleanup_expired(&self, ttl_secs: Option<i64>) -> Result<usize, String> {
let ttl = ttl_secs.unwrap_or(DEFAULT_TTL_SECS);
+9
View File
@@ -2,6 +2,7 @@
use tauri::State;
use crate::api_cache::ApiCacheState;
use crate::cache::CacheStats;
use crate::AppState;
@@ -19,3 +20,11 @@ pub fn clear_cache(state: State<'_, AppState>) -> Result<(), String> {
state.audio_cache.clear();
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()
}
-4
View File
@@ -616,7 +616,6 @@ pub async fn start_legacy_migration(
let download_root = cache_state.get_cache_path();
let qobuz_client = state.client.clone();
let library_db = library_state.db.clone();
let app_progress = app_handle.clone();
let app_complete = app_handle.clone();
// Spawn migration task
@@ -627,9 +626,6 @@ pub async fn start_legacy_migration(
download_root,
qobuz_client,
library_db,
move |status| {
let _ = app_progress.emit("migration:progress", status);
},
).await;
let _ = app_complete.emit("migration:complete", status);
@@ -163,7 +163,6 @@ pub async fn migrate_legacy_downloads(
download_root: String,
qobuz_client: Arc<Mutex<QobuzClient>>,
library_db: Arc<Mutex<LibraryDatabase>>,
progress_callback: impl Fn(MigrationStatus) + Send + 'static,
) -> MigrationStatus {
let total = track_ids.len();
let mut status = MigrationStatus {
@@ -173,8 +172,6 @@ pub async fn migrate_legacy_downloads(
..Default::default()
};
progress_callback(status.clone());
for track_id in track_ids {
let legacy_path = tracks_dir.join(format!("{}.flac", track_id));
@@ -215,7 +212,6 @@ pub async fn migrate_legacy_downloads(
drop(client_guard);
status.processed += 1;
progress_callback(status.clone());
}
status.in_progress = false;
@@ -228,6 +224,5 @@ pub async fn migrate_legacy_downloads(
status.failed
);
progress_callback(status.clone());
status
}
+1
View File
@@ -331,6 +331,7 @@ pub fn run() {
// Cache commands
commands::get_cache_stats,
commands::clear_cache,
commands::clear_artist_cache,
// Last.fm commands
commands::lastfm_has_embedded_credentials,
commands::lastfm_has_credentials,
+17 -3
View File
@@ -555,19 +555,33 @@
localStorage.setItem('qbz-prefer-highest', String(enabled));
}
function handleLanguageChange(lang: string) {
async function handleLanguageChange(lang: string) {
language = lang;
const localeCode = languageToLocale[lang];
if (localeCode) {
// 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 {
// 'Auto' - use browser locale, defaulting to 'en'
const browserLocale = navigator.language.split('-')[0];
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
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);
}
}
}