From f3efb43ab0f94f6f1e166056a16668e603f9e669 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Sat, 6 Apr 2024 08:26:54 +0100 Subject: [PATCH] wip Signed-off-by: Grant Ramsay --- fastapi_redis_cache/cache.py | 3 +++ tests/live_test.py | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fastapi_redis_cache/cache.py b/fastapi_redis_cache/cache.py index 0491bba..f1d756c 100644 --- a/fastapi_redis_cache/cache.py +++ b/fastapi_redis_cache/cache.py @@ -54,6 +54,7 @@ async def inner_wrapper( request = func_kwargs.pop("request", None) response = func_kwargs.pop("response", None) create_response_directly = not response + if create_response_directly: response = Response() # below fix by @jaepetto on the original repo. @@ -149,6 +150,8 @@ async def inner_wrapper( **kwargs: Any, # noqa: ANN401 ) -> Any: # noqa: ANN401 """Invalidate all cached responses with the same tag.""" + _headers = kwargs.get("request", None).headers + redis_cache = FastApiRedisCache() orig_response = await get_api_response_async(func, *args, **kwargs) diff --git a/tests/live_test.py b/tests/live_test.py index 9bf556d..0b5ba4c 100644 --- a/tests/live_test.py +++ b/tests/live_test.py @@ -99,7 +99,7 @@ def cache_invalid_type(request: Request, response: Response) -> logging.Logger: @app.get("/cache_with_args/{user}") @cache_one_hour(tag="user_tag") -def cache_with_args(user: int) -> dict[str, Union[bool, str]]: +def cache_with_args(user: int, request: Request) -> dict[str, Union[bool, str]]: """Have a varying cache key based on the user argument.""" return { "success": True, @@ -109,7 +109,9 @@ def cache_with_args(user: int) -> dict[str, Union[bool, str]]: @app.put("/cache_with_args/{user}") @expires(tag="user_tag") -def put_cache_with_args(user: int) -> dict[str, Union[bool, str]]: +def put_cache_with_args( + user: int, request: Request +) -> dict[str, Union[bool, str]]: """Put request to change data for a specific user.""" return { "success": True,