I have a Flask route structured like so:
@app.route('/rootpath1/<path:path>')
@app.route('/rootpath2/<path:path>', methods=['GET', 'POST'])
@cache.cached()
def rootpath():
...
POSTs to '/rootpath2/' for a given page are typically retrieved from cache (when a cached value is present), which is usually the last GET request.
For example, a user would visit '/rootpath2/myform', fill out and then submit the form. The form would post to '/rootpath2/myform' and the user would be returned to the same URI with a message indicating that the form submission was successful (or that errors occurred, if they did).
The problem here is that a GET always precedes the POST, and the POST always triggers a cache hit and returns that value.
Is there a way for Flask-Cache to distinguish between GETs and POSTs and handle them according (only caching GETs)?
Yes. The
cache
decorator provides anunless
kwarg that accepts a callable. ReturnTrue
from the callable to cancel caching. Test it out with the following: