禁用Flask中的缓存

drs*_*lks 34 python caching 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" />
Run Code Online (Sandbox Code Playgroud)

另外,我试过这个(烧瓶专用):

resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"
Run Code Online (Sandbox Code Playgroud)

这是我尝试修改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"
Run Code Online (Sandbox Code Playgroud)

Google Chrome和Safari仍然会进行缓存.

这可能是什么问题?

先感谢您

drs*_*lks 54

好,

最后它适用于此:

@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
Run Code Online (Sandbox Code Playgroud)

如果添加此项,则在每个请求完成后调用此函数.请看这里

我很高兴,如果有人能解释我为什么这个标题覆盖不起作用的页面处理程序?

谢谢.

  • 请注意,您将从第二行的第一行覆盖`r.headers ["Cache-Control"]`.因此,你的响应只有``public,max-age = 0'为`Cache-Control`设置 (23认同)
  • 相关:不再需要手动指定标头,而是可以通过Flask 1.0.2访问“ Response”实例的基础“ cache_control”对象,然后设置[`no_cache`](http://werkzeug.pocoo。 org / docs / 0.14 / datastructures /#werkzeug.datastructures.ResponseCacheControl.no_cache)字段更改为“ True”(以及其他相关字段)。参见/sf/answers/1618089301/。 (5认同)
  • 从 MDN 文档看来,“no-store”是唯一需要的东西,并且在设置“Cache-Control”时只放置它是更好的做法:https://developer.mozilla.org/en-US/docs/Web /HTTP/标头/缓存控制。即服务器响应中的“Cache-Control: no-store”以防止缓存。 (3认同)

Ily*_*yas 10

如果您始终遇到相同的问题,则Flask在JS和CSS文件中看不到更新,这是因为默认情况下,Flask的最大寿命值为12小时。您可以将其设置为0以解决以下问题:

app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅其文档

  • 这是一个更简单的解决方案,我需要做的就是 (4认同)