Created
October 26, 2017 13:38
-
-
Save nitobuendia/a2080e20333bc3841bfa9ec81607a86c to your computer and use it in GitHub Desktop.
Django middleware that allows to set up a global cache limit for all your requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Control MAX age for all Django pages.""" | |
from django.conf import settings | |
from django.utils.deprecation import MiddlewareMixin | |
class MaxAgeMiddleware(MiddlewareMixin): | |
"""Set up Cache-Control header for all Django-handled requests.""" | |
def process_response(self, request, response): | |
"""Adds Cache-Control header to response. | |
Args: | |
request: User HTTP request. | |
response: Django HTTP response. | |
Returns: | |
response with Cache-Control set up to value in settings. | |
""" | |
response['Cache-Control'] = 'max-age=%d' % settings.CACHE_CONTROL_MAX_AGE | |
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Your Django settings.py... Only adding to document variable set up.""" | |
# Max cache age in seconds. | |
# Read more in: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control | |
CACHE_CONTROL_MAX_AGE = 604800 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment