在Flask中禁用缓存 [英] Disabling caching in Flask

查看:574
本文介绍了在Flask中禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些缓存问题。我正在运行一个很小的Web应用程序,该应用程序将读取一帧并将其保存到磁盘,然后在浏览器窗口中显示。

I have some caching issues. I'm running very small web-application which reads one frame, saves it to the disk and then shows it in browsers window.

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

I know, it is probably not the best solution, but every time I save this read frame with the same name and therefor any browser will cache it.

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

I tried to use html meta-tags - no success:

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

此外,我也尝试过此操作(特定于烧瓶):

Also, I have tried this one (flask-specific):

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

这是我尝试修改 resp 标头的方式:

This is how I tried to modify resp headers:

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都可以缓存。

Still both Google Chrome and Safari do caching.

这里可能是什么问题?

推荐答案

好,

最后,它可以这样工作:

finally it worked with this:

@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

如果添加此功能,则每次请求完成后都会调用此函数,请参见此处

If you add this, this function will called after each request done. Please,see here

I会高兴的是,如果有人能解释我为什么页处理程序中的标头覆盖无法正常工作?

I would be happy, if anyone could explain me why this headers overwriting did not work from the page handler?

谢谢。

这篇关于在Flask中禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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