This commit is contained in:
@@ -132,6 +132,7 @@ class ScheduleBuilder:
|
||||
self._cache: dict[str, tuple[list[Path], list[float], list[dict], float]] = {}
|
||||
self._ttl_sec = SCHEDULE_CACHE_TTL_SEC
|
||||
self._channel_repo = None # injected
|
||||
self._cache_manager = None # injected for disk persistence
|
||||
|
||||
def invalidate_cache(self) -> None:
|
||||
"""Clear in-memory schedule cache (e.g. when channels.json changes)."""
|
||||
@@ -142,11 +143,15 @@ class ScheduleBuilder:
|
||||
"""Set the channel repository for schedule building."""
|
||||
self._channel_repo = repo
|
||||
|
||||
def set_cache_manager(self, cache_manager) -> None:
|
||||
"""Set the cache manager for disk persistence across restarts."""
|
||||
self._cache_manager = cache_manager
|
||||
|
||||
def get_or_build(
|
||||
self,
|
||||
channel_id: str,
|
||||
) -> tuple[list[Path], list[float], list[dict]] | None:
|
||||
"""Return (paths, durations, path_configs) for the channel. Cached per TTL."""
|
||||
"""Return (paths, durations, path_configs) for the channel. Uses memory and disk cache."""
|
||||
now = time.time()
|
||||
if channel_id in self._cache:
|
||||
paths, durations, path_configs, ts = self._cache[channel_id]
|
||||
@@ -154,6 +159,14 @@ class ScheduleBuilder:
|
||||
logger.debug("schedule cache hit for channel %s (%s videos)", channel_id, len(paths))
|
||||
return paths, durations, path_configs
|
||||
|
||||
if self._cache_manager:
|
||||
cached = self._cache_manager.get_schedule(channel_id)
|
||||
if cached is not None:
|
||||
paths, durations, path_configs = cached
|
||||
self._cache[channel_id] = (paths, durations, path_configs, now)
|
||||
logger.debug("schedule disk cache hit for channel %s (%s videos)", channel_id, len(paths))
|
||||
return paths, durations, path_configs
|
||||
|
||||
logger.info("Building schedule for channel %s ...", channel_id)
|
||||
t0 = time.monotonic()
|
||||
if not self._channel_repo:
|
||||
@@ -167,5 +180,7 @@ class ScheduleBuilder:
|
||||
durations = [get_duration(p) for p in paths]
|
||||
elapsed = time.monotonic() - t0
|
||||
self._cache[channel_id] = (paths, durations, path_configs, now)
|
||||
if self._cache_manager:
|
||||
self._cache_manager.set_schedule(channel_id, paths, durations, path_configs)
|
||||
logger.info("Schedule for channel %s built in %.1fs (%s videos)", channel_id, elapsed, len(paths))
|
||||
return paths, durations, path_configs
|
||||
|
||||
Reference in New Issue
Block a user