1
0
mirror of https://github.com/natekspencer/hacs-oasis_mini.git synced 2025-12-06 18:44:14 -05:00

Cache playlist based on type

This commit is contained in:
Nathan Spencer
2025-11-24 02:06:57 +00:00
parent f0669c7f63
commit c17d1682d0

View File

@@ -53,9 +53,11 @@ class OasisCloudClient:
self._owns_session = session is None self._owns_session = session is None
self._access_token = access_token self._access_token = access_token
now_dt = now()
# playlists cache # playlists cache
self.playlists: list[dict[str, Any]] = [] self._playlists_cache: dict[bool, list[dict[str, Any]]] = {False: [], True: []}
self._playlists_next_refresh = now() self._playlists_next_refresh = {False: now_dt, True: now_dt}
self._playlists_lock = asyncio.Lock() self._playlists_lock = asyncio.Lock()
self._playlist_details: dict[int, dict[str, str]] = {} self._playlist_details: dict[int, dict[str, str]] = {}
@@ -65,6 +67,20 @@ class OasisCloudClient:
self._software_next_refresh = now() self._software_next_refresh = now()
self._software_lock = asyncio.Lock() self._software_lock = asyncio.Lock()
@property
def playlists(self) -> list[dict]:
"""Return all cached playlists, deduplicated by ID."""
seen = set()
merged: list[dict] = []
for items in self._playlists_cache.values():
for pl in items:
if (pid := pl.get("id")) not in seen:
seen.add(pid)
merged.append(pl)
return merged
@property @property
def session(self) -> ClientSession: def session(self) -> ClientSession:
""" """
@@ -175,16 +191,18 @@ class OasisCloudClient:
Returns: Returns:
`true` if the playlists cache contains data and the next refresh timestamp is later than the current time, `false` otherwise. `true` if the playlists cache contains data and the next refresh timestamp is later than the current time, `false` otherwise.
""" """
return self._playlists_next_refresh > now_dt and bool(self.playlists) cache = self._playlists_cache[personal_only]
next_refresh = self._playlists_next_refresh[personal_only]
return bool(cache) and next_refresh > now_dt
if _is_cache_valid(): if _is_cache_valid():
return self.playlists return self._playlists_cache[personal_only]
async with self._playlists_lock: async with self._playlists_lock:
# Double-check in case another task just refreshed it # Double-check in case another task just refreshed it
now_dt = now() now_dt = now()
if _is_cache_valid(): if _is_cache_valid():
return self.playlists return self._playlists_cache[personal_only]
params = {"my_playlists": str(personal_only).lower()} params = {"my_playlists": str(personal_only).lower()}
playlists = await self._async_auth_request( playlists = await self._async_auth_request(
@@ -194,10 +212,12 @@ class OasisCloudClient:
if not isinstance(playlists, list): if not isinstance(playlists, list):
playlists = [] playlists = []
self.playlists = playlists self._playlists_cache[personal_only] = playlists
self._playlists_next_refresh = now_dt + PLAYLISTS_REFRESH_LIMITER self._playlists_next_refresh[personal_only] = (
now_dt + PLAYLISTS_REFRESH_LIMITER
)
return self.playlists return playlists
async def async_get_track_info(self, track_id: int) -> dict[str, Any] | None: async def async_get_track_info(self, track_id: int) -> dict[str, Any] | None:
""" """
@@ -247,10 +267,10 @@ class OasisCloudClient:
Retrieve the latest software metadata from the cloud, using an internal cache to limit requests. Retrieve the latest software metadata from the cloud, using an internal cache to limit requests.
Parameters: Parameters:
force_refresh (bool): If True, bypass the cache and fetch fresh metadata from the cloud. force_refresh (bool): If True, bypass the cache and fetch fresh metadata from the cloud.
Returns: Returns:
details (dict[str, int | str] | None): A mapping of software metadata keys to integer or string values, or `None` if no metadata is available. details (dict[str, int | str] | None): A mapping of software metadata keys to integer or string values, or `None` if no metadata is available.
""" """
now_dt = now() now_dt = now()