我在使用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时,我没有得到正确的启用/禁用缓存的方法。有没有一种标准的方法可以让我们完全启用/禁用缓存行为。
发布于 2014-01-03 22:12: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).https://stackoverflow.com/questions/20900281
复制相似问题