使用Flask禁用特定页面上的缓存 [英] Disable cache on a specific page using Flask

查看:468
本文介绍了使用Flask禁用特定页面上的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板显示作者可以编辑/删除的各种条目。
用户可以删除他们的帖子点击删除



删除后,用户被重定向到条目页面,但该项目仍然存在,页面需要再次重新加载以显示删除效果。
如果我禁用缓存的问题消失了,但我真的想在所有其他页面的缓存...

添加这些标签没有工作,我认为我的浏览器只是忽略它们

 < meta http-equiv =Cache-Controlcontent =no-cache, no-store,must-revalidate/> 
< meta http-equiv =Pragmacontent =no-cache/>
< meta http-equiv =Expirescontent =0/>

我启用缓存槽:

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

有没有一种方法可以禁用特定页面?
$ b 编辑



建议我尝试使用包装:

  def no_cache(f):
def new_func(* args,** kwargs):
resp = make_response(f(* args,** kwargs))
resp.cache_control.no_cache = True
return resp
return update_wrapper(new_func,f)

no_cache装饰,仍然没有运气...

解决方案

只有当没有这样的标题为一个特定页面:

$ p $ @ app.after_request
def add_header(响应):
response.headers ['' ('Cache-Control'not in response.headers):
response.headers ['Cache-Control'] ='IE = Edge,chrome = 1'
'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 - 所以你可以使用它只是我ike:

$ p $ @ app.route('/ page_without_cache')
@no_cache
def page_without_cache( ):
return'hello'


I have a template showing various entries that the author can edit/delete. Users can delete their posts clicking on Delete

After the deletion the user gets redirected to the entries page, but the item is still there, and the page needs to be reloaded again to show the deletion effect. If i disable caching the problem disappears, but i really want to have caches in all the other pages...

adding these tags didn't work, i think my browser just ignores them

<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" />

I'm enabling the cache trough:

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

Is there a way I can disable it for a specific page?

edit

as suggested i tried using a wrapper:

def no_cache(f):
    def new_func(*args, **kwargs):
        resp = make_response(f(*args, **kwargs))
        resp.cache_control.no_cache = True
        return resp
    return update_wrapper(new_func, f)

and wrap the page i want wihtout cache in a @no_cache decorator, still had no luck...

解决方案

You can try adding cache control headers only if there are no such headers for a specific page:

@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

And in your page code - e.g.:

@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'

The point is that you shouldn't override your headers in @app.after_request for all pages - only for those where cache isn't turned off explicitly.

Further, you might want to move the code adding headers to a wrapper such as @no_cache - so you can use it just like that:

 @app.route('/page_without_cache')
 @no_cache
 def page_without_cache():
   return 'hello'

这篇关于使用Flask禁用特定页面上的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆