⚡️ Use Redis to cache REST responses
This commit is contained in:
@@ -3,6 +3,7 @@ from datetime import datetime
|
||||
from typing import Sequence
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
from backend.idfm_interface import (
|
||||
Destinations as IdfmDestinations,
|
||||
@@ -40,6 +41,7 @@ def optional_datetime_to_ts(dt: datetime | None) -> int | None:
|
||||
|
||||
# TODO: Add limit support
|
||||
@router.get("/")
|
||||
@cache(namespace="stop")
|
||||
async def get_stop(
|
||||
name: str = "", limit: int = 10
|
||||
) -> Sequence[StopAreaSchema | StopSchema] | None:
|
||||
@@ -83,8 +85,8 @@ async def get_stop(
|
||||
return formatted
|
||||
|
||||
|
||||
# TODO: Cache response for 30 secs ?
|
||||
@router.get("/{stop_id}/nextPassages")
|
||||
@cache(namespace="stop-nextPassages", expire=30)
|
||||
async def get_next_passages(stop_id: int) -> NextPassagesSchema | None:
|
||||
res = await idfm_interface.get_next_passages(stop_id)
|
||||
if res is None:
|
||||
@@ -149,6 +151,7 @@ async def get_next_passages(stop_id: int) -> NextPassagesSchema | None:
|
||||
|
||||
|
||||
@router.get("/{stop_id}/destinations")
|
||||
@cache(namespace="stop-destinations", expire=30)
|
||||
async def get_stop_destinations(
|
||||
stop_id: int,
|
||||
) -> IdfmDestinations | None:
|
||||
@@ -158,6 +161,7 @@ async def get_stop_destinations(
|
||||
|
||||
|
||||
@router.get("/{stop_id}/shape")
|
||||
@cache(namespace="stop-shape")
|
||||
async def get_stop_shape(stop_id: int) -> StopShapeSchema | None:
|
||||
connection_area = None
|
||||
|
||||
|
Reference in New Issue
Block a user