Skip to content

Commit

Permalink
Merge pull request #31 from wednesday-solutions/fix/cache_middleware_fix
Browse files Browse the repository at this point in the history
Minor fix in cache_middleware plus optional option added for sentry_dsn
  • Loading branch information
himanshu-wedensday authored Mar 14, 2024
2 parents 36519cb + 0c03556 commit 9dbe83f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/config/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pydantic import BaseSettings


Expand Down Expand Up @@ -25,7 +27,7 @@ class Config:
class Settings(BaseSettings):
SECRET_KEY: str
REDIS_URL: str
SENTRY_DSN: str
SENTRY_DSN: str | None
SLACK_WEBHOOK_URL: str
ALLOWED_HOSTS: list = ["*"]
CACHE_MAX_AGE: int = 60
Expand Down
7 changes: 4 additions & 3 deletions app/middlewares/cache_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ async def dispatch(self, request: Request, call_next) -> Response:
if request_type != "GET":
return await call_next(request)

stored_cache = await retrieve_cache(key)
stored_cache, expire = await retrieve_cache(key)
res = stored_cache and cache_control != "no-cache"

if res:
headers = {"Cache-Control": f"max-age:{stored_cache[1]}"}
return StreamingResponse(iter([stored_cache[0]]), media_type="application/json", headers=headers)
headers = {"Cache-Control": f"max-age:{expire}"}
return StreamingResponse(iter([stored_cache]), media_type="application/json", headers=headers)

response: Response = await call_next(request)
response_body = [chunk async for chunk in response.body_iterator]
response.body_iterator = iterate_in_threadpool(iter(response_body))
Expand Down

1 comment on commit 9dbe83f

@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.py312423%10–61
app/config
   __init__.py2150%4
   base.py32391%44–46
   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.py50500%1–65
   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.py30300%1–40
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
TOTAL6035637% 

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

Please sign in to comment.