Consider not including data property in Response.__getstate__ for rendered responses #7766
Unanswered
diox
asked this question in
Ideas & Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checklist
master
branch of Django REST framework.Description
Once a
Response
is rendered, the instance contains the data twice:data
property, in the "raw" form that was passed to the Response. Usually adict
or alist
.content
property, in the "final" form that is going to be sent down to the client (at this point content negotiation has found the relevant renderer for the request and used it to render the response)This means that, when caching a
Response
object after it has been rendered, the object is roughly twice as big as it should be. This is a problem when using django'scache_page
decorator as suggested in DRF docs because not only the larger object makes the round-trip unnecessarily longer, but also it can cause some caching backend like Memcached to silently fail to cache the response entirely! With Memcached default configuration objects larger than 1MB are not stored in the cache, and django's Memcached backend hides that failure.Response.__getstate__()
already excludes some properties to prevent them from being cached. As far as I can tell this already prevents a cachedResponse
from being re-rendered since a bunch of properties will be missing, so please consider also excludingdata
to improve cacheability of Response objects (note that__getstate__()
can't be called successfully on non-renderedResponse
s anyway because it inherits from django'sSimpleTemplateResponse
).I realize this is a backwards-incompatible change if people are relying on
data
being present (it can probably mess up view tests that directly look at aResponse
data
and use caching), but I think it's worth it to solve this problem. I'd be happy to write a PR if this issue is accepted (even if the solution is different to what I'm proposing, I'm happy to help implementing anything that fixes the functional problem).Steps to reproduce
Expected behavior
Actual behavior
Beta Was this translation helpful? Give feedback.
All reactions