0

I am wondering if there is a way to allow the user to control the caching properties of a given view using Flask-Cache.

For example, I would like for a view to be cached indefinitely unless the user clicks a reload link, in which case the view would be re-generated. I noticed there is an unless kwarg available to @cached decorator, but I am not sure how one would use this.

It seems that I should be able to add a url_for('this_view', dont_cache=True) somewhere on this_view's Jinja template.

alex
  • 2,968
  • 3
  • 23
  • 25

1 Answers1

2

You can clear the cache; given a view function and the full path to the route, use:

from flask import current_app

with current_app.test_request_context(path=path):
    # the cache key can use `request` so we need to provide the correct
    # context here
    cache_key = view.make_cache_key()

cache.delete(cache_key)

Here path is the path to the view; you could use path = url_for('this_view') to generate it, and view is the (decorated) function object you used @cache.cached() on. cache is the Flask-Cache object.

Once the cache is cleared, a new request to that view will re-generate it.

If you never set a custom key_prefix (callable or string) then the default cache key for a given view is based on the request.path value; you could use this too:

cache_key = 'view/{}'.format(url_for('this_view'))
cache.delete(cache_key)

but the current_app.test_request_context / view.make_cache_key() dance above will make your cache key re-generation more robust.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • i apologize, but i dont understand exactly how to use your suggestion. it may be obvious, but i am new to flask. – alex Aug 14 '14 at 17:36
  • @alex: you'd use this in a separate view; one that you'd call to clear the cache for the cached view. – Martijn Pieters Aug 14 '14 at 17:39
  • ok i see. so your solution is to use 'redirect' view in the middle? link on view1 -> redirect view (deletes cache)-> new view1 – alex Aug 14 '14 at 17:56
  • @alex: not really, if view1 is entirely cached for all possible route parameters, then that means it won't issue a redirect unless that was the cached response to begin with. How you steer your users to the cache clearing view is something you still have to work out. – Martijn Pieters Aug 14 '14 at 18:06
  • @alex: I picked the code in this answer from a recent project where a backend API lets someone clear the cache if backend data has changed; this way the normal views are all heavily cached and highly performant, until the owners of the backend API send a signal that the data on which the view has been based has changed. – Martijn Pieters Aug 14 '14 at 18:07
  • my application is essentially identical. a view is expensive to generate, but want the user to be able to re-generate if they desire. so i have a 'cache clearing view' which redirects the user to the view, and thus re evaluates the original view function. – alex Aug 14 '14 at 20:17
  • what was you solution for indefinite cache timeout? just a large number for `CACHE_DEFAULT_TIMEOUT`? – alex Sep 09 '14 at 15:55
  • @alex: That'd work for 'near' indefinite, yes. Take into account that many backends do not guarantee that the value is going to be cached for that long, just that it won't be cached for longer. – Martijn Pieters Sep 09 '14 at 16:36