diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/middleware/cache.py | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/middleware/cache.py')
| -rw-r--r-- | django/middleware/cache.py | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/django/middleware/cache.py b/django/middleware/cache.py index c191118183..0fdffe1bbe 100644 --- a/django/middleware/cache.py +++ b/django/middleware/cache.py @@ -46,7 +46,10 @@ More details about how the caching works: from django.conf import settings from django.core.cache import DEFAULT_CACHE_ALIAS, caches from django.utils.cache import ( - get_cache_key, get_max_age, has_vary_header, learn_cache_key, + get_cache_key, + get_max_age, + has_vary_header, + learn_cache_key, patch_response_headers, ) from django.utils.deprecation import MiddlewareMixin @@ -61,6 +64,7 @@ class UpdateCacheMiddleware(MiddlewareMixin): UpdateCacheMiddleware must be the first piece of middleware in MIDDLEWARE so that it'll get called last during the response phase. """ + def __init__(self, get_response): super().__init__(get_response) self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS @@ -73,7 +77,7 @@ class UpdateCacheMiddleware(MiddlewareMixin): return caches[self.cache_alias] def _should_update_cache(self, request, response): - return hasattr(request, '_cache_update_cache') and request._cache_update_cache + return hasattr(request, "_cache_update_cache") and request._cache_update_cache def process_response(self, request, response): """Set the cache, if needed.""" @@ -86,11 +90,15 @@ class UpdateCacheMiddleware(MiddlewareMixin): # Don't cache responses that set a user-specific (and maybe security # sensitive) cookie in response to a cookie-less request. - if not request.COOKIES and response.cookies and has_vary_header(response, 'Cookie'): + if ( + not request.COOKIES + and response.cookies + and has_vary_header(response, "Cookie") + ): return response # Don't cache a response with 'Cache-Control: private' - if 'private' in response.get('Cache-Control', ()): + if "private" in response.get("Cache-Control", ()): return response # Page timeout takes precedence over the "max-age" and the default @@ -107,8 +115,10 @@ class UpdateCacheMiddleware(MiddlewareMixin): 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) - if hasattr(response, 'render') and callable(response.render): + 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) ) @@ -125,6 +135,7 @@ class FetchFromCacheMiddleware(MiddlewareMixin): FetchFromCacheMiddleware must be the last piece of middleware in MIDDLEWARE so that it'll get called last during the request phase. """ + def __init__(self, get_response): super().__init__(get_response) self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX @@ -139,19 +150,21 @@ class FetchFromCacheMiddleware(MiddlewareMixin): Check whether the page is already cached and return the cached version if available. """ - if request.method not in ('GET', 'HEAD'): + if request.method not in ("GET", "HEAD"): request._cache_update_cache = False return None # Don't bother checking the cache. # try and get the cached GET response - cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache) + cache_key = get_cache_key(request, self.key_prefix, "GET", cache=self.cache) if cache_key is None: request._cache_update_cache = True return None # No cache information available, need to rebuild. response = self.cache.get(cache_key) # 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', cache=self.cache) + if response is None and request.method == "HEAD": + cache_key = get_cache_key( + request, self.key_prefix, "HEAD", cache=self.cache + ) response = self.cache.get(cache_key) if response is None: @@ -170,6 +183,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, cache_timeout=None, page_timeout=None, **kwargs): super().__init__(get_response) # We need to differentiate between "provided, but using default value", @@ -178,14 +192,14 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware): # we need to use middleware defaults. try: - key_prefix = kwargs['key_prefix'] + key_prefix = kwargs["key_prefix"] if key_prefix is None: - key_prefix = '' + key_prefix = "" self.key_prefix = key_prefix except KeyError: pass try: - cache_alias = kwargs['cache_alias'] + cache_alias = kwargs["cache_alias"] if cache_alias is None: cache_alias = DEFAULT_CACHE_ALIAS self.cache_alias = cache_alias |
