From 575816a017af3adebc174e6f38cbbe8237af487f Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Mon, 2 Jun 2025 14:54:56 -0700 Subject: [PATCH 1/3] :bug: Set namespace only if not None --- fastapi_cache/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi_cache/__init__.py b/fastapi_cache/__init__.py index b5f9218..eda2762 100644 --- a/fastapi_cache/__init__.py +++ b/fastapi_cache/__init__.py @@ -108,5 +108,5 @@ async def clear( assert ( # noqa: S101 cls._backend and cls._prefix is not None ), "You must call init first!" - namespace = cls._prefix + (":" + namespace if namespace else "") + namespace = f"{cls._prefix}:{namespace}" if namespace else None return await cls._backend.clear(namespace, key) From ba351a9c196e138280b613598ee9c1a287c46930 Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Tue, 1 Jul 2025 17:22:03 -0700 Subject: [PATCH 2/3] :fire: Allow caching of non-GET requests --- fastapi_cache/decorator.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index 1839166..5f96eb4 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -75,7 +75,6 @@ def _uncacheable(request: Optional[Request]) -> bool: Returns true if: - Caching has been disabled globally - - This is not a GET request - The request has a Cache-Control header with a value of "no-store" or "no-cache" """ @@ -83,8 +82,6 @@ def _uncacheable(request: Optional[Request]) -> bool: return True if request is None: return False - if request.method != "GET": - return True return request.headers.get("Cache-Control") in ("no-store", "no-cache") From 5a046a869667f738e59bfebbaa75df946c36e0fe Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Tue, 1 Jul 2025 17:42:32 -0700 Subject: [PATCH 3/3] :sparkles: Add allow_post --- fastapi_cache/decorator.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index 5f96eb4..5513223 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -70,7 +70,7 @@ def _locate_param( return param -def _uncacheable(request: Optional[Request]) -> bool: +def _uncacheable(request: Optional[Request], allow_post: bool) -> bool: """Determine if this request should not be cached Returns true if: @@ -82,6 +82,10 @@ def _uncacheable(request: Optional[Request]) -> bool: return True if request is None: return False + if request.method == "POST" and allow_post: + return False + if request.method != "GET": + return True return request.headers.get("Cache-Control") in ("no-store", "no-cache") @@ -94,6 +98,7 @@ def cache( injected_dependency_namespace: str = "__fastapi_cache", private: bool = False, client_expire: Optional[int] = None, + allow_post: bool = False, ) -> Callable[ [Callable[P, Awaitable[R]]], Callable[P, Awaitable[Union[R, Response]]] ]: @@ -107,6 +112,7 @@ def cache( :param client_expire: :param injected_dependency_namespace: :param namespace_builder: + :param allow_post: :return: """ @@ -174,7 +180,7 @@ async def ensure_async_func(*args: P.args, **kwargs: P.kwargs) -> R: response_param.name, None ) # type: ignore[assignment] - if _uncacheable(request): + if _uncacheable(request, allow_post): return await ensure_async_func(*args, **kwargs) prefix = FastAPICache.get_prefix()