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

使用Flask禁用特定页面上的缓存,第1张

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

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

@app.after_requestdef 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'


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5667517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存