Delete specific cache in Flask-Cache or Flask-Caching

Cramix picture Cramix · Mar 23, 2016 · Viewed 8.5k times · Source

I am using Flask cache in my API in python.

Currently I am using the decorator @app.cache.memoize(cache_memoize_value) and I flush it by calling app.cache.delete_memoized(view)

The problem is that with memoize it will be cached for n views and not for a specific amount of time. If I want to specify a timeout for the cache I need to use the decorator @app.cache.cached(timeout=300) and clear it with app.cache.clear(). However, this clear method will clear everything and not only a specific view.

How can I only clear a specific view while using the cached decorator?

Answer

Grey Li picture Grey Li · Apr 29, 2018
  • For cache.cached(), use cache.delete() to delete specific cache, pass the cache key (default to view/<request.path>).
  • For cache.memoize(), use cache.delete_memoized() to delete specific cache, pass the cache key (default to function name with or without args).
  • Use cache.clear() to delete all the cache data.