【问题标题】:How to disable Flask-Cache caching如何禁用 Flask-Cache 缓存
【发布时间】:2014-01-20 22:10:41
【问题描述】:

我在使用 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 时,我没有获得启用/禁用缓存的正确方法。 有没有一种标准的方法可以让我们完全启用/禁用缓存行为。

【问题讨论】:

    标签: python caching flask flask-extensions flask-cache


    【解决方案1】:

    在初始化 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).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-26
      • 2020-03-20
      • 2016-07-10
      • 2014-01-27
      • 2016-03-08
      • 2016-09-23
      • 1970-01-01
      相关资源
      最近更新 更多