Fix for offline mode
This commit is contained in:
@@ -196,11 +196,41 @@ impl HybridRepository {
|
||||
#[async_trait]
|
||||
impl MediaRepository for HybridRepository {
|
||||
async fn get_libraries(&self) -> Result<Vec<Library>, RepoError> {
|
||||
// Libraries change infrequently, try cache first with fast timeout
|
||||
let cache_future = self.cache_with_timeout(self.offline.get_libraries());
|
||||
let server_future = self.online.get_libraries();
|
||||
// Cache-first (100ms). On a cache hit, refresh the cache from the server
|
||||
// in the background. On a miss, fetch from the server and persist so the
|
||||
// list is available on the next (possibly offline) startup.
|
||||
let cache_result = self.cache_with_timeout(self.offline.get_libraries()).await;
|
||||
|
||||
self.parallel_race(cache_future, server_future).await
|
||||
if let Ok(libs) = &cache_result {
|
||||
if libs.has_content() {
|
||||
debug!("[HybridRepo] Cache hit for libraries, returning immediately");
|
||||
let online = Arc::clone(&self.online);
|
||||
let offline = Arc::clone(&self.offline);
|
||||
tokio::spawn(async move {
|
||||
if let Ok(server_libs) = online.get_libraries().await {
|
||||
if !server_libs.is_empty() {
|
||||
if let Err(e) = offline.save_libraries_to_cache(&server_libs).await {
|
||||
warn!("[HybridRepo] Background library cache update failed: {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return cache_result;
|
||||
}
|
||||
}
|
||||
|
||||
// Cache miss — fetch from server and persist for offline use.
|
||||
match self.online.get_libraries().await {
|
||||
Ok(server_libs) => {
|
||||
if !server_libs.is_empty() {
|
||||
if let Err(e) = self.offline.save_libraries_to_cache(&server_libs).await {
|
||||
warn!("[HybridRepo] Failed to cache {} libraries: {:?}", server_libs.len(), e);
|
||||
}
|
||||
}
|
||||
Ok(server_libs)
|
||||
}
|
||||
Err(e) => cache_result.or(Err(e)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_items(&self, parent_id: &str, options: Option<GetItemsOptions>) -> Result<SearchResult, RepoError> {
|
||||
@@ -377,20 +407,60 @@ impl MediaRepository for HybridRepository {
|
||||
}
|
||||
|
||||
async fn get_genres(&self, parent_id: Option<&str>) -> Result<Vec<Genre>, RepoError> {
|
||||
let offline = Arc::clone(&self.offline);
|
||||
let online = Arc::clone(&self.online);
|
||||
// Cache-first (100ms). On a cache hit, refresh the cached genre catalog
|
||||
// from the server in the background. On a miss, fetch from the server and
|
||||
// persist so the full genre list is available offline. Mirrors
|
||||
// get_libraries — NOT parallel_race, whose "any non-empty cache wins"
|
||||
// rule would pin genres to whatever sparse set the local albums yield.
|
||||
let parent_id_str = parent_id.map(|s| s.to_string());
|
||||
let parent_id_clone = parent_id_str.clone();
|
||||
|
||||
let cache_future = self.cache_with_timeout(async move {
|
||||
offline.get_genres(parent_id_str.as_deref()).await
|
||||
});
|
||||
let cache_offline = Arc::clone(&self.offline);
|
||||
let cache_pid = parent_id_str.clone();
|
||||
let cache_result = self
|
||||
.cache_with_timeout(async move { cache_offline.get_genres(cache_pid.as_deref()).await })
|
||||
.await;
|
||||
|
||||
let server_future = async move {
|
||||
online.get_genres(parent_id_clone.as_deref()).await
|
||||
};
|
||||
if let Ok(genres) = &cache_result {
|
||||
if genres.has_content() {
|
||||
debug!("[HybridRepo] Cache hit for genres, returning immediately");
|
||||
let online = Arc::clone(&self.online);
|
||||
let offline = Arc::clone(&self.offline);
|
||||
let pid = parent_id_str.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Ok(server_genres) = online.get_genres(pid.as_deref()).await {
|
||||
if !server_genres.is_empty() {
|
||||
if let Err(e) =
|
||||
offline.save_genres_to_cache(pid.as_deref(), &server_genres).await
|
||||
{
|
||||
warn!("[HybridRepo] Background genre cache update failed: {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return cache_result;
|
||||
}
|
||||
}
|
||||
|
||||
self.parallel_race(cache_future, server_future).await
|
||||
// Cache miss — fetch from server and persist for offline use.
|
||||
match self.online.get_genres(parent_id_str.as_deref()).await {
|
||||
Ok(server_genres) => {
|
||||
if !server_genres.is_empty() {
|
||||
if let Err(e) = self
|
||||
.offline
|
||||
.save_genres_to_cache(parent_id_str.as_deref(), &server_genres)
|
||||
.await
|
||||
{
|
||||
warn!(
|
||||
"[HybridRepo] Failed to cache {} genres: {:?}",
|
||||
server_genres.len(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(server_genres)
|
||||
}
|
||||
Err(e) => cache_result.or(Err(e)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, query: &str, options: Option<SearchOptions>) -> Result<SearchResult, RepoError> {
|
||||
|
||||
Reference in New Issue
Block a user