Skip to content

Instantly share code, notes, and snippets.

@philipn
Created January 6, 2012 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipn/1569228 to your computer and use it in GitHub Desktop.
Save philipn/1569228 to your computer and use it in GitHub Desktop.
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):
response.add_post_render_callback(
lambda r: self.cache.set(cache_key, r, timeout)
)
else:
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