[英]Disabling caching in 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" />
另外,我嘗試過這個(特定於燒瓶的):
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 都在做緩存。
這里可能有什么問題?
好的,
最后它與這個一起工作:
@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
如果你添加這個,這個函數將在每個請求完成后調用。 請看這里
我會很高興,如果有人能解釋我為什么這個標題覆蓋在頁面處理程序中不起作用?
謝謝你。
如果你總是遇到同樣的問題,那就是 Flask 沒有看到 JS 和 CSS 文件中的更新,因為默認情況下,Flask 的 max-age 值為 12 小時。 您可以將其設置為 0 來解決這樣的問題:
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
有關詳細信息,請參閱其文檔。
python文件中app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0的組合
和 Chrome 瀏覽器的硬重新加載(命令 + shift + R)對我有用,因為 Chrome 似乎緩存了靜態文件
您可以使用 ctrl + F5 繞過緩存
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.