您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Flask禁用特定页面上的缓存

使用Flask禁用特定页面上的缓存

仅当特定页面没有此类标题时,才可以尝试添加缓存控制标题

@app.after_request
def add_header(response):    
  response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
  if ('Cache-Control' not in response.headers):
    response.headers['Cache-Control'] = 'public, max-age=600'
  return response

在你的页面代码中-例如:

@app.route('/page_without_cache')
def page_without_cache():
   response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
   response.headers['Pragma'] = 'no-cache'
   return 'hello'

重点是,你不应覆盖@app.after_request所有页面标题-仅适用于未明确关闭缓存的页面

此外,你可能希望将添加标头的代码移动到诸如@no_cache- 的包装器中,因此可以像这样使用它:

 @app.route('/page_without_cache')
 @no_cache
 def page_without_cache():
   return 'hello'
Python 2022/1/1 18:24:05 有190人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶