What is the proper way to set cache control?
Sometimes I see the use of headers[]
self.response.headers["Pragma"]="no-cache"
self.response.headers["Cache-Control"]="no-cache, no-store, must-revalidate, pre-check=0, post-check=0"
self.response.headers["Expires"]="Thu, 01 Dec 1994 16:00:00"
Other times, I see headers.add_header()
self.response.headers.add_header("Pragma","no-cache")
self.response.headers.add_header("Cache-Control","no-cache, no-store, must-revalidate, pre-check=0, post-check=0")
self.response.headers.add_header("Expires","Thu, 01 Dec 1994 16:00:00")
And even a mix of both headers[] and headers.add_header()
self.response.headers["Pragma"]="no-cache"
self.response.headers.add_header("Cache-Control","no-cache, no-store, must-revalidate, pre-check=0, post-check=0")
self.response.headers.add_header("Expires","Thu, 01 Dec 1994 16:00:00")
To add HTTP headers to a request, you can simply pass them in a dict to the headers parameter. Similarly, you can also send your own cookies to a server using a dict passed to the cookies parameter.
A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.
Handling requests Any request can be routed to any instance, so consecutive requests from the same user are not necessarily sent to the same instance. An instance can handle multiple requests concurrently. The number of instances can be adjusted automatically as traffic changes.
The difference is that using headers[]
will overwrite previous values, while add_header
won't.
From the wsgiref.headers docs (referred to by the GAE docs), "Setting a header deletes any existing values for that header, then adds a new value at the end of the wrapped header list."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With