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:
1 parent
5b8e9920eb
commit
828b31671c
2 files changed
+86
-2
No files matched your search
@@ -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(())
|
||||
}
|
||||
Reference in new issue
Block a user