Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Additional cache control features #7

Merged
merged 3 commits into from
Aug 25, 2023
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
24 changes: 22 additions & 2 deletions fastapi_cache/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ def cache(
key_builder: Optional[KeyBuilder] = None,
namespace: str = "",
injected_dependency_namespace: str = "__fastapi_cache",
private: bool = False,
client_expire: Optional[int] = None,
) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[Union[R, Response]]]]:
"""
cache all function
:param namespace:
:param expire:
:param coder:
:param key_builder:
:param private:
:param client_expire:
:param injected_dependency_namespace:

:return:
"""
Expand Down Expand Up @@ -127,6 +132,8 @@ async def inner(*args: P.args, **kwargs: P.kwargs) -> Union[R, Response]:
nonlocal coder
nonlocal expire
nonlocal key_builder
nonlocal client_expire
nonlocal private

async def ensure_async_func(*args: P.args, **kwargs: P.kwargs) -> R:
"""Run cached sync functions in thread pool just like FastAPI."""
Expand Down Expand Up @@ -157,6 +164,7 @@ async def ensure_async_func(*args: P.args, **kwargs: P.kwargs) -> R:
prefix = FastAPICache.get_prefix()
coder = coder or FastAPICache.get_coder()
expire = expire or FastAPICache.get_expire()
client_expire = client_expire or expire
key_builder = key_builder or FastAPICache.get_key_builder()
backend = FastAPICache.get_backend()
cache_status_header = FastAPICache.get_cache_status_header()
Expand All @@ -182,6 +190,18 @@ async def ensure_async_func(*args: P.args, **kwargs: P.kwargs) -> R:
)
ttl, cached = 0, None

# Determine cache-control value
cache_control = ""
if client_expire == 0:
cache_control.append("no-cache, ")
else:
cache_control.append(f"max-age={client_expire}, ")

if private:
cache_control.append("private, ")

cache_control = cache_control.rstrip(", ")

if cached is None: # cache miss
result = await ensure_async_func(*args, **kwargs)
to_cache = coder.encode(result)
Expand All @@ -197,7 +217,7 @@ async def ensure_async_func(*args: P.args, **kwargs: P.kwargs) -> R:
if response:
response.headers.update(
{
"Cache-Control": f"max-age={expire}",
"Cache-Control": cache_control,
"ETag": f"W/{hash(to_cache)}",
cache_status_header: "MISS",
}
Expand All @@ -208,7 +228,7 @@ async def ensure_async_func(*args: P.args, **kwargs: P.kwargs) -> R:
etag = f"W/{hash(cached)}"
response.headers.update(
{
"Cache-Control": f"max-age={ttl}",
"Cache-Control": cache_control,
"ETag": etag,
cache_status_header: "HIT",
}
Expand Down