【发布时间】:2016-01-06 07:52:09
【问题描述】:
我想使用 Flask-Cache 来缓存不是视图的函数的结果。但是,它似乎只有在我装饰视图功能时才有效。 Flask-Cache 可以用来缓存“普通”函数吗?
如果我装饰视图函数,缓存会起作用。
cache = Cache(app,config={'CACHE_TYPE': 'simple'})
@app.route('/statistics/', methods=['GET'])
@cache.cached(timeout=500, key_prefix='stats')
def statistics():
return render_template('global/application.html') # caching works here
如果我装饰一个“普通”函数并从视图中调用它,它就不起作用。
class Foo(object):
@cache.cached(timeout=10, key_prefix='filters1')
def simple_method(self):
a = 1
return a # caching NOT working here
@app.route('/statistics/filters/', methods=['GET'])
def statistics_filter():
Foo().simple_method()
如果我对这两个函数使用相同的key_prefix,它也可以工作。我认为这是一个线索,表明它自己的缓存正在正确初始化,但我调用简单方法或定义它的方式是错误的。
【问题讨论】:
-
你有没有让这个工作?我认为这解决了你的问题(但不是我的) - stackoverflow.com/questions/55474340/…
标签: python caching flask flask-cache