fix builtin playlists caching
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 13 Dec 2025 02:26:12 +0000 (03:26 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 13 Dec 2025 02:26:12 +0000 (03:26 +0100)
music_assistant/providers/builtin/__init__.py

index 8069e1b66a6a3c858726b8aa3fe28811fd9b0df1..6e98e4cb9746e53bbb183da7ff5f929717c524cd 100644 (file)
@@ -37,6 +37,7 @@ from music_assistant_models.media_items import (
 from music_assistant_models.streamdetails import StreamDetails
 
 from music_assistant.constants import MASS_LOGO, VARIOUS_ARTISTS_FANART
+from music_assistant.controllers.cache import use_cache
 from music_assistant.helpers.tags import AudioTags, async_parse_tags
 from music_assistant.helpers.uri import parse_uri
 from music_assistant.models.music_provider import MusicProvider
@@ -74,6 +75,8 @@ if TYPE_CHECKING:
     from music_assistant.models import ProviderInstanceType
 
 CACHE_CATEGORY_MEDIA_INFO: Final[int] = 1
+CACHE_CATEGORY_PLAYLISTS: Final[int] = 2
+
 
 SUPPORTED_FEATURES = {
     ProviderFeature.BROWSE,
@@ -134,21 +137,6 @@ class BuiltinProvider(MusicProvider):
         if not await asyncio.to_thread(os.path.exists, self._playlists_dir):
             await asyncio.to_thread(os.mkdir, self._playlists_dir)
         await super().loaded_in_mass()
-        # migrate old image path from absolute to relative
-        # TODO: remove this after 2.5+ release
-        for old_path in (
-            "/usr/local/lib/python3.12/site-packages/music_assistant/server/helpers/resources/",
-            "/app/venv/lib/python3.12/site-packages/music_assistant/server/helpers/resources/",
-            "/Users/marcelvanderveldt/Workdir/music-assistant/core/music_assistant/server/helpers/resources/",
-        ):
-            query = (
-                "UPDATE playlists SET metadata = "
-                f"REPLACE (metadata, '{old_path}', '') "
-                f"WHERE playlists.metadata LIKE '%{old_path}%'"
-            )
-            if self.mass.music.database:
-                await self.mass.music.database.execute(query)
-                await self.mass.music.database.commit()
 
     @property
     def is_streaming_provider(self) -> bool:
@@ -544,6 +532,7 @@ class BuiltinProvider(MusicProvider):
             allow_seek=not is_radio,
         )
 
+    @use_cache(expiration=120, category=CACHE_CATEGORY_PLAYLISTS)
     async def _get_builtin_playlist_random_favorite_tracks(self) -> list[Track]:
         result: list[Track] = []
         res = await self.mass.music.tracks.library_items(
@@ -554,6 +543,7 @@ class BuiltinProvider(MusicProvider):
             result.append(item)
         return result
 
+    @use_cache(expiration=120, category=CACHE_CATEGORY_PLAYLISTS)
     async def _get_builtin_playlist_random_tracks(self) -> list[Track]:
         result: list[Track] = []
         res = await self.mass.music.tracks.library_items(limit=500, order_by="random_play_count")
@@ -562,6 +552,7 @@ class BuiltinProvider(MusicProvider):
             result.append(item)
         return result
 
+    @use_cache(expiration=3600, category=CACHE_CATEGORY_PLAYLISTS)
     async def _get_builtin_playlist_random_album(self) -> list[Track]:
         for in_library_only in (True, False):
             for min_tracks_required in (10, 5, 1):
@@ -578,6 +569,7 @@ class BuiltinProvider(MusicProvider):
                     return tracks
         return []
 
+    @use_cache(expiration=3600, category=CACHE_CATEGORY_PLAYLISTS)
     async def _get_builtin_playlist_random_artist(self) -> list[Track]:
         for in_library_only in (True, False):
             for min_tracks_required in (25, 10, 5, 1):
@@ -596,6 +588,7 @@ class BuiltinProvider(MusicProvider):
                     return tracks
         return []
 
+    @use_cache(expiration=30, category=CACHE_CATEGORY_PLAYLISTS)
     async def _get_builtin_playlist_recently_played(self) -> list[Track]:
         result: list[Track] = []
         recent_tracks = await self.mass.music.recently_played(100, [MediaType.TRACK])
@@ -620,6 +613,7 @@ class BuiltinProvider(MusicProvider):
             result.append(track)
         return result
 
+    @use_cache(expiration=60, category=CACHE_CATEGORY_PLAYLISTS)
     async def _get_builtin_playlist_recently_added_tracks(self) -> list[Track]:
         result: list[Track] = []
         recent_tracks = await self.mass.music.recently_added_tracks(100)