Flask-Cache 报错:KeyError: 'cache'

下面的代码:

from flask import Flask
from flask_cache import Cache

app = Flask(__name__)

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


@app.route('/test_cache')
@cache.cached(timeout=5)
def hello_world():
    print('if cache,the second request do not print')
    return 'Hello, World!'

运行:flask run,然后请求:curl http://127.0.0.1:5000/test_cache,有报错:

Exception possibly due to cache backend.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask_cache/__init__.py", line 289, in decorated_function
    rv = self.cache.get(cache_key)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask_cache/__init__.py", line 192, in cache
    return app.extensions['cache'][self]
KeyError: 'cache'

请问,是什么原因?

阅读 5.2k
2 个回答
from flask import Flask
from flask.ext.cache import Cache

app = Flask(__name__)
# Check Configuring Flask-Cache section for more details
cache = Cache(app,config={'CACHE_TYPE': 'simple'})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题