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:
parent
d9b08f3e5b
commit
04cb3e8179
2 changed files with 15 additions and 0 deletions
|
|
@ -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:
|
def load_cached_launches(rocket_dir: str) -> StrDict | None:
|
||||||
"""Read the most recent cache of launches."""
|
"""Read the most recent cache of launches."""
|
||||||
filename = get_most_recent_file(rocket_dir, "json")
|
filename = get_most_recent_file(rocket_dir, "json")
|
||||||
|
|
|
||||||
|
|
@ -321,6 +321,9 @@ def update_thespacedevs(config: flask.config.Config) -> None:
|
||||||
existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir)
|
existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir)
|
||||||
assert existing_data
|
assert existing_data
|
||||||
|
|
||||||
|
if agenda.thespacedevs.is_launches_cache_fresh(rocket_dir):
|
||||||
|
return
|
||||||
|
|
||||||
# Update active crewed mission cache used by the launches page.
|
# Update active crewed mission cache used by the launches page.
|
||||||
# Uses the 2-hour TTL; failures are handled internally with cache fallback.
|
# Uses the 2-hour TTL; failures are handled internally with cache fallback.
|
||||||
active_crewed = agenda.thespacedevs.get_active_crewed_flights(rocket_dir)
|
active_crewed = agenda.thespacedevs.get_active_crewed_flights(rocket_dir)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue