From 04cb3e8179113553c63330a8bc77b8cbb4a662cb Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 12 Mar 2026 13:28:10 +0000 Subject: [PATCH] Add 2-hour TTL cache check to SpaceDevs launch updater Prevents hitting the SpaceDevs rate limit by skipping the API call when the launches cache is still fresh (< 2 hours old), matching the same TTL pattern already used for the active crewed flights cache. Co-Authored-By: Claude Sonnet 4.6 --- agenda/thespacedevs.py | 12 ++++++++++++ update.py | 3 +++ 2 files changed, 15 insertions(+) diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index 5b5cbf9..648d4ea 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -322,6 +322,18 @@ def summarize_launch(launch: Launch) -> Summary: } +def is_launches_cache_fresh(rocket_dir: str) -> bool: + """Return True if the launches cache is younger than the TTL.""" + now = datetime.now() + existing = [ + x for x in (filename_timestamp(f, "json") for f in os.listdir(rocket_dir)) if x + ] + if not existing: + return False + existing.sort(reverse=True) + return (now - existing[0][0]).total_seconds() <= ttl + + def load_cached_launches(rocket_dir: str) -> StrDict | None: """Read the most recent cache of launches.""" filename = get_most_recent_file(rocket_dir, "json") diff --git a/update.py b/update.py index 97e4d41..f6e5ef3 100755 --- a/update.py +++ b/update.py @@ -321,6 +321,9 @@ def update_thespacedevs(config: flask.config.Config) -> None: existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir) assert existing_data + if agenda.thespacedevs.is_launches_cache_fresh(rocket_dir): + return + # Update active crewed mission cache used by the launches page. # Uses the 2-hour TTL; failures are handled internally with cache fallback. active_crewed = agenda.thespacedevs.get_active_crewed_flights(rocket_dir)