feat(core, slint): wire MusicBrainz cache for metadata and relationships

QbzCore gains an Arc<Mutex<Option<MusicBrainzCache>>> field plus a
`set_musicbrainz_cache(cache)` setter so the frontend (which owns
the data-dir path) can install the cache after construction.

musicbrainz_get_artist_metadata and musicbrainz_get_artist_relation-
ships now check the cache before fetching and persist successful
results on the way out — mirrors the v2_musicbrainz_get_artist_*
behavior in src-tauri/src/commands_v2/integrations.rs. Missing cache
degrades gracefully: methods skip the lookup and the persist hook
silently when no cache is set.

qbz-slint opens the cache at startup at <data-dir>/qbz/cache/
musicbrainz_cache.db (same path Tauri uses) and hands it to the
core. Failure to open the directory or the SQLite file just falls
back to direct network calls — a warn line in the log surfaces it,
but the sidebar still works.

cc
This commit is contained in:
vicrodh committed 2026-05-20 10:58:53 -06:00
1 parent 5c9de3e4d1
commit cccb0c2af0
2 files changed
+71 -2

No files matched your search

+49 -2
View File
@@ -14,6 +14,7 @@ use qbz_models::{
RepeatMode, SearchAllResults, SearchResultsPage, StreamUrl, Track, TracksContainer,
UserSession,
};
use qbz_integrations::musicbrainz::cache::MusicBrainzCache;
use qbz_integrations::musicbrainz::{
location, ArtistMetadata, ArtistRelationships, DiscoveryArtist, DiscoveryResponse,
MusicBrainzClient, Period, RelatedArtist, ResolvedArtist,
@@ -142,6 +143,10 @@ pub struct QbzCore<A: FrontendAdapter> {
player: Arc<Player>,
/// MusicBrainz client (always present; enable/disable toggle lives inside)
musicbrainz: Arc<MusicBrainzClient>,
/// Persistent MB cache. Opened by the frontend (which owns the
/// data-dir path) via `set_musicbrainz_cache`. Methods read the
/// cache before hitting the network and persist on miss.
musicbrainz_cache: Arc<std::sync::Mutex<Option<MusicBrainzCache>>>,
/// Whether the core is initialized
initialized: Arc<RwLock<bool>>,
}
@@ -158,10 +163,21 @@ impl<A: FrontendAdapter + Send + Sync + 'static> QbzCore<A> {
queue: Arc::new(RwLock::new(QueueManager::new())),
player: Arc::new(player),
musicbrainz: Arc::new(MusicBrainzClient::new()),
musicbrainz_cache: Arc::new(std::sync::Mutex::new(None)),
initialized: Arc::new(RwLock::new(false)),
}
}
/// Install a MusicBrainz cache. The frontend owns the data path
/// and opens the cache; QbzCore just stores the handle and uses
/// it transparently in `musicbrainz_get_artist_metadata` and
/// `musicbrainz_get_artist_relationships`.
pub fn set_musicbrainz_cache(&self, cache: MusicBrainzCache) {
if let Ok(mut guard) = self.musicbrainz_cache.lock() {
*guard = Some(cache);
}
}
/// Initialize the core
///
/// This should be called once at startup to set up all subsystems.
@@ -1426,6 +1442,15 @@ impl<A: FrontendAdapter + Send + Sync + 'static> QbzCore<A> {
&self,
mbid: &str,
) -> Result<ArtistMetadata, CoreError> {
// Cache lookup — same behavior as Tauri's v2 command.
if let Ok(guard) = self.musicbrainz_cache.lock() {
if let Some(cache) = guard.as_ref() {
if let Ok(Some(cached)) = cache.get_artist_metadata(mbid) {
return Ok(cached);
}
}
}
let artist = self
.musicbrainz
.get_artist_with_relations(mbid)
@@ -1448,6 +1473,12 @@ impl<A: FrontendAdapter + Send + Sync + 'static> QbzCore<A> {
}
}
if let Ok(guard) = self.musicbrainz_cache.lock() {
if let Some(cache) = guard.as_ref() {
let _ = cache.set_artist_metadata(mbid, &metadata);
}
}
Ok(metadata)
}
@@ -1460,6 +1491,14 @@ impl<A: FrontendAdapter + Send + Sync + 'static> QbzCore<A> {
&self,
mbid: &str,
) -> Result<ArtistRelationships, CoreError> {
if let Ok(guard) = self.musicbrainz_cache.lock() {
if let Some(cache) = guard.as_ref() {
if let Ok(Some(cached)) = cache.get_artist_relations(mbid) {
return Ok(cached);
}
}
}
let artist = self
.musicbrainz
.get_artist_with_relations(mbid)
@@ -1511,12 +1550,20 @@ impl<A: FrontendAdapter + Send + Sync + 'static> QbzCore<A> {
}
}
Ok(ArtistRelationships {
let result = ArtistRelationships {
members,
past_members,
groups,
collaborators,
})
};
if let Ok(guard) = self.musicbrainz_cache.lock() {
if let Some(cache) = guard.as_ref() {
let _ = cache.set_artist_relations(mbid, &result);
}
}
Ok(result)
}
/// "You may also like" tag-based discovery — finds artists that
+22
View File
@@ -459,6 +459,28 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
let app_runtime = Arc::new(AppRuntime::new(SlintAdapter::new(window.as_weak())));
// MusicBrainz cache — opens a SQLite store at
// <data-dir>/qbz/cache/musicbrainz_cache.db so artist metadata
// and relationships persist across sessions (matches Tauri's
// MusicBrainzCache init path). Failure to open just degrades to
// direct network calls — the methods skip the cache when none
// is set.
if let Some(data_dir) = dirs::data_dir() {
let cache_dir = data_dir.join("qbz").join("cache");
if let Err(e) = std::fs::create_dir_all(&cache_dir) {
log::warn!("[qbz-slint] MB cache dir create failed: {e}");
} else {
let db_path = cache_dir.join("musicbrainz_cache.db");
match qbz_integrations::musicbrainz::cache::MusicBrainzCache::new(&db_path) {
Ok(cache) => {
app_runtime.core().set_musicbrainz_cache(cache);
log::info!("[qbz-slint] MB cache opened at {db_path:?}");
}
Err(e) => log::warn!("[qbz-slint] MB cache open failed: {e}"),
}
}
}
// Shared QBZ image cache for album artwork; trim it on startup.
let image_cache = artwork::open_cache();
artwork::spawn_evict(image_cache.clone());