cache_control() "max_age" overrides cache_page() "timeout"

47 views
Skip to first unread message

Danny Flack

unread,
Sep 12, 2019, 7:23:10 PM9/12/19
to Django developers (Contributions to Django itself)
It is currently impossible to specify a different client-side cache value than a server-side cache value when using both decorators cache_control and cache_page on a view.

For example:

@cache_control(max_age=60)
@cache_page(timeout=3600)
def my_view():
  return HttpResponse()

This would correctly return the header Cache-Control: max-age=60 but would cache the result, server-side, for 60 seconds instead of the requested 3600 seconds. 

The reason this happens is because UpdateCacheMiddleware.process_response() chooses the "max-age" header over any explicitly set self.cache_timeout value (i.e set from CacheMiddleware which cache_page uses).

My immediate thought would be to have UpdateCacheMiddleware not initially set self.cache_timeout to settings.CACHE_MIDDLEWARE_SECONDS and then, in process_response(), only use "max-age" if self.cache_timeout was indeed None.


Any ideas if this would work? It seems too simple.

Reply all
Reply to author
Forward
0 new messages