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

+60
View File
@@ -61,6 +61,9 @@ impl ApiCacheState {
/// Default TTL for cached items (24 hours)
const DEFAULT_TTL_SECS: i64 = 24 * 60 * 60;
/// TTL for genres — Qobuz rarely updates genre lists (7 days)
const GENRE_TTL_SECS: i64 = 7 * 24 * 60 * 60;
pub struct ApiCache {
conn: Connection,
}
@@ -103,6 +106,13 @@ impl ApiCache {
fetched_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_cached_tracks_fetched ON cached_tracks(fetched_at);
CREATE TABLE IF NOT EXISTS cached_genres (
cache_key TEXT PRIMARY KEY,
data TEXT NOT NULL,
fetched_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_cached_genres_fetched ON cached_genres(fetched_at);
"#,
)
.map_err(|e| format!("Failed to initialize API cache: {}", e))?;
@@ -294,6 +304,46 @@ impl ApiCache {
Ok(())
}
// ============ Genre Cache ============
fn genre_cache_key(parent_id: Option<u64>) -> String {
match parent_id {
Some(id) => format!("parent_{}", id),
None => "root".to_string(),
}
}
/// Get cached genres if they exist and haven't expired
pub fn get_genres(&self, parent_id: Option<u64>) -> Result<Option<String>, String> {
let key = Self::genre_cache_key(parent_id);
let min_fetched_at = Self::current_timestamp() - GENRE_TTL_SECS;
let result: Option<String> = self
.conn
.query_row(
"SELECT data FROM cached_genres WHERE cache_key = ? AND fetched_at > ?",
params![key, min_fetched_at],
|row| row.get(0),
)
.optional()
.map_err(|e| format!("Failed to query cached genres: {}", e))?;
Ok(result)
}
/// Cache a genres response
pub fn set_genres(&self, parent_id: Option<u64>, data: &str) -> Result<(), String> {
let key = Self::genre_cache_key(parent_id);
let fetched_at = Self::current_timestamp();
self.conn
.execute(
"INSERT OR REPLACE INTO cached_genres (cache_key, data, fetched_at) VALUES (?, ?, ?)",
params![key, data, fetched_at],
)
.map_err(|e| format!("Failed to cache genres: {}", e))?;
Ok(())
}
// ============ Maintenance ============
/// Clear all cached artists for a specific locale
@@ -353,6 +403,15 @@ impl ApiCache {
)
.map_err(|e| format!("Failed to cleanup cached tracks: {}", e))?;
let genre_min_fetched_at = Self::current_timestamp() - GENRE_TTL_SECS;
total_deleted += self
.conn
.execute(
"DELETE FROM cached_genres WHERE fetched_at <= ?",
params![genre_min_fetched_at],
)
.map_err(|e| format!("Failed to cleanup cached genres: {}", e))?;
Ok(total_deleted)
}
@@ -364,6 +423,7 @@ impl ApiCache {
DELETE FROM cached_albums;
DELETE FROM cached_artists;
DELETE FROM cached_tracks;
DELETE FROM cached_genres;
"#,
)
.map_err(|e| format!("Failed to clear API cache: {}", e))?;
+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.)