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:
@@ -53,9 +53,11 @@ class OasisCloudClient:
|
||||
self._owns_session = session is None
|
||||
self._access_token = access_token
|
||||
|
||||
now_dt = now()
|
||||
|
||||
# playlists cache
|
||||
self.playlists: list[dict[str, Any]] = []
|
||||
self._playlists_next_refresh = now()
|
||||
self._playlists_cache: dict[bool, list[dict[str, Any]]] = {False: [], True: []}
|
||||
self._playlists_next_refresh = {False: now_dt, True: now_dt}
|
||||
self._playlists_lock = asyncio.Lock()
|
||||
|
||||
self._playlist_details: dict[int, dict[str, str]] = {}
|
||||
@@ -65,6 +67,20 @@ class OasisCloudClient:
|
||||
self._software_next_refresh = now()
|
||||
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
|
||||
def session(self) -> ClientSession:
|
||||
"""
|
||||
@@ -175,16 +191,18 @@ class OasisCloudClient:
|
||||
Returns:
|
||||
`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():
|
||||
return self.playlists
|
||||
return self._playlists_cache[personal_only]
|
||||
|
||||
async with self._playlists_lock:
|
||||
# Double-check in case another task just refreshed it
|
||||
now_dt = now()
|
||||
if _is_cache_valid():
|
||||
return self.playlists
|
||||
return self._playlists_cache[personal_only]
|
||||
|
||||
params = {"my_playlists": str(personal_only).lower()}
|
||||
playlists = await self._async_auth_request(
|
||||
@@ -194,10 +212,12 @@ class OasisCloudClient:
|
||||
if not isinstance(playlists, list):
|
||||
playlists = []
|
||||
|
||||
self.playlists = playlists
|
||||
self._playlists_next_refresh = now_dt + PLAYLISTS_REFRESH_LIMITER
|
||||
self._playlists_cache[personal_only] = playlists
|
||||
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:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user