使用Flask,我如何修改所有输出的Cache-Control头? [英] Using Flask, how do I modify the Cache-Control header for ALL output?

查看:144
本文介绍了使用Flask,我如何修改所有输出的Cache-Control头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



I tried using this

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



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

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 object 对象;这是一个 ResponseCacheControl()实例,让你直接设置各种缓存属性。而且,如果已经存在的话,它将确保不添加重复的标题。

Use the response.cache_control object 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆