如何禁用Flask-Cache缓存

17

在使用Flask-Cache时遇到了问题。我需要根据需求进行缓存,并定义一个配置变量,让用户可以设置启用或禁用缓存。

我正在使用Flask-Cache进行缓存,如下:

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

# To initialize cache 
cache.init_app(app)

# clear cache
with app.app_context():
    cache.clear()

并在views.py中使用缓存:

@app.route('/<int:id>', methods=['GET'])

@validate_access(current_user, "read")

@login_required

@cache.memoize()

def get_values(id):
    return get_values()

我不知道在使用Flask-Cache的时候如何正确地启用/禁用缓存。 是否有一种标准的方法可以完全启用/禁用缓存行为。

1个回答

22

只需在初始化 Flask-Cache 之前,将你的 app.config 的 CACHE_TYPE 键设置为 "null"

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).

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接