Opened 11 years ago

Closed 11 years ago

#5813 closed (fixed)

CacheMiddleware should respect max-age

Reported by: Chris Beaven Owned by: nobody
Component: Core (Cache system) Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This came up in IRC today, someone (K0001) wanted to use the per-site middleware but disable caching for a specific view. It seems like the django.views.decorators.cache.never_cache decorator should work but it wasn't.

The reason is because currently the cache middleware doesn't bother looking to see if an expiry age was set by the response, it just uses the CACHE_MIDDLEWARE_SECONDS setting.

Attachments (1)

cache_middleware_respect_maxage.diff (4.5 KB) - added by Chris Beaven 11 years ago.

Download all attachments as: .zip

Change History (5)

Changed 11 years ago by Chris Beaven

comment:1 Changed 11 years ago by Chris Beaven

For example (assuming you have the middleware turned on):

from django.views.decorators.cache import never_cache, cache_control

def test_cache(request):
    from datetime import datetime
    from django.http import HttpResponse
    return HttpResponse(str(

cached_view = test_cache
no_cache_view = never_cache(test_cache)
short_cache_view = cache_control(max_age=5)(test_cache)

When/if this is committed it should probably be noted on BackwardsIncompatibleChanges that the per-site cache logic has changed.

comment:2 Changed 11 years ago by anonymous

Triage Stage: UnreviewedReady for checkin

comment:3 Changed 11 years ago by Simon G

oops, that was me.

comment:4 Changed 11 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [6736]) Fixed #5813 -- Taught the CacheMiddleware to respect any max-age HTTP header
when setting the expiry time. Thanks, SmileyChris.

Note: See TracTickets for help on using tickets.
Back to Top