Skip to content

Commit 31a84e5

Browse files
committed
feat(routes): add in-memory cache
1 parent f5d82d6 commit 31a84e5

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
# ------------------------------------------------------------------------------
44

55
from fastapi import FastAPI
6+
from fastapi_cache import FastAPICache
7+
from fastapi_cache.backends.inmemory import InMemoryBackend
68
from routes import player_route
9+
from contextlib import asynccontextmanager
710

8-
app = FastAPI()
11+
12+
@asynccontextmanager
13+
async def lifespan(app: FastAPI):
14+
FastAPICache.init(InMemoryBackend())
15+
yield
16+
17+
app = FastAPI(lifespan=lifespan)
918
app.include_router(player_route.api_router)

requirements.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,75 @@
1+
aiohttp==3.9.5
2+
aiosignal==1.3.1
13
annotated-types==0.7.0
24
anyio==4.4.0
5+
attrs==23.2.0
36
certifi==2024.6.2
7+
charset-normalizer==3.3.2
48
click==8.1.7
9+
colorclass==2.2.2
510
coverage==7.5.3
11+
dnspython==2.6.1
12+
docopt==0.6.2
13+
email_validator==2.1.2
614
fastapi==0.111.0
15+
fastapi-cache2==0.2.1
16+
fastapi-cli==0.0.4
717
flake8==7.1.0
18+
frozenlist==1.4.1
819
greenlet==3.0.3
920
h11==0.14.0
1021
httpcore==1.0.5
1122
httptools==0.6.1
1223
httpx==0.27.0
1324
idna==3.7
25+
ijson==3.2.3
1426
iniconfig==2.0.0
27+
Jinja2==3.1.4
28+
markdown-it-py==3.0.0
29+
MarkupSafe==2.1.5
1530
mccabe==0.7.0
31+
mdurl==0.1.2
32+
multidict==6.0.5
33+
orjson==3.10.5
1634
packaging==24.1
35+
pendulum==3.0.0
1736
pluggy==1.5.0
37+
pur==7.3.1
1838
pycodestyle==2.12.0
1939
pydantic==2.7.4
2040
pydantic_core
41+
pydeps==1.12.19
2142
pyflakes==3.2.0
43+
Pygments==2.18.0
2244
pytest==8.2.2
2345
pytest-cov==5.0.0
2446
pytest-sugar==1.0.0
47+
python-dateutil==2.9.0.post0
2548
python-dotenv==1.0.1
49+
python-multipart==0.0.9
2650
PyYAML==6.0.1
51+
requests==2.31.0
52+
responses==0.21.0
53+
rfc3986==1.5.0
54+
rich==13.7.1
55+
setuptools==69.5.1
56+
shellingham==1.5.4
57+
six==1.16.0
2758
sniffio==1.3.1
2859
SQLAlchemy==2.0.30
2960
starlette
61+
stdlib-list==0.10.0
62+
termcolor==2.4.0
63+
terminaltables==3.1.10
64+
time-machine==2.14.1
65+
tree-sitter==0.20.4
66+
typer==0.12.3
3067
typing_extensions==4.12.2
68+
tzdata==2024.1
69+
ujson==5.10.0
70+
urllib3==2.2.1
3171
uvicorn==0.30.1
3272
uvloop==0.19.0
3373
watchfiles==0.22.0
3474
websockets==12.0
75+
yarl==1.9.4

routes/player_route.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
from data.player_database import OrmSession
99
from models.player_model import PlayerModel
1010
from services import player_service
11+
from fastapi_cache import FastAPICache
12+
from fastapi_cache.decorator import cache
1113

1214
api_router = APIRouter()
1315

16+
CACHING_TIME_IN_SECONDS = 600
1417

1518
# https://fastapi.tiangolo.com/tutorial/sql-databases/#create-a-dependency
19+
20+
1621
def get_orm_session():
1722
orm_session = OrmSession()
1823
try:
@@ -39,6 +44,8 @@ def post(
3944

4045
player_service.create(orm_session, player_model)
4146

47+
FastAPICache.clear()
48+
4249
# GET --------------------------------------------------------------------------
4350

4451

@@ -48,6 +55,7 @@ def post(
4855
status_code=status.HTTP_200_OK,
4956
summary="Retrieves a collection of Players"
5057
)
58+
@cache(expire=CACHING_TIME_IN_SECONDS)
5159
def get_all(
5260
orm_session: Session = Depends(get_orm_session)
5361
):
@@ -62,6 +70,7 @@ def get_all(
6270
status_code=status.HTTP_200_OK,
6371
summary="Retrieves a Player by its Id"
6472
)
73+
@cache(expire=CACHING_TIME_IN_SECONDS)
6574
def get_by_id(
6675
player_id: int = Path(..., title="The Id of the Player"),
6776
orm_session: Session = Depends(get_orm_session)
@@ -80,6 +89,7 @@ def get_by_id(
8089
status_code=status.HTTP_200_OK,
8190
summary="Retrieves a Player by its Squad Number"
8291
)
92+
@cache(expire=CACHING_TIME_IN_SECONDS)
8393
def get_by_squad_number(
8494
squad_number: int = Path(..., title="The Squad Number of the Player"),
8595
orm_session: Session = Depends(get_orm_session)
@@ -112,6 +122,8 @@ def put(
112122

113123
player_service.update(orm_session, player_model)
114124

125+
FastAPICache.clear()
126+
115127
# DELETE -----------------------------------------------------------------------
116128

117129

@@ -130,3 +142,5 @@ def delete(
130142
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Player not found")
131143

132144
player_service.delete(orm_session, player_id)
145+
146+
FastAPICache.clear()

0 commit comments

Comments
 (0)