From 12e7522ca6a76c2db8d999277b8c8043bb606693 Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Tue, 12 Mar 2024 17:30:37 +0530 Subject: [PATCH] fix:Cache Integration V4 --- app/config/base.py | 1 + app/middlewares/cache_middleware.py | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/config/base.py b/app/config/base.py index 640a871..f138a8b 100644 --- a/app/config/base.py +++ b/app/config/base.py @@ -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" diff --git a/app/middlewares/cache_middleware.py b/app/middlewares/cache_middleware.py index d9123f4..2bedae1 100644 --- a/app/middlewares/cache_middleware.py +++ b/app/middlewares/cache_middleware.py @@ -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 @@ -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) @@ -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