Opened 3 weeks ago
Last modified 10 days ago
#37428 assigned New feature
Add support for HTTP QUERY method to CacheMiddleware/@cache_page
| Reported by: | Jacob Walls | Owned by: | Michiel Beijen |
|---|---|---|---|
| Component: | Core (Cache system) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Jonathan Biemond, Michiel Beijen | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Sibling ticket for #37232, which is already merged in main.
#37232 left caching out of scope. We should consider adding support for caching HTTP QUERY methods to the cache middleware if we can answer these questions:
- Cache key: I presume we need to append a hash of the request body to the cache key, without parsing.
- Re-reading: what happens when a view calls
request.read()? This would causeRawPostDataExceptionwhen trying to read the raw body again to calculate the cache key afterward. (Calculating the key after the view could happen on cold-caches.) - Do we let
RequestDataTooBigemanate from the cache middleware instead of the view? This could catch someone who hasexcept RequestDataTooBigin adef post()decorated with@csrf_exemptthat they are converting todef query(). Or do we treat the over-limit case as "don't cache" and let the view run? One way or the other we might need a backward incompatible change notice.
Change History (6)
comment:1 by , 2 weeks ago
| Triage Stage: | Unreviewed → Someday/Maybe |
|---|
comment:2 by , 12 days ago
comment:3 by , 12 days ago
| Owner: | changed from to |
|---|
comment:4 by , 10 days ago
| Triage Stage: | Someday/Maybe → Accepted |
|---|
comment:5 by , 10 days ago
| Description: | modified (diff) |
|---|---|
| Has patch: | set |
Note:
See TracTickets
for help on using tickets.
I'm interested in taking this one. I have a draft implementation stacked on the #37232 branch; this compare view shows only the caching commit:
https://github.com/mbeijen/django/compare/ticket_37232...http-query-caching
On the three open questions:
QUERYrequests the key also hashes theContent-Typeand the raw request body, without parsing it, as RFC 10008 section 2.7 requires.QUERYentries use their own method component in the key, so they never mix with cachedGET/HEADresponses for the same URL (unlikeHEAD, which can be served from a cachedGET).FetchFromCacheMiddleware.process_request()(and socache_page()) readsrequest.bodybefore the view runs. A view that later callsrequest.read()then reads from the copy Django keeps in memory, and on a cold cachelearn_cache_key()in the response phase no longer hitsRawPostDataException. If an earlier middleware already consumed the stream, the request just isn't cached.DATA_UPLOAD_MAX_MEMORY_SIZE, the middleware catchesRequestDataTooBig, marks the request as not cacheable and lets the view run as normal. No new exceptions are raised. Since cachingQUERYis new, I don't think this needs a backwards-incompatible change note, but I'm happy to add one if you see it differently.get_cache_key()/learn_cache_key()called directly on such a request will still raise.I'll open a PR once #37232 has landed, so the diff doesn't include those commits.
AI disclosure: I used Claude Code to draft and rebase the implementation and tests; I reviewed the changes and verified the new tests fail without the fix.