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

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

问题描述

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

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 would be happy, if anyone could explain me why this headers overwriting did not work from the page handler?

谢谢.

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

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