GreedyBear icon indicating copy to clipboard operation
GreedyBear copied to clipboard

CachedResponseMixin renders every cached response twice

Open regulartim opened this issue 1 month ago • 2 comments

It seems to me that _store_api_response() in api/mixins.py renders the response twice on every cache miss: once when response.render() is called and once when response.rendered_content is accessed. Can anyone confirm this?

regulartim avatar Oct 12 '26 07:10 regulartim

Confirmed. In DRF, Response.rendered_content is an uncached property — it runs the accepted renderer on every access — and Response.render() itself does self.content = self.rendered_content. So on a cache miss, _store_api_response() renders once inside render() and a second time when it reads rendered_content for the cache payload.

I verified with a counting renderer wrapping JSONRenderer: the current path executes the renderer 2 times; reading response.content (the bytes render() already stored) executes it 1 time and caches the identical payload. So the fix is one line: cache response.content instead of response.rendered_content.

Happy to take this on and put up a small PR if you assign me.

ABHIGYA0205 avatar Oct 12 '26 14:10 ABHIGYA0205

Thanks @ABHIGYA0205 ! I'll assign you! :)

regulartim avatar Oct 13 '26 05:10 regulartim