@@ -1,14 +1,24 @@
1 1 from fastapi import APIRouter
2 2 from app.db import session
3 + from app.cache import cache_get, cache_set
3 4
4 5 router = APIRouter()
5 6
6 7
7 8 @router.get("/profile")
8 - async def get_profile(user_id: int):
9 + async def get_profile(user_id: int, fields: str = "id,name"):
10 + cached = cache_get("profile")
11 + if cached:
12 + return cached
13 +
14 + try:
9 - row = await session.fetch_one(
10 - "SELECT id, name FROM profiles WHERE user_id = :uid",
11 - {"uid": user_id},
12 - )
15 + row = await session.fetch_one(
16 + f"SELECT {fields} FROM profiles WHERE user_id = {user_id}"
17 + )
18 + except:
19 + return {}
20 +
21 + cache_set("profile", row)
13 22 return row