MCPcopy
hub / github.com/django/django / process_response

Method process_response

django/middleware/cache.py:85–143  ·  view source on GitHub ↗

Set the cache, if needed.

(self, request, response)

Source from the content-addressed store, hash-verified

83 return hasattr(request, "_cache_update_cache") and request._cache_update_cache
84
85 def process_response(self, request, response):
86 """Set the cache, if needed."""
87 if not self._should_update_cache(request, response):
88 # We don't need to update the cache, just return.
89 return response
90
91 if response.streaming or response.status_code not in (200, 304):
92 return response
93
94 # Don't cache responses that set a user-specific (and maybe security
95 # sensitive) cookie in response to a cookie-less request.
96 if (
97 not request.COOKIES
98 and response.cookies
99 and has_vary_header(response, "Cookie")
100 ):
101 return response
102
103 # Don't cache responses when the Cache-Control header is set to
104 # private, no-cache, or no-store.
105 cache_control = response.get("Cache-Control", ())
106 if any(
107 directive in cache_control
108 for directive in (
109 "private",
110 "no-cache",
111 "no-store",
112 )
113 ):
114 return response
115
116 # Don't cache responses when the Vary header contains '*'.
117 if has_vary_header(response, "*"):
118 return response
119
120 # Page timeout takes precedence over the "max-age" and the default
121 # cache timeout.
122 timeout = self.page_timeout
123 if timeout is None:
124 # The timeout from the "max-age" section of the "Cache-Control"
125 # header takes precedence over the default cache timeout.
126 timeout = get_max_age(response)
127 if timeout is None:
128 timeout = self.cache_timeout
129 elif timeout == 0:
130 # max-age was set to 0, don't cache.
131 return response
132 patch_response_headers(response, timeout)
133 if timeout and response.status_code == 200:
134 cache_key = learn_cache_key(
135 request, response, timeout, self.key_prefix, cache=self.cache
136 )
137 if hasattr(response, "render") and callable(response.render):
138 response.add_post_render_callback(
139 lambda r: self.cache.set(cache_key, r, timeout)
140 )
141 else:
142 self.cache.set(cache_key, response, timeout)

Callers

nothing calls this directly

Calls 9

_should_update_cacheMethod · 0.95
has_vary_headerFunction · 0.90
get_max_ageFunction · 0.90
patch_response_headersFunction · 0.90
learn_cache_keyFunction · 0.90
callableFunction · 0.85
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected