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

Overriding Cache-Control header #256

Open
joshfermin opened this issue Aug 7, 2023 · 1 comment
Open

Overriding Cache-Control header #256

joshfermin opened this issue Aug 7, 2023 · 1 comment

Comments

@joshfermin
Copy link

Hello, is there any way to override the cache-control header?

I am running into an issue where I need to clear the cache key on update, but since fastapi-cache is setting the cache-control header I am getting a 200 OK (from disk cache) response. I need to be able to override the cache-control header so that we don't keep stale data

@arkhodakov
Copy link

I encountered the same problem. I had to create a simple middleware to override the header without changing the library code.

class RouterCacheControlResetMiddleware(BaseHTTPMiddleware):
    """Disable Response headers Cache-Control (set to 'no-cache').

    The initial reason for this is that the fastapi-cache library sets the max-age param of the header
    equal to the expire parameter that is provided to the caching layer (Redis),
    so the response is also cached on the browser side, which in most cases is unnecessary."""
    async def dispatch(self, request: Request, call_next: Callable) -> Response:
        response: Response = await call_next(request)
        response.headers.update({
            "Cache-Control": "no-cache"
        })
        return response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants