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.
This commit is contained in:
vicrodh committed 2026-02-06 06:13:14 -06:00
1 parent 5b8e9920eb
commit 828b31671c
2 files changed
+86 -2

No files matched your search

+35
View File
@@ -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
+51 -2
View File
@@ -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::<u64>() {
let _ = cache.invalidate_track(id);
}
}
"artist" => {
if let Ok(id) = item_id.parse::<u64>() {
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::<u64>() {
let _ = cache.invalidate_track(id);
}
}
"artist" => {
if let Ok(id) = item_id.parse::<u64>() {
let _ = cache.invalidate_artist(id);
}
}
_ => {}
}
}
}
Ok(())
}