Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions fastapi_cache/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,20 @@ 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"

"""
if not FastAPICache.get_enable():
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")
Expand All @@ -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]]]
]:
Expand All @@ -110,6 +112,7 @@ def cache(
:param client_expire:
:param injected_dependency_namespace:
:param namespace_builder:
:param allow_post:

:return:
"""
Expand Down Expand Up @@ -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()
Expand Down