views:

49

answers:

1

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")
+2  A: 

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."

Matthew Flaschen
So to set cache control, just use `headers[]` since these headers are single-valued (you don't want multiple `Pragma` headers to appear, for instance).
David Underhill