Cache GTFS-RT for 1 minute
This commit is contained in:
parent
ec5720af1d
commit
eba7967c90
|
|
@ -29,12 +29,20 @@ def _assert_region_exists(region_id: str) -> Region:
|
|||
|
||||
return region
|
||||
|
||||
def is_cached(path : str):
|
||||
# File on disk is from the today
|
||||
def is_cached_day(path : str):
|
||||
if not os.path.isfile(path): return False
|
||||
|
||||
timestamp = os.path.getmtime(path)
|
||||
return datetime.fromtimestamp(timestamp).date() == date.today()
|
||||
|
||||
# File on disk is from the last minute
|
||||
def is_cached_1m(path : str):
|
||||
if not os.path.isfile(path): return False
|
||||
|
||||
timestamp = os.path.getmtime(path)
|
||||
return datetime.now() - datetime.fromtimestamp(timestamp) < timedelta(minutes=1)
|
||||
|
||||
@router.get("/region/{region_id}/gtfs",
|
||||
summary="Return GTFS Feed for this region",
|
||||
response_description="GTFS-Feed (zip-file)",
|
||||
|
|
@ -47,11 +55,11 @@ async def get_file(region_id: str, requesting_user: User = Depends(get_current_u
|
|||
verify_permission("gtfs", requesting_user)
|
||||
_assert_region_exists(region_id)
|
||||
file_path = f'data/gtfs/amarillo.{region_id}.gtfs.zip'
|
||||
if is_cached(file_path):
|
||||
logger.info("Returning cached response")
|
||||
if is_cached_day(file_path):
|
||||
# logger.info("Returning cached response")
|
||||
return FileResponse(file_path)
|
||||
|
||||
logger.info("Returning new response")
|
||||
# logger.info("Returning new response")
|
||||
response = requests.get(f"{config.generator_url}/region/{region_id}/gtfs/")
|
||||
# cache response
|
||||
with open(file_path, "wb") as file:
|
||||
|
|
@ -79,7 +87,7 @@ async def get_file(region_id: str, format: str = 'protobuf', requesting_user: Us
|
|||
message = "Specified format is not supported, i.e. neither protobuf nor json."
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message)
|
||||
|
||||
if is_cached(file_path):
|
||||
if is_cached_1m(file_path):
|
||||
logger.info("Returning cached response")
|
||||
return FileResponse(file_path)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue