From cb269883ea710d967f8ddda9cfb7c33853557c0f Mon Sep 17 00:00:00 2001 From: euzu Date: Thu, 16 Oct 2025 21:52:57 +0200 Subject: [PATCH] playlist cache into memory --- README.md | 2 +- backend/src/repository/m3u_repository.rs | 7 ++-- backend/src/repository/playlist_repository.rs | 38 ++++++++++--------- backend/src/repository/xtream_repository.rs | 5 ++- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 103d1dacd..dd14ff2f4 100644 --- a/README.md +++ b/README.md @@ -1473,7 +1473,7 @@ Now you can do `nginx` configuration like } ``` When you use nginx be sure to have -``` +```nginx proxy_redirect off; proxy_buffering off; proxy_request_buffering off; diff --git a/backend/src/repository/m3u_repository.rs b/backend/src/repository/m3u_repository.rs index 259bf1833..12a384e38 100644 --- a/backend/src/repository/m3u_repository.rs +++ b/backend/src/repository/m3u_repository.rs @@ -95,9 +95,10 @@ pub async fn m3u_get_item_for_stream_id(stream_id: u32, app_state: &AppState, ta { if let Some(playlist) = app_state.playlists.data.read().await.get(target.name.as_str()) { if let Some(m3u_playlist) = playlist.m3u.as_ref() { - return Ok(m3u_playlist.query(&stream_id) - .ok_or_else(|| str_to_io_error(&format!("Failed to read m3u item for id {stream_id}")))? - .clone()) + if let Some(item) = m3u_playlist.query(&stream_id) { + return Ok(item.clone()); + } + // fall through to disk lookup on cache miss } } diff --git a/backend/src/repository/playlist_repository.rs b/backend/src/repository/playlist_repository.rs index eb61e2678..7cde916e5 100644 --- a/backend/src/repository/playlist_repository.rs +++ b/backend/src/repository/playlist_repository.rs @@ -68,24 +68,6 @@ pub async fn persist_playlist(app_config: &AppConfig, playlist: &mut [PlaylistGr errors.push(err); } } - - if target.use_memory_cache { - if let Some(playlist_storage) = playlist_state { - match output { - TargetOutput::Xtream(_) => { - if let Ok(storage) = load_xtream_target_storage(app_config, target) { - playlist_storage.cache_playlist(&target.name, PlaylistStorage::XtreamPlaylist(Box::new(storage))).await; - } - }, - TargetOutput::M3u(_) => { - if let Ok(storage) = load_m3u_target_storage(app_config, target) { - playlist_storage.cache_playlist(&target.name, PlaylistStorage::M3uPlaylist(Box::new(storage))).await; - } - }, - _ => {} - } - } - } } Err(err) => errors.push(err) } @@ -96,6 +78,26 @@ pub async fn persist_playlist(app_config: &AppConfig, playlist: &mut [PlaylistGr } drop(file_lock); + if target.use_memory_cache { + if let Some(playlist_storage) = playlist_state { + for output in &target.output { + match output { + TargetOutput::Xtream(_) => { + if let Ok(storage) = load_xtream_target_storage(app_config, target) { + playlist_storage.cache_playlist(&target.name, PlaylistStorage::XtreamPlaylist(Box::new(storage))).await; + } + }, + TargetOutput::M3u(_) => { + if let Ok(storage) = load_m3u_target_storage(app_config, target) { + playlist_storage.cache_playlist(&target.name, PlaylistStorage::M3uPlaylist(Box::new(storage))).await; + } + }, + _ => {} + } + } + } + } + if errors.is_empty() { Ok(()) } else { Err(errors) } } diff --git a/backend/src/repository/xtream_repository.rs b/backend/src/repository/xtream_repository.rs index 694b6161f..c03a434f6 100644 --- a/backend/src/repository/xtream_repository.rs +++ b/backend/src/repository/xtream_repository.rs @@ -415,10 +415,11 @@ pub async fn xtream_get_item_for_stream_id( xtream_cluster: Option, ) -> Result<(XtreamPlaylistItem, VirtualIdRecord), Error> { if target.use_memory_cache { - if let Some((playlist_item, virtual_record)) = - xtream_get_item_for_stream_id_from_memory(virtual_id, app_state, target, xtream_cluster).await? { + if let Ok(Some((playlist_item, virtual_record))) = + xtream_get_item_for_stream_id_from_memory(virtual_id, app_state, target, xtream_cluster).await { return Ok((playlist_item, virtual_record)); } + // fall through to disk lookup on cache miss } let app_config: &AppConfig = &app_state.app_config;