Fix scanner cache usage

This commit is contained in:
Zoe Roux 2024-01-08 01:37:34 +01:00
parent 0b55bd7dbb
commit 3d697fbcd0

View File

@ -16,8 +16,6 @@ from providers.types.season import Season
from .utils import batch, log_errors from .utils import batch, log_errors
from .cache import cache, exec_as_cache, make_key from .cache import cache, exec_as_cache, make_key
season_cache = {}
class Scanner: class Scanner:
def __init__( def __init__(
@ -36,6 +34,10 @@ class Scanner:
self.provider = Provider.get_all(client, languages)[0] self.provider = Provider.get_all(client, languages)[0]
self.languages = languages self.languages = languages
self._collection_cache = {}
self._show_cache = {}
self._season_cache = {}
async def scan(self, path: str): async def scan(self, path: str):
logging.info("Starting the scan. It can take some times...") logging.info("Starting the scan. It can take some times...")
self.registered = await self.get_registered_paths() self.registered = await self.get_registered_paths()
@ -122,7 +124,7 @@ class Scanner:
logging.warn("Unknown video file type: %s", raw["type"]) logging.warn("Unknown video file type: %s", raw["type"])
async def create_or_get_collection(self, collection: Collection) -> str: async def create_or_get_collection(self, collection: Collection) -> str:
@cache(ttl=timedelta(days=1)) @cache(ttl=timedelta(days=1), cache=self._collection_cache)
async def create_collection(provider_id: str): async def create_collection(provider_id: str):
# TODO: Check if a collection with the same metadata id exists already on kyoo. # TODO: Check if a collection with the same metadata id exists already on kyoo.
new_collection = ( new_collection = (
@ -150,7 +152,7 @@ class Scanner:
r.raise_for_status() r.raise_for_status()
async def create_or_get_show(self, episode: Episode) -> str: async def create_or_get_show(self, episode: Episode) -> str:
@cache(ttl=timedelta(days=1)) @cache(ttl=timedelta(days=1), cache=self._show_cache)
async def create_show(_: str): async def create_show(_: str):
# TODO: Check if a show with the same metadata id exists already on kyoo. # TODO: Check if a show with the same metadata id exists already on kyoo.
show = ( show = (
@ -164,20 +166,20 @@ class Scanner:
logging.debug("Got show: %s", episode) logging.debug("Got show: %s", episode)
ret = await self.post("show", data=show.to_kyoo()) ret = await self.post("show", data=show.to_kyoo())
async def create_season(season: Season): async def create_season(season: Season, id: str):
try: try:
season.show_id = ret season.show_id = id
return await self.post("seasons", data=season.to_kyoo()) return await self.post("seasons", data=season.to_kyoo())
except Exception as e: except Exception as e:
logging.exception("Unhandled error create a season", exc_info=e) logging.exception("Unhandled error create a season", exc_info=e)
season_tasks = ( season_tasks = map(
exec_as_cache( lambda s: exec_as_cache(
season_cache, self._season_cache,
make_key((ret, s.season_number)), make_key((ret, s.season_number)),
lambda: create_season(s), lambda: create_season(s, ret),
) ),
for s in show.seasons show.seasons,
) )
await asyncio.gather(*season_tasks) await asyncio.gather(*season_tasks)
@ -191,7 +193,7 @@ class Scanner:
self, show: Show | PartialShow, show_id: str, season_number: int self, show: Show | PartialShow, show_id: str, season_number: int
) -> str: ) -> str:
# We use an external season cache because we want to edit this cache programatically # We use an external season cache because we want to edit this cache programatically
@cache(ttl=timedelta(days=1), cache=season_cache) @cache(ttl=timedelta(days=1), cache=self._season_cache)
async def create_season(_: str, __: int): async def create_season(_: str, __: int):
season = await self.provider.identify_season( season = await self.provider.identify_season(
show.external_id[self.provider.name].data_id, season_number show.external_id[self.provider.name].data_id, season_number