diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index 1839166..5513223 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -70,12 +70,11 @@ 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: - 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,6 +82,8 @@ 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") @@ -97,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]]] ]: @@ -110,6 +112,7 @@ def cache( :param client_expire: :param injected_dependency_namespace: :param namespace_builder: + :param allow_post: :return: """ @@ -177,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()