What is Flask-Caching used for in a Flask application?
Flask-Caching is used to store the results of expensive or slow operations so that future requests can get the response faster without repeating the work.
Click to reveal answer
beginner
How do you initialize Flask-Caching in a Flask app?
You create a Cache object with your Flask app and a configuration, like this: <br><code>from flask_caching import Cache<br>cache = Cache(app, config={'CACHE_TYPE': 'simple'})</code>
Click to reveal answer
beginner
What does the @cache.cached(timeout=60) decorator do on a Flask route?
It saves the response of that route for 60 seconds. During that time, repeated requests return the saved response quickly without running the route code again.
Click to reveal answer
intermediate
Name two cache types supported by Flask-Caching.
Two common cache types are 'simple' (in-memory cache for development) and 'redis' (using Redis server for production caching).
Click to reveal answer
beginner
Why is caching useful in web applications?
Caching reduces server work and speeds up response times by reusing previous results. This helps handle more users and improves user experience.
Click to reveal answer
What does the 'timeout' parameter in @cache.cached(timeout=30) control?
AThe priority of the cache
BThe number of times the cache is used
CThe size of the cache in megabytes
DHow long the cached response is stored before refreshing
✗ Incorrect
The 'timeout' sets how many seconds the cached response stays valid before the cache is refreshed.
Which Flask-Caching cache type is best for simple development testing?
Aredis
Bsimple
Cmemcached
Dfilesystem
✗ Incorrect
'simple' cache stores data in memory and is easy to use for development.
How do you apply caching to a Flask route function?
AUse @cache.cached decorator above the route function
BCall cache.set() inside the route
CUse Flask's built-in cache automatically
DAdd cache configuration in HTML
✗ Incorrect
The @cache.cached decorator wraps the route to cache its output.
What happens if you do not use caching on a slow route?
AThe route runs faster automatically
BThe server caches it by default
CEvery request runs the slow code again
DThe browser caches the response
✗ Incorrect
Without caching, each request triggers the slow operation again.
Which of these is NOT a benefit of using Flask-Caching?
AAutomatically fixes bugs
BReduces server load
CImproves response speed
DHelps handle more users
✗ Incorrect
Caching improves speed and load but does not fix bugs automatically.
Explain how to add caching to a Flask route using Flask-Caching.
Think about setup, decorator, and timeout.
You got /4 concepts.
Describe why caching responses in a web app can improve user experience.
Focus on speed and server work.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using Flask-Caching in a Flask application?
easy
A. To handle database migrations automatically
B. To secure the application from unauthorized access
C. To store responses temporarily to speed up repeated requests
D. To manage user sessions and cookies
Solution
Step 1: Understand Flask-Caching role
Flask-Caching is used to save the output of views temporarily.
Step 2: Identify the benefit
Saving responses reduces processing time for repeated requests, speeding up the app.
Final Answer:
To store responses temporarily to speed up repeated requests -> Option C
Quick Check:
Flask-Caching = Response speedup [OK]
Hint: Caching saves repeated response time, boosting speed [OK]
Common Mistakes:
Confusing caching with security features
Thinking caching manages database or sessions
Assuming caching permanently stores data
2. Which of the following is the correct way to cache a Flask view response for 60 seconds using Flask-Caching?
easy
A. @cache.cached(timeout=60)
def my_view():
return 'Hello World'
B. @cache.cache(timeout=60)
def my_view():
return 'Hello World'
C. @cache.cached(60)
def my_view():
return 'Hello World'
D. @cache.cache(60)
def my_view():
return 'Hello World'
Solution
Step 1: Identify correct decorator name
The correct decorator is @cache.cached with a named argument timeout.
Step 2: Check syntax for timeout
Timeout must be passed as a keyword argument: timeout=60.
Final Answer:
@cache.cached(timeout=60)\ndef my_view():\n return 'Hello World' -> Option A
Quick Check:
Decorator name and timeout keyword correct = A [OK]
Hint: Use @cache.cached(timeout=seconds) exactly [OK]
Common Mistakes:
Using @cache.cache instead of @cache.cached
Passing timeout as positional argument
Omitting the timeout keyword
3. Given this Flask code snippet using Flask-Caching, what will be printed on the second request to /time within 10 seconds?
from flask import Flask
from flask_caching import Cache
import time
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
@app.route('/time')
@cache.cached(timeout=10)
def get_time():
return str(time.time())
# Assume the first request prints 1680000000.0
medium
A. A new timestamp different from 1680000000.0
B. The string 'None'
C. An error because caching is not configured
D. The same timestamp 1680000000.0
Solution
Step 1: Understand caching behavior
The first call caches the response (timestamp) for 10 seconds.
Step 2: Predict second request output within timeout
Within 10 seconds, the cached response is returned, so the timestamp stays the same.
Final Answer:
The same timestamp 1680000000.0 -> Option D
Quick Check:
Cached response repeats within timeout = D [OK]
Hint: Cached output repeats until timeout expires [OK]
Common Mistakes:
Expecting a new timestamp every request
Assuming caching is disabled without error
Confusing cache timeout with immediate refresh
4. What is wrong with this Flask-Caching setup code?
A. The order of decorators is incorrect; @app.route should be above @cache.cached
B. Cache must be initialized before defining cached routes
C. cache.init_app(app) should be called before using @cache.cached
D. No error; this code works correctly
Solution
Step 1: Check decorator order
In Flask, @app.route must be the outer decorator, so it should be above @cache.cached.
Step 2: Understand decorator stacking
Incorrect order causes the route not to register properly, breaking caching behavior.
Final Answer:
The order of decorators is incorrect; @app.route should be above @cache.cached -> Option A
Quick Check:
Decorator order matters = B [OK]
Hint: Place @app.route above @cache.cached [OK]
Common Mistakes:
Swapping decorator order
Thinking cache.init_app must be before decorators
Assuming no error with wrong decorator order
5. You want to cache a Flask view response but only for requests with a query parameter ?user=admin. Which approach correctly applies Flask-Caching to cache only those responses?
hard
A. Use @cache.cached(timeout=30, query_string=True) and check user param inside view
B. Use @cache.cached(timeout=30, unless=lambda: request.args.get('user') != 'admin')
C. Use @cache.cached(timeout=30) and manually cache responses inside the view
D. Use @cache.memoize(timeout=30) without conditions
Solution
Step 1: Understand conditional caching
Flask-Caching's unless parameter skips caching when condition is True.
Step 2: Apply condition for user param
Using unless=lambda: request.args.get('user') != 'admin' caches only when user is 'admin'.
Final Answer:
Use @cache.cached(timeout=30, unless=lambda: request.args.get('user') != 'admin') -> Option B
Quick Check:
Conditional caching uses unless param = A [OK]
Hint: Use unless param to skip caching conditionally [OK]
Common Mistakes:
Using query_string=True without filtering user
Trying to cache manually inside view
Using memoize without conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.