feat: cache genres with 7-day TTL

Genre lists from Qobuz are near-static, so caching them avoids
unnecessary API calls. Added cached_genres table with GENRE_TTL_SECS
(7 days), get/set methods, and wired cache into get_genres() command.
Also updated cleanup_expired and clear_all to include the new table.
This commit is contained in:
vicrodh committed 2026-02-06 06:11:54 -06:00
1 parent a4eb35002c
commit 5b8e9920eb
2 files changed
+97 -2

No files matched your search

+37 -2
View File
@@ -630,14 +630,49 @@ pub async fn get_label(
pub async fn get_genres(
parent_id: Option<u64>,
state: State<'_, AppState>,
cache_state: State<'_, ApiCacheState>,
) -> Result<Vec<crate::api::models::GenreInfo>, String> {
log::debug!("Command: get_genres parent_id={:?}", parent_id);
// Check cache first
{
let guard = cache_state.cache.lock().await;
if let Some(cache) = guard.as_ref() {
match cache.get_genres(parent_id) {
Ok(Some(cached_data)) => {
log::debug!("Cache hit for genres parent_id={:?}", parent_id);
return serde_json::from_str(&cached_data)
.map_err(|e| format!("Failed to parse cached genres: {}", e));
}
Ok(None) => {} // Cache miss
Err(e) => {
log::warn!("Genre cache read error: {}", e);
}
}
}
}
// Cache miss - fetch from API
log::debug!("Cache miss for genres parent_id={:?}, fetching from API", parent_id);
let client = state.client.lock().await;
client
let genres = client
.get_genres(parent_id)
.await
.map_err(|e| e.to_string())
.map_err(|e| e.to_string())?;
// Cache the result
{
let guard = cache_state.cache.lock().await;
if let Some(cache) = guard.as_ref() {
let json = serde_json::to_string(&genres)
.map_err(|e| format!("Failed to serialize genres: {}", e))?;
if let Err(e) = cache.set_genres(parent_id, &json) {
log::warn!("Genre cache write error: {}", e);
}
}
}
Ok(genres)
}
/// Get discover index (home page content: playlists, ideal discography, etc.)