Skip to content

Commit a07cca9

Browse files
569 setup fastapi caching (#603)
# Purpose Closes #569. Set up fastAPI caching for endpoints and create documentation for implementing caching for future endpoints. # New Changes Explain new changes below in short bullet points. - Added fastapi-caching2 to requirements - initialize cache in lifespan.py - add caching to available endpoints Documentation for adding caching to future endpoints as they are developed will be added to the team documentation site # Testing Explain tests that you ran to verify code functionality. - [x] I have unit-tested this PR. Otherwise, explain why it cannot be unit-tested. - [ ] I have tested this PR on a board if the code will run on a board (Only required for firmware developers). - [ ] I have tested this PR by running the ARO website (Only required if the code will impact the ARO website). - [ ] I have tested this PR by running the MCC website (Only required if the code will impact the MCC website). - [x] I have included screenshots of the tests performed below. <img width="944" height="36" alt="image" src="https://github.com/user-attachments/assets/d5ca5fba-80b0-41df-9664-485c4ece999d" /> tests have been run for api, everything passes. The warnings are not related to my changes # Outstanding Changes If there are non-critical changes (i.e. additional features) that can be made to this feature in the future, indicate them here.
1 parent c69676c commit a07cca9

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

gs/backend/api/lifespan.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from contextlib import asynccontextmanager
33

44
from fastapi import FastAPI
5+
from fastapi_cache import FastAPICache
6+
from fastapi_cache.backends.inmemory import InMemoryBackend
57

68
from gs.backend.data.database.engine import get_db_session, setup_database
79
from gs.backend.data.resources.utils import add_main_commands
@@ -10,6 +12,9 @@
1012
@asynccontextmanager
1113
async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]:
1214
"""Lifecycle event for the FastAPI app."""
15+
# Initialize FastAPI Cache (in memory cache)
16+
FastAPICache.init(InMemoryBackend())
17+
1318
# Must all the get_db_session each time when pass it into a separate function.
1419
# Otherwise, will get transaction is inactive error
1520
setup_database(get_db_session())

gs/backend/api/v1/mcc/endpoints/main_commands.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import APIRouter
2+
from fastapi_cache.decorator import cache
23

34
from gs.backend.api.v1.mcc.models.responses import MainCommandsResponse
45
from gs.backend.data.data_wrappers.mcc_wrappers.main_command_wrapper import get_all_main_commands
@@ -7,6 +8,7 @@
78

89

910
@main_commands_router.get("/")
11+
@cache(expire=300) # Cache for 5 minutes
1012
async def get_main_commands() -> MainCommandsResponse:
1113
"""
1214
Gets the main commands that are available for the MCC

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ requests==2.31.0
33
pyserial==3.5
44
skyfield==1.48
55
fastapi[standard]==0.115.0
6+
fastapi-cache2==0.2.1
67
sqlmodel==0.0.22
78
jinja2==3.1.6
89
toml==0.10.2

0 commit comments

Comments
 (0)