Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2012 05:52
What would you like to do?
from django.middleware.cache import UpdateCacheMiddleware
from django.utils.cache import learn_cache_key, get_max_age
class UpdateCacheMiddlewareNoHeaders(UpdateCacheMiddleware):
Just like UpdateCacheMiddleware but we don't set cache headers in the
HTTP response.
def process_response(self, request, response):
"""Sets the cache, if needed."""
if not self._should_update_cache(request, response):
# We don't need to update the cache, just return.
return response
if not response.status_code == 200:
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)
if timeout == None:
timeout = self.cache_timeout
elif timeout == 0:
# max-age was set to 0, don't bother caching.
return response
#patch_response_headers(response, timeout)
if timeout:
cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)
if hasattr(response, 'render') and callable(response.render):
lambda r: self.cache.set(cache_key, r, timeout)
self.cache.set(cache_key, response, timeout)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment