使用 Flask,如何修改所有输出的 Cache-Control 标头? [英] Using Flask, how do I modify the Cache-Control header for ALL output?
本文介绍了使用 Flask,如何修改所有输出的 Cache-Control 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过用这个
@app.after_request
def add_header(response):
response.headers['Cache-Control'] = 'max-age=300'
return response
但这会导致出现重复的 Cache-Control 标头.我只想要 max-age=300,而不是 max-age=1209600 行!
But this causes a duplicate Cache-Control header to appear. I only want max-age=300, NOT the max-age=1209600 line!
$ 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
推荐答案
使用 response.cache_control
对象;这是一个 ResponseCacheControl()
实例让您直接设置各种缓存属性.此外,如果已经有一个标题,它会确保不添加重复的标题.
Use the response.cache_control
object; this is a ResponseCacheControl()
instance letting you set various cache attributes directly. Moreover, it'll make sure not to add duplicate headers if there is one there already.
@app.after_request
def add_header(response):
response.cache_control.max_age = 300
return response
这篇关于使用 Flask,如何修改所有输出的 Cache-Control 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文