Bird
0
0

What is wrong with this Flask-Caching setup code?

medium📝 Debug Q14 of 15
Flask - Middleware and Extensions
What is wrong with this Flask-Caching setup code?
from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
cache = Cache()

@cache.cached(timeout=30)
@app.route('/')
def home():
    return 'Welcome!'

cache.init_app(app)
AThe order of decorators is incorrect; @app.route should be above @cache.cached
BCache must be initialized before defining cached routes
Ccache.init_app(app) should be called before using @cache.cached
DNo error; this code works correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check decorator order

    In Flask, @app.route must be the outer decorator, so it should be above @cache.cached.
  2. Step 2: Understand decorator stacking

    Incorrect order causes the route not to register properly, breaking caching behavior.
  3. Final Answer:

    The order of decorators is incorrect; @app.route should be above @cache.cached -> Option A
  4. Quick Check:

    Decorator order matters = B [OK]
Quick Trick: Place @app.route above @cache.cached [OK]
Common Mistakes:
MISTAKES
  • Swapping decorator order
  • Thinking cache.init_app must be before decorators
  • Assuming no error with wrong decorator order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes