Delete specific cache in Flask-Cache or Flask-Caching
Asked Answered
V

2

8

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?

Vishnu answered 23/3, 2016 at 13:49 Comment(1)
In fact, I got same problem. There is function, delete, but the api says ` delete(*args, **kwargs) Proxy function for internal cache object. `Methenamine
V
3

It's in fact pretty easy and I should have tried this before. Like for the cached decorator, you can specify a value in the memoized decorator. But instead of doing this: @app.cache.memoize(cache_memoize_value)

You need to do this @app.cache.memoize(timeout=cache_memoize_value)

Vishnu answered 30/3, 2016 at 13:2 Comment(2)
Do you know how to delete a spercific cache, if I want to cache the viewsMethenamine
Just use cache.delete_memoized(function_with_cache_memoize_decorator, param1, param2) within another function than function_with_cache_memoize_decorator.Cesium
S
12
  • 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.
Schenck answered 29/4, 2018 at 3:28 Comment(1)
I would add for future reference, if you need to delete a particular cached value, you can do it like this: cache.delete_memoized(function_with_cache_decorator, value_to_delete)Croner
V
3

It's in fact pretty easy and I should have tried this before. Like for the cached decorator, you can specify a value in the memoized decorator. But instead of doing this: @app.cache.memoize(cache_memoize_value)

You need to do this @app.cache.memoize(timeout=cache_memoize_value)

Vishnu answered 30/3, 2016 at 13:2 Comment(2)
Do you know how to delete a spercific cache, if I want to cache the viewsMethenamine
Just use cache.delete_memoized(function_with_cache_memoize_decorator, param1, param2) within another function than function_with_cache_memoize_decorator.Cesium

© 2022 - 2024 — McMap. All rights reserved.