Challenge - 5 Problems
Flask-Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the output of this Flask route with caching?
Consider this Flask app snippet using Flask-Caching. What will be the output if you call the
/time route twice within 10 seconds?Flask
from flask import Flask from flask_caching import Cache import time app = Flask(__name__) app.config['CACHE_TYPE'] = 'SimpleCache' app.config['CACHE_DEFAULT_TIMEOUT'] = 10 cache = Cache(app) @app.route('/time') @cache.cached() def get_time(): return str(time.time())
Attempts:
2 left
💡 Hint
Think about what caching does to the function output within the timeout period.
✗ Incorrect
The @cache.cached() decorator caches the output of the route for the default timeout (10 seconds). So repeated calls within that time return the cached response, which is the same timestamp string.
📝 Syntax
intermediate1:30remaining
Which option correctly initializes Flask-Caching with Redis backend?
You want to use Redis as the cache backend in Flask-Caching. Which code snippet correctly sets this up?
Attempts:
2 left
💡 Hint
Check the exact string Flask-Caching expects for Redis backend type and the config key for Redis URL.
✗ Incorrect
Flask-Caching expects 'redis' (all lowercase) for CACHE_TYPE and 'CACHE_REDIS_URL' for the Redis connection string. Option A matches this exactly.
🔧 Debug
advanced2:00remaining
What controls the cache timeout in this decorated route?
Given this Flask route using Flask-Caching with both CACHE_DEFAULT_TIMEOUT=5 and decorator timeout=5, what determines the actual cache duration?
Flask
from flask import Flask from flask_caching import Cache import time app = Flask(__name__) app.config['CACHE_TYPE'] = 'SimpleCache' app.config['CACHE_DEFAULT_TIMEOUT'] = 5 cache = Cache(app) @app.route('/count') @cache.cached(timeout=5) def count(): return str(time.time())
Attempts:
2 left
💡 Hint
Consider how the timeout parameter in the @cache.cached() decorator interacts with the app's CACHE_DEFAULT_TIMEOUT.
✗ Incorrect
The @cache.cached(timeout=5) decorator explicitly sets the timeout to 5 seconds for this route, taking precedence over the app.config['CACHE_DEFAULT_TIMEOUT']. Repeated calls within 5 seconds return the cached response; after 5 seconds, the function re-executes and returns a new timestamp.
🧠 Conceptual
advanced1:30remaining
What happens if you use @cache.cached() on a route with dynamic URL parameters?
You decorate a Flask route with
@cache.cached() but the route has a dynamic parameter like /user/<id>. What is the caching behavior?Flask
from flask import Flask from flask_caching import Cache import time app = Flask(__name__) app.config['CACHE_TYPE'] = 'SimpleCache' cache = Cache(app) @app.route('/user/<id>') @cache.cached() def user_profile(id): return f"User {id} profile at {time.time()}"
Attempts:
2 left
💡 Hint
Think about how Flask-Caching generates cache keys by default for routes with parameters.
✗ Incorrect
By default, @cache.cached() generates cache keys based on the request path, which includes dynamic path parameters (<id>). Thus, /user/1 and /user/2 have different paths (/user/1 vs. /user/2) and therefore different cache keys, caching separately.
❓ state_output
expert2:30remaining
What is the output count after these cached Flask route calls?
Given this Flask app, what is the value of
counter after calling /increment route 3 times quickly?Flask
from flask import Flask from flask_caching import Cache app = Flask(__name__) app.config['CACHE_TYPE'] = 'SimpleCache' app.config['CACHE_DEFAULT_TIMEOUT'] = 60 cache = Cache(app) counter = 0 @app.route('/increment') @cache.cached() def increment(): global counter counter += 1 return str(counter)
Attempts:
2 left
💡 Hint
Remember what caching does to function execution and state changes.
✗ Incorrect
The @cache.cached() decorator caches the entire response of the route. On the first call, counter increments from 0 to 1 and returns '1'. This response is cached. Subsequent calls within the cache timeout return the cached response without executing the function again, so counter does not increment further and remains 1.