Google App Engine - headers[] and headers.add_header() for cache control
Asked Answered
P

1

9

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")
Peduncle answered 25/6, 2010 at 0:16 Comment(0)
C
10

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

Cleaver answered 25/6, 2010 at 0:22 Comment(1)
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).Casillas

© 2022 - 2024 — McMap. All rights reserved.