Skip to content

fix: recognize POST method as cacheable #546

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

99sphere
Copy link

@99sphere 99sphere commented Mar 30, 2025

Description

I am using fastapi-cache for my project and encountered a small issue regarding request caching. Currently, the _uncachable function allows caching only for GET requests, and I would like to propose allowing POST requests as well.

Use Case

In my setup, I use a Bastion Host to receive user requests and forward them to the actual server. To reduce unnecessary computations, I want to cache responses for POST requests when the same input is provided multiple times. There are cases where multiple identical requests are sent in a short period, and caching can help prevent redundant processing. However, since _uncachable restricts caching to GET requests, I am unable to achieve this with the current implementation.
Allowing caching for POST requests could be useful when performing idempotent operations based on the user's request.

Proposed Change

I modified the code to allow caching for POST requests and confirmed that it works correctly. If there are no other logical issues, would it be possible to consider making this behavior configurable or allowing caching for POST requests under certain conditions?

Looking forward to your thoughts. Thanks!

@JonathanLifschutz
Copy link

+1 to supporting POST requests. In cases where request payloads are large - such as in analytics dashboards with complex filters or sorts - POST is preferable over GET, even for read-only operations. Supporting caching in these scenarios can significantly improve performance.

@neelvirdy
Copy link

+1, @long2ice could we get help with reviewing this please?

@sleep1223
Copy link

It may also be necessary to modify the default key-builder as it does not include request.body

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

Successfully merging this pull request may close these issues.

4 participants