Fer*_*ian 5 python caching google-chrome flask
编辑:想出来,按F12,单击网络,选中"禁用缓存".
我有一个基本的烧瓶服务器,我正在学习d3.问题是chrome给了我一个我正在使用的缓存javascript文件,example.js.
请求方法:GET状态代码:200 OK(来自内存缓存)
服务器本身正在发送一个非缓存的响应,我可以通过直接查看响应来看到:
/static/example.js
我在application.py中添加了这个以防止缓存.
@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)
这是整个代码
import os
import re
from flask import Flask, jsonify, render_template, request, url_for
from flask_jsglue import JSGlue
from flask import send_file
# configure application
app = Flask(__name__)
JSGlue(app)
# prevent cached responses
@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
@app.route("/<string:filename>")
def main(filename):
"""Render file."""
return render_template(filename)
@app.route("/favicon.ico")
def favicon():
filename = 'images/fav.png'
return send_file(filename, mimetype='image/png')
Run Code Online (Sandbox Code Playgroud)
谢谢阅读.
Fer*_*ian 13
要防止来自浏览器的缓存响应: - 按F12或右键单击>检查 - 单击网络选项卡 - 选中"禁用缓存".
要防止来自服务器的缓存响应:将以下定义添加到application.py:
# prevent cached responses
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4966 次 |
| 最近记录: |