Opened 9 years ago

Closed 9 years ago

#5813 closed (fixed)

CacheMiddleware should respect max-age

Reported by: SmileyChris 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: UI/UX:


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 SmileyChris 9 years ago.

Download all attachments as: .zip

Change History (5)

Changed 9 years ago by SmileyChris

comment:1 Changed 9 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 9 years ago by anonymous

  • Triage Stage changed from Unreviewed to Ready for checkin

comment:3 Changed 9 years ago by Simon G

oops, that was me.

comment:4 Changed 9 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(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