如何在Flask中禁用缓存?

我有一些缓存问题。

我正在运行一个很小的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" />

另外,我已经尝试过这一步骤(特定于flask):

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中进行缓存。 这里可能是什么问题?

OK,

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?

Thank you.

If you have always the same problem, that Flask didn't see the updates in JS and CSS files, that because by default, Flask has as max-age value 12 hours. You can set it to 0 to resolve the problem like this:

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

Refer to its documentation for details.

A combination of app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 in the python file

and hard reload (command + shift + R) of the chrome browser worked for me as it seems Chrome caches the static files