Python 在 Flask 中禁用缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34066804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling caching in Flask
提问by drsealks
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.
我有一些缓存问题。我正在运行非常小的 web 应用程序,它读取一帧,将其保存到磁盘,然后在浏览器窗口中显示。
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.
我知道,这可能不是最好的解决方案,但每次我用相同的名称保存这个读取帧时,任何浏览器都会缓存它。
I tried to use html meta-tags - no success:
我尝试使用 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" />
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"
This is how I tried to modify resp
headers:
这就是我尝试修改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"
Still both Google Chrome and Safari do caching.
仍然 Google Chrome 和 Safari 都在做缓存。
What might be the problem here?
这里可能有什么问题?
采纳答案by drsealks
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.
谢谢你。
回答by Ilyas
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:
如果你总是遇到同样的问题,那就是 Flask 没有看到 JS 和 CSS 文件中的更新,因为默认情况下,Flask 的 max-age 值为 12 小时。您可以将其设置为 0 来解决这样的问题:
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
Refer to its documentationfor details.
有关详细信息,请参阅其文档。
回答by code-interpreter
A combination of app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 in the python file
python文件中app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0的组合
and hard reload (command + shift + R) of the chrome browser worked for me as it seems Chrome caches the static files
和 Chrome 浏览器的硬重新加载(命令 + shift + R)对我有用,因为 Chrome 似乎缓存了静态文件