小编典典

使用Flask,如何修改ALL输出的Cache-Control标头?

flask

我尝试使用这个

@app.after_request
def add_header(response):
    response.headers['Cache-Control'] = 'max-age=300'
    return response

但这会导致出现重复的Cache-Control标头。我只想要max-age = 300,而不是max-age = 1209600行!

$ curl -I http://my.url.here/
HTTP/1.1 200 OK
Date: Wed, 16 Apr 2014 14:24:22 GMT
Server: Apache
Cache-Control: max-age=300
Content-Length: 107993
Cache-Control: max-age=1209600
Expires: Wed, 30 Apr 2014 14:24:22 GMT
Content-Type: text/html; charset=utf-8

阅读 768

收藏
2020-04-05

共1个答案

小编典典

使用response.cache_control对象 ; 这是一个ResponseCacheControl()实例,可让你直接设置各种缓存属性。此外,如果已经有一个重复的标题,请确保不要添加重复的标题。

@app.after_request
def add_header(response):
    response.cache_control.max_age = 300
    return response
2020-04-05