禁用Flas中的缓存

2024-10-03 06:26:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些缓存问题。我正在运行一个非常小的web应用程序,它读取一个帧,保存到磁盘,然后在浏览器窗口中显示。

我知道,这可能不是最好的解决方案,但每次我用相同的名称保存这个读取帧,因此任何浏览器都会缓存它。

我尝试使用html元标记-没有成功:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

另外,我试过这个(特定于烧瓶):

resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"

这就是我试图修改resp头的方式:

r = make_response(render_template('video.html', video_info=video_info))

r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"

Google Chrome和Safari都在做缓存。

这里可能有什么问题?

提前谢谢你


Tags: storenohttpcachecontentrespmetacontrol
2条回答

如果您总是遇到同样的问题,那么Flask在JS和CSS文件中看不到更新,因为默认情况下,Flask的最大年龄值为12小时。可以将其设置为0来解决以下问题:

app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

有关详细信息,请参阅its documentation

好的

最后它解决了这个问题:

@app.after_request
def add_header(r):
    """
    Add headers to both force latest IE rendering engine or Chrome Frame,
    and also to cache the rendered page for 10 minutes.
    """
    r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    r.headers["Pragma"] = "no-cache"
    r.headers["Expires"] = "0"
    r.headers['Cache-Control'] = 'public, max-age=0'
    return r

如果添加此项,则在每个请求完成后将调用此函数。请看here

如果有人能解释一下为什么页面处理程序不能覆盖这些标题,我会很高兴的?

谢谢你。

相关问题 更多 >