summaryrefslogtreecommitdiff
path: root/django/middleware/cache.py
diff options
context:
space:
mode:
authorFlavio Curella <flavio.curella@gmail.com>2019-09-26 11:41:38 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-01-16 13:39:16 +0100
commitd08d4f464ab11cc226d4e197c0f43e26a7fd2961 (patch)
tree568fce9e1eee95192ad4bf2e24edc3b3573cda3d /django/middleware/cache.py
parent1e0dcd6c8bfa4519c21014c73eb510620dd1a000 (diff)
Fixed #30765 -- Made cache_page decorator take precedence over max-age Cache-Control directive.
Diffstat (limited to 'django/middleware/cache.py')
-rw-r--r--django/middleware/cache.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/django/middleware/cache.py b/django/middleware/cache.py
index 6b320f1db5..9705270b59 100644
--- a/django/middleware/cache.py
+++ b/django/middleware/cache.py
@@ -63,6 +63,7 @@ class UpdateCacheMiddleware(MiddlewareMixin):
"""
def __init__(self, get_response=None):
self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
+ self.page_timeout = None
self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
self.cache = caches[self.cache_alias]
@@ -89,15 +90,18 @@ class UpdateCacheMiddleware(MiddlewareMixin):
if 'private' in response.get('Cache-Control', ()):
return response
- # Try to get the timeout from the "max-age" section of the "Cache-
- # Control" header before reverting to using the default cache_timeout
- # length.
- timeout = get_max_age(response)
+ # Page timeout takes precedence over the "max-age" and the default
+ # cache timeout.
+ timeout = self.page_timeout
if timeout is None:
- timeout = self.cache_timeout
- elif timeout == 0:
- # max-age was set to 0, don't bother caching.
- return response
+ # The timeout from the "max-age" section of the "Cache-Control"
+ # header takes precedence over the default cache timeout.
+ timeout = get_max_age(response)
+ if timeout is None:
+ timeout = self.cache_timeout
+ elif timeout == 0:
+ # max-age was set to 0, don't cache.
+ return response
patch_response_headers(response, timeout)
if timeout and response.status_code == 200:
cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)
@@ -160,7 +164,7 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
Also used as the hook point for the cache decorator, which is generated
using the decorator-from-middleware utility.
"""
- def __init__(self, get_response=None, cache_timeout=None, **kwargs):
+ def __init__(self, get_response=None, cache_timeout=None, page_timeout=None, **kwargs):
self.get_response = get_response
# We need to differentiate between "provided, but using default value",
# and "not provided". If the value is provided using a default, then
@@ -186,4 +190,5 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
if cache_timeout is None:
cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
self.cache_timeout = cache_timeout
+ self.page_timeout = page_timeout
self.cache = caches[self.cache_alias]