Skip to content

Commit

Permalink
Merge pull request #27 from wednesday-solutions/fix/redis-cache
Browse files Browse the repository at this point in the history
fix - utilised our global cache wrapper into get profile api
  • Loading branch information
himanshu-wedensday committed Mar 13, 2024
2 parents d478b01 + b989828 commit f05de51
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions app/daos/users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pickle
import json

from redis import Redis
from fastapi import HTTPException
from sqlalchemy.orm import Session
from app.constants import jwt_utils
Expand All @@ -11,16 +10,16 @@
from fastapi_pagination.ext.sqlalchemy import paginate
from sqlalchemy import select
from app.utils.user_utils import check_existing_field, responseFormatter
from app.wrappers.cache_wrappers import create_cache, retrieve_cache


def get_user(user_id: int, dbSession: Session):
async def get_user(user_id: int, dbSession: Session):
try:
cache_key = f"user:{user_id}"

cached_user = Redis().get(cache_key)
cache_key = f"user_{user_id}"
cached_user, expire = await retrieve_cache(cache_key)
if cached_user:
return pickle.loads(cached_user)
# Check if the subject already exists in the database
return json.loads(cached_user)
# Check if the user already exists in the database
user = (
dbSession.query(User)
.where(User.id == user_id)
Expand All @@ -35,11 +34,10 @@ def get_user(user_id: int, dbSession: Session):
)
.first()
)
if user:
Redis().set(cache_key, pickle.dumps(user))
if not user:
raise Exception(messages["NO_USER_FOUND_FOR_ID"])

await create_cache(json.dumps(user._asdict(), default=str), cache_key, 60)
return user

except Exception as e:
Expand Down
4 changes: 2 additions & 2 deletions app/routes/users/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def login(payload: Login, db: Session = Depends(create_local_session)):


@user_router.get("/{user_id}", tags=["Users"], dependencies=[Depends(get_current_user)], response_model=UserOutResponse)
def profile(
async def profile(
token: Annotated[str, Depends(httpBearerScheme)],
user_id,
db: Session = Depends(create_local_session),
):
print("Request ID:", request_id_contextvar.get())
response = get_user_dao(user_id, dbSession=db)
response = await get_user_dao(user_id, dbSession=db)
return response


Expand Down
Empty file added app/wrappers/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion app/wrappers/cache_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def retrieve_cache(key: str):
redis = await get_redis_pool()
data = await redis.get(key)
if not data:
return None
return None, None
expire = await redis.ttl(key)
return data, expire

Expand Down
Empty file modified scripts/lint_and_format.sh
100644 → 100755
Empty file.

1 comment on commit f05de51

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app
   app.py231822%6–46
app/config
   __init__.py2150%4
   base.py30390%41–43
   celery_config.py16160%1–25
   celery_utils.py20200%1–28
   redis_config.py4325%2–6
app/constants
   jwt_utils.py15150%1–19
app/daos
   home.py880%1–14
   users.py63630%1–115
app/middlewares
   cache_middleware.py42420%1–60
   rate_limiter_middleware.py24240%1–32
   request_id_injection.py16160%1–23
app/models
   __init__.py220%11–13
   users.py25250%1–35
app/routes
   __init__.py11110%1–13
app/routes/cache_router
   __init__.py220%1–3
   cache_samples.py11110%1–15
app/routes/celery_router
   __init__.py220%1–3
   celery_samples.py11110%1–14
app/routes/home
   __init__.py220%1–3
   home.py24240%1–32
app/routes/users
   __init__.py220%1–3
   users.py38380%1–59
app/schemas/users
   users_request.py41410%1–70
   users_response.py880%1–11
app/sessions
   db.py50500%1–75
app/tests
   test_basic.py151313%5–27
app/utils
   exception_handler.py16160%1–32
   redis_utils.py330%1–5
   slack_notification_utils.py13130%1–29
   user_utils.py23230%1–32
app/wrappers
   cache_wrappers.py17170%1–24
TOTAL5795436% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 0.514s ⏱️

Please sign in to comment.