1

I started from scratch the project but get this error

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Exception possibly due to cache backend.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask_caching/__init__.py", line 451, in decorated_function
    rv = self.cache.get(cache_key)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask_caching/__init__.py", line 259, in cache
    return app.extensions["cache"][self]
KeyError: <flask_caching.Cache object at 0x7fbec92bfac0>
127.0.0.1 - - [21/Apr/2021 14:02:42] "GET /test HTTP/1.1" 200 -

When I try to cache a simple function:

The structure of my app is:

| - app.py
| - api
      | - __init__.py
      | - main_api.py

So first I initiate the app.py

from flask import Flask
from flask_caching import Cache


app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})


from api import api as api_bp
app.register_blueprint(api_bp)

Second I create the main_api.py

from flask import jsonify
from . import api
from app import cache
import random


@api.route('/test', methods=['GET', 'POST'])
@cache.cached(timeout=2)
def rand():
  return jsonify(random.randint(1, 10))

init.py is simple as abc

from flask import Blueprint

api = Blueprint('api', __name__)
from . import main_api

Used Python3.8 and Installed dependencies

click==7.1.2
Flask==1.1.2
Flask-Caching==1.10.1
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
Werkzeug==1.0.1
4

0 回答 0