0
0
Flaskframework~20 mins

Flask-Caching for response caching - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask-Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1: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())
AThe first call returns an error, the second returns the timestamp.
BEach call returns a different timestamp string.
CBoth calls return the exact same timestamp string.
DBoth calls return an empty string.
Attempts:
2 left
💡 Hint
Think about what caching does to the function output within the timeout period.
📝 Syntax
intermediate
1: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?
A
app.config['CACHE_TYPE'] = 'redis'
app.config['CACHE_REDIS_URL'] = 'redis://localhost:6379/0'
cache = Cache(app)
B
app.config['CACHE_TYPE'] = 'RedisCache'
app.config['CACHE_REDIS_URL'] = 'redis://localhost:6379/0'
cache = Cache(app)
C
app.config['CACHE_TYPE'] = 'Redis'
app.config['CACHE_REDIS_URL'] = 'redis://localhost:6379/0'
cache = Cache(app)
D
app.config['CACHE_TYPE'] = 'redis'
app.config['REDIS_URL'] = 'redis://localhost:6379/0'
cache = Cache(app)
Attempts:
2 left
💡 Hint
Check the exact string Flask-Caching expects for Redis backend type and the config key for Redis URL.
🔧 Debug
advanced
2: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())
AThe CACHE_DEFAULT_TIMEOUT overrides the decorator's timeout parameter.
BThe route function is not executed after the first call.
CSimpleCache does not respect the timeout parameter.
DThe decorator's timeout=5 controls the cache duration for this route.
Attempts:
2 left
💡 Hint
Consider how the timeout parameter in the @cache.cached() decorator interacts with the app's CACHE_DEFAULT_TIMEOUT.
🧠 Conceptual
advanced
1: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()}"
AThe cache key ignores the dynamic parameter, so all user IDs share the same cached response.
BThe cache key includes the dynamic parameter, so each user ID caches separately.
CThe cache decorator raises an error because dynamic parameters are not supported.
DThe cache is disabled automatically for routes with dynamic parameters.
Attempts:
2 left
💡 Hint
Think about how Flask-Caching generates cache keys by default for routes with parameters.
state_output
expert
2: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)
Acounter is 1, and all 3 calls return '1'.
Bcounter is 3, and calls return '1', '2', '3' respectively.
Ccounter is 0, and all calls return '0'.
Dcounter is 3, but all calls return '3'.
Attempts:
2 left
💡 Hint
Remember what caching does to function execution and state changes.