0
0
Flaskframework~10 mins

Response caching strategies in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the caching extension in Flask.

Flask
from flask_caching import [1]
Drag options to blanks, or click blank then click option'
ACacheControl
BCache
CCacheManager
DCacheHandler
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'CacheControl' which is unrelated to Flask-Caching
Using plural forms like 'Caches'
2fill in blank
medium

Complete the code to initialize caching with a simple config.

Flask
app = Flask(__name__)
app.config['CACHE_TYPE'] = [1]
cache = Cache(app)
Drag options to blanks, or click blank then click option'
A'simple'
B'redis'
C'filesystem'
D'null'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'redis' without Redis server configured
Using 'null' which disables caching
3fill in blank
hard

Fix the error in the decorator to cache the response for 60 seconds.

Flask
@cache.[1](timeout=60)
def get_data():
    return 'Hello World'
Drag options to blanks, or click blank then click option'
Acache_timeout
Bcache_for
Ccache_response
Dcached
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent decorators like 'cache_response'
Confusing with cache timeout parameter
4fill in blank
hard

Fill both blanks to cache a function with a custom key prefix and 120 seconds timeout.

Flask
@cache.[1](timeout=[2], key_prefix='my_key')
def fetch_data():
    return {'data': 123}
Drag options to blanks, or click blank then click option'
Acached
B60
C120
Dmemoize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'memoize' decorator which is for different caching
Setting timeout to 60 instead of 120
5fill in blank
hard

Fill all three blanks to create a cache key dynamically using function arguments and cache the result.

Flask
def make_cache_key(*args, **kwargs):
    return f"user_[1]_[2]"

@cache.[3](key_prefix=make_cache_key)
def get_user_data(user_id, detail_level):
    return {'id': user_id, 'detail': detail_level}
Drag options to blanks, or click blank then click option'
Aargs[0]
Bargs[1]
Ccached
Dmemoize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cached' decorator which does not support function arguments well
Using parameter names directly instead of args[0]/args[1]