Add 24-hour TTL to Eurostar price cache
Cache reads now accept an optional ttl (seconds). get_cached checks the file mtime and returns None if the entry is older than the TTL, triggering a fresh fetch. Eurostar prices use a 24-hour TTL; timetable caches remain indefinite (date-scoped keys become irrelevant once the date passes). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0dee942e16
commit
6b044b9493
4 changed files with 52 additions and 6 deletions
6
cache.py
6
cache.py
|
|
@ -1,5 +1,6 @@
|
|||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
CACHE_DIR = os.path.join(os.path.dirname(__file__), 'cache')
|
||||
|
||||
|
|
@ -9,10 +10,13 @@ def _cache_path(key: str) -> str:
|
|||
return os.path.join(CACHE_DIR, f"{safe_key}.json")
|
||||
|
||||
|
||||
def get_cached(key: str):
|
||||
def get_cached(key: str, ttl: int | None = None):
|
||||
"""Return cached data, or None if missing or older than ttl seconds."""
|
||||
path = _cache_path(key)
|
||||
if not os.path.exists(path):
|
||||
return None
|
||||
if ttl is not None and time.time() - os.path.getmtime(path) > ttl:
|
||||
return None
|
||||
with open(path) as f:
|
||||
return json.load(f)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue