From 828b31671c5b12c8bf65c9921c82e4f08a79e9af Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 6 Feb 2026 06:13:14 -0600 Subject: [PATCH] fix: invalidate API cache on favorite mutations add_favorite and remove_favorite now invalidate the corresponding album/track/artist entry in the API cache after a successful API call. This prevents stale JSON (missing the updated favorite status) from being served on the next detail fetch. Added targeted invalidate_album, invalidate_track, and invalidate_artist methods to ApiCache. --- src-tauri/src/api_cache/mod.rs | 35 +++++++++++++++++++ src-tauri/src/commands/favorites.rs | 53 +++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/api_cache/mod.rs b/src-tauri/src/api_cache/mod.rs index 3bc704496..dba59906d 100644 --- a/src-tauri/src/api_cache/mod.rs +++ b/src-tauri/src/api_cache/mod.rs @@ -344,6 +344,41 @@ impl ApiCache { Ok(()) } + // ============ Targeted Invalidation ============ + + /// Invalidate a specific cached album + pub fn invalidate_album(&self, album_id: &str) -> Result<(), String> { + self.conn + .execute( + "DELETE FROM cached_albums WHERE album_id = ?", + params![album_id], + ) + .map_err(|e| format!("Failed to invalidate cached album: {}", e))?; + Ok(()) + } + + /// Invalidate a specific cached track + pub fn invalidate_track(&self, track_id: u64) -> Result<(), String> { + self.conn + .execute( + "DELETE FROM cached_tracks WHERE track_id = ?", + params![track_id], + ) + .map_err(|e| format!("Failed to invalidate cached track: {}", e))?; + Ok(()) + } + + /// Invalidate a specific cached artist (all locales) + pub fn invalidate_artist(&self, artist_id: u64) -> Result<(), String> { + self.conn + .execute( + "DELETE FROM cached_artists WHERE artist_id = ?", + params![artist_id], + ) + .map_err(|e| format!("Failed to invalidate cached artist: {}", e))?; + Ok(()) + } + // ============ Maintenance ============ /// Clear all cached artists for a specific locale diff --git a/src-tauri/src/commands/favorites.rs b/src-tauri/src/commands/favorites.rs index 9b266e9d1..d39b86d3e 100644 --- a/src-tauri/src/commands/favorites.rs +++ b/src-tauri/src/commands/favorites.rs @@ -3,6 +3,7 @@ use serde_json::Value; use tauri::State; +use crate::api_cache::ApiCacheState; use crate::AppState; /// Get user's favorites @@ -30,6 +31,7 @@ pub async fn add_favorite( fav_type: String, item_id: String, state: State<'_, AppState>, + cache_state: State<'_, ApiCacheState>, ) -> Result<(), String> { log::info!("Command: add_favorite type={} id={}", fav_type, item_id); @@ -37,7 +39,30 @@ pub async fn add_favorite( client .add_favorite(&fav_type, &item_id) .await - .map_err(|e| format!("Failed to add favorite: {}", e)) + .map_err(|e| format!("Failed to add favorite: {}", e))?; + + // Invalidate stale cache entry so next fetch gets fresh data + { + let guard = cache_state.cache.lock().await; + if let Some(cache) = guard.as_ref() { + match fav_type.as_str() { + "album" => { let _ = cache.invalidate_album(&item_id); } + "track" => { + if let Ok(id) = item_id.parse::() { + let _ = cache.invalidate_track(id); + } + } + "artist" => { + if let Ok(id) = item_id.parse::() { + let _ = cache.invalidate_artist(id); + } + } + _ => {} + } + } + } + + Ok(()) } /// Remove item from favorites @@ -47,6 +72,7 @@ pub async fn remove_favorite( fav_type: String, item_id: String, state: State<'_, AppState>, + cache_state: State<'_, ApiCacheState>, ) -> Result<(), String> { log::info!("Command: remove_favorite type={} id={}", fav_type, item_id); @@ -54,5 +80,28 @@ pub async fn remove_favorite( client .remove_favorite(&fav_type, &item_id) .await - .map_err(|e| format!("Failed to remove favorite: {}", e)) + .map_err(|e| format!("Failed to remove favorite: {}", e))?; + + // Invalidate stale cache entry so next fetch gets fresh data + { + let guard = cache_state.cache.lock().await; + if let Some(cache) = guard.as_ref() { + match fav_type.as_str() { + "album" => { let _ = cache.invalidate_album(&item_id); } + "track" => { + if let Ok(id) = item_id.parse::() { + let _ = cache.invalidate_track(id); + } + } + "artist" => { + if let Ok(id) = item_id.parse::() { + let _ = cache.invalidate_artist(id); + } + } + _ => {} + } + } + } + + Ok(()) }