summaryrefslogtreecommitdiff
path: root/django/middleware/cache.py
diff options
context:
space:
mode:
authorHonza Král <honza.kral@gmail.com>2010-10-29 01:31:15 +0000
committerHonza Král <honza.kral@gmail.com>2010-10-29 01:31:15 +0000
commitcb17f7ca2252265ab4a844e7924cb8ebab4a1a76 (patch)
tree7aaf1bdfab5ea33a83bab1567b3967f90ed792e8 /django/middleware/cache.py
parent8a724802c5b3b8d4f8d51bcf276838bf0f4ba176 (diff)
downloaddjango-cb17f7ca2252265ab4a844e7924cb8ebab4a1a76.tar.gz
Fixed #14560 -- Enable HEAD requests to be cached properly. Thanks, codemonkey!
Introducing ability to cache HEAD requests and GET requests separately by adding the method to the cache key while preserving the functionality that HEAD requests can use cached reponses generated by a GET request. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14391 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/middleware/cache.py')
-rw-r--r--django/middleware/cache.py21
1 files changed, 12 insertions, 9 deletions
diff --git a/django/middleware/cache.py b/django/middleware/cache.py
index 3f602fe652..a3076acd22 100644
--- a/django/middleware/cache.py
+++ b/django/middleware/cache.py
@@ -34,8 +34,8 @@ More details about how the caching works:
and effective way of avoiding the caching of the Django admin (and any other
user-specific content).
-* This middleware expects that a HEAD request is answered with a response
- exactly like the corresponding GET request.
+* This middleware expects that a HEAD request is answered with the same response
+ headers exactly like the corresponding GET request.
* When a hit occurs, a shallow copy of the original response object is returned
from process_request.
@@ -71,12 +71,6 @@ class UpdateCacheMiddleware(object):
if not hasattr(request, '_cache_update_cache') or not request._cache_update_cache:
# We don't need to update the cache, just return.
return response
- if request.method != 'GET':
- # This is a stronger requirement than above. It is needed
- # because of interactions between this middleware and the
- # HTTPMiddleware, which throws the body of a HEAD-request
- # away before this middleware gets a chance to cache it.
- return response
if not response.status_code == 200:
return response
# Try to get the timeout from the "max-age" section of the "Cache-
@@ -123,16 +117,25 @@ class FetchFromCacheMiddleware(object):
request._cache_update_cache = False
return None # Don't cache requests from authenticated users.
- cache_key = get_cache_key(request, self.key_prefix)
+ # try and get the cached GET response
+ cache_key = get_cache_key(request, self.key_prefix, 'GET')
+
if cache_key is None:
request._cache_update_cache = True
return None # No cache information available, need to rebuild.
response = cache.get(cache_key, None)
+
+ # if it wasn't found and we are looking for a HEAD, try looking just for that
+ if response is None and request.method == 'HEAD':
+ cache_key = get_cache_key(request, self.key_prefix, 'HEAD')
+ response = cache.get(cache_key, None)
+
if response is None:
request._cache_update_cache = True
return None # No cache information available, need to rebuild.
+ # hit, return cached response
request._cache_update_cache = False
return response