Skip to content

Commit

Permalink
Merge pull request #23 from wednesday-solutions/feature/caching_integ…
Browse files Browse the repository at this point in the history
…ration_v2

fix:Cache Integration V4
  • Loading branch information
himanshu-wedensday authored Mar 12, 2024
2 parents f8ecbbb + 12e7522 commit 1244890
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Settings(BaseSettings):
REDIS_URL: str
SLACK_WEBHOOK_URL: str
ALLOWED_HOSTS: list = ['*']
CACHE_MAX_AGE:int =60
class Config:
env_file = ".env"

Expand Down
10 changes: 3 additions & 7 deletions app/middlewares/cache_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from starlette.concurrency import iterate_in_threadpool
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import StreamingResponse, Response
from app.config.base import settings

from app.wrappers.cache_wrappers import create_cache, retrieve_cache

Expand All @@ -27,7 +28,7 @@ async def dispatch(self, request: Request, call_next) -> Response:
cache_control = request.headers.get('Cache-Control', None)
auth = request.headers.get('Authorization', "token public")
token = auth.split(" ")[1]

max_age=settings.CACHE_MAX_AGE
key = f"{path_url}_{token}"

matches = self.matches_any_path(path_url)
Expand All @@ -46,13 +47,8 @@ async def dispatch(self, request: Request, call_next) -> Response:
if response.status_code == 200:
if cache_control == 'no-store':
return response

if not cache_control:
max_age = 60
elif "max-age" in cache_control:
if "max-age" in cache_control:
max_age = int(cache_control.split("=")[1])
else:
max_age = 60
await create_cache(response_body[0].decode(), key, max_age)
return response

Expand Down

0 comments on commit 1244890

Please sign in to comment.