Flask缓存抛出错误-KeyError:<;0x7ff585e47358处的flask_caching.Cache对象>;

Flask缓存抛出错误-KeyError:<;0x7ff585e47358处的flask_caching.Cache对象>;,flask,python-3.6,Flask,Python 3.6,我正在尝试将缓存添加到我的Python Flask应用程序中。 我按照Flask缓存页面的建议做了,所以我有一个应用程序内模块: config = { # "DEBUG": True, "env": 'dev', "secret_key": 'my secret stuff', "CACHE_TYPE": "simple", "CACHE_DEFA

我正在尝试将缓存添加到我的Python Flask应用程序中。 我按照Flask缓存页面的建议做了,所以我有一个应用程序内模块:

config = {
    # "DEBUG": True,
    "env": 'dev',
    "secret_key": 'my secret stuff',
    "CACHE_TYPE": "simple",
    "CACHE_DEFAULT_TIMEOUT": 300
}

app = Flask(__name__)
app.config.from_mapping(config)
cors = CORS(app, resources={"/api/*": {"origins": "*"}})
cache = Cache(app)
cache.init_app(app)


@app.before_first_request
def init_resources():
    Database.initialize()


app.register_blueprint(auth_api_blueprint, url_prefix="/api/auth")
app.register_blueprint(user_api_blueprint, url_prefix="/api/user")
app.register_blueprint(year_api_blueprint, url_prefix="/api/year")
app.register_blueprint(notice_api_blueprint, url_prefix="/api/notice")
app.register_blueprint(event_api_blueprint, url_prefix="/api/event")
app.register_blueprint(admins_api_blueprint, url_prefix="/api/admin")
app.register_blueprint(guardian_api_blueprint, url_prefix="/api/guardian")
app.register_blueprint(employee_api_blueprint, url_prefix="/api/employee")
app.register_blueprint(student_api_blueprint, url_prefix="/api/student")
app.register_blueprint(teacher_api_blueprint, url_prefix="/api/teacher")

if __name__ == '__main__':
    with app.app_context():
        cache.clear()
        
    # app.run(port=8080) - port does not work here, it is still default 5000
    app.run()
然后我将缓存装饰器应用于如下方法:

from common.database import Database
from common.decorators import requires_login

year_api_blueprint = Blueprint('api/year', __name__)

from src.app import cache

@year_api_blueprint.route('/all')
@cache.cached(timeout=500, key_prefix="years")
# @requires_login - this need to be public
def get_all_years():
    data = Database.find("years", {})
    if data is not None:
        return jsonify([year for year in data])
一切似乎都很好,一年以上不再被多次调用(仅一次) 但是,每次使用缓存年份时,我都会遇到此错误:

127.0.0.1 - - [20/Oct/2020 17:36:32] "OPTIONS /api/year/all HTTP/1.1" 200 -
Exception possibly due to cache backend.
Traceback (most recent call last):
  File "/home/smoczyna/Python-Projects/SchoolMateAPI/venv/lib/python3.6/site-packages/flask_caching/__init__.py", line 435, in decorated_function
    rv = self.cache.get(cache_key)
  File "/home/smoczyna/Python-Projects/SchoolMateAPI/venv/lib/python3.6/site-packages/flask_caching/__init__.py", line 244, in cache
    return app.extensions["cache"][self]
KeyError: <flask_caching.Cache object at 0x7ff585e47358>
127.0.0.1---[20/Oct/2020 17:36:32]“OPTIONS/api/year/all HTTP/1.1”200-
异常可能是由于缓存后端。
回溯(最近一次呼叫最后一次):
文件“/home/smoczyna/Python Projects/SchoolMateAPI/venv/lib/python3.6/site packages/flask_caching/_init__.py”,第435行,在函数中
rv=self.cache.get(缓存密钥)
文件“/home/smoczyna/Python Projects/SchoolMateAPI/venv/lib/python3.6/site packages/flask_caching/_init__.py”,缓存中第244行
return app.extensions[“缓存”][自身]
关键错误:
我在这里看到过类似的帖子,但我不了解解决方案,也不知道如何应用。关于应用程序中的缓存,我没有什么比这更重要的了


所以我的问题是这里可能缺少什么或配置错误?

它在哪一行抛出Keyerror?请尝试这样的上下文装饰器:感谢您的响应,每个用@cache.cached()修饰的方法都会发生错误。添加@app.context\u处理器似乎没有任何效果,添加@app.app\u context()时仍会出现错误。原因编译/rutime错误:TypeError:“AppContext”对象不可调用