How to disable cache caching

I am encountering a problem when using Flask-Cache. I need to do caching as needed by specifying a configuration variable that the user can set to enable or disable caching.

I use Flask-Cache for caching since

cache = Cache (config = {'CACHE_TYPE': 'redis'})

app = Flask ( name )

To initialize the cache

cache.init_app (application)

Clear cache

with app.app_context ():

cache.clear() 

And using the cache (in views.py) as

@ app.route ('/', methods = ['GET'])

@validate_access (current_user, "read")

@login_required

@ cache.memoize ()

def get_values ​​(id):

  return get_values() 

I do not get the correct way to enable / disable caching when using Flask-Cache. Is there a standard way by which we can completely enable / disable cache behavior.

+7
python flask caching flask-extensions
source share
1 answer

Just set app.config CACHE_TYPE to "null" before initializing the cache flag:

 app.config["CACHE_TYPE"] = "null" # change to "redis" and restart to cache again # some time later cache.init_app(app) # All caching functions will simply call through # to the wrapped function, with no caching # (since NullCache does not cache). 
+9
source share

All Articles