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 <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2026-03-12 13:28:10 +00:00
parent d9b08f3e5b
commit 04cb3e8179
2 changed files with 15 additions and 0 deletions

View file

@ -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")

View file

@ -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)