You can call cache.delete_memoized
in an after_this_request
handler:
# @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()
Alternately, looking at the code for Flask-Cache it appears it does not handle errors thrown by the view. So you could simply raise the error yourself and handle the exception with a separate handler:
@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()