66import asyncpg
77from dotenv import load_dotenv
88from fastapi import Depends , FastAPI , HTTPException , Query
9+ from fastapi .middleware .gzip import GZipMiddleware
10+ from fastapi_cache import FastAPICache
11+ from fastapi_cache .backends .inmemory import InMemoryBackend
12+ from fastapi_cache .decorator import cache
913
1014load_dotenv ()
1115
@@ -18,10 +22,12 @@ async def lifespan(app: FastAPI):
1822 min_size = 5 ,
1923 max_size = 20
2024 )
25+ FastAPICache .init (InMemoryBackend ())
2126 yield
2227 await app .state .pool .close ()
2328
2429app = FastAPI (lifespan = lifespan )
30+ app .add_middleware (GZipMiddleware , minimum_size = 1000 )
2531
2632async def get_db ():
2733 async with app .state .pool .acquire () as conn :
@@ -34,6 +40,7 @@ async def index():
3440
3541
3642@app .get ("/visits" )
43+ @cache (expire = 300 )
3744async def get_visits (
3845 begin : str = Query (..., description = "Start date in ISO format" ),
3946 end : str = Query (..., description = "End date in ISO format" ),
@@ -57,6 +64,7 @@ async def get_visits(
5764 )
5865
5966@app .get ("/registrations" )
67+ @cache (expire = 300 )
6068async def get_registrations (
6169 begin : str = Query (..., description = "Start date in ISO format" ),
6270 end : str = Query (..., description = "End date in ISO format" ),
0 commit comments