Review
Cache user lookups in request path
Profile pages hit the users table 5-6 times per request. Adds a small cache in front of get_user.
FastAPITier 2bugsperformance
Click a line to flag it, pick one or more labels, then submit. If the change looks correct, approve it.
app/services/users.py+6-2
| 8 | - | def get_user(user_id: int) -> User: | |
| 9 | - | return db.query(User).get(user_id) | |
| 8 | + | def get_user(user_id: int, cache: dict = {}) -> User: | |
| 9 | + | if user_id in cache: | |
| 10 | + | return cache[user_id] | |
| 11 | + | user = db.query(User).get(user_id) | |
| 12 | + | cache[user_id] = user | |
| 13 | + | return user |