您可以调用cache.delete_memoized
处理after_this_request
程序:
# @cached needs to be invoked before @route
# because @route registers the handler with Flask
# and @cached returns a different function.
# If you @route first and then @cache Flask will
# always execute the *uncached* version.
@app.route('/myview')
@mycache.cached()
def process_view():
try:
return get_something()
except:
@app.after_this_request
def clear_cache(response):
mycache.delete_memoized('process_view')
return response
return print_error_and_no_cache_this_view()
或者,查看 Flask-Cache 的代码,它似乎不处理视图抛出的错误。因此,您可以简单地自己引发错误并使用单独的处理程序处理异常:
@app.route('/myview')
@mycache.cached()
def process_view():
try:
return get_something()
except Exception as e:
raise ProcessViewIssue(e)
@app.errorhandler(ProcessViewIssue)
def handle_process_view_error(e):
return print_error()