0
0
Flaskframework~20 mins

Response caching strategies in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Caching Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route with cache control?

Consider this Flask route that sets cache headers:

from flask import Flask, make_response
app = Flask(__name__)

@app.route('/data')
def data():
    response = make_response('Hello World')
    response.headers['Cache-Control'] = 'public, max-age=60'
    return response

What will the browser do with the response?

Flask
from flask import Flask, make_response
app = Flask(__name__)

@app.route('/data')
def data():
    response = make_response('Hello World')
    response.headers['Cache-Control'] = 'public, max-age=60'
    return response
AThe browser does not cache the response at all.
BThe browser caches the response for 60 seconds and shares it with other users.
CThe browser caches the response but only for the current user session.
DThe browser caches the response indefinitely until manually cleared.
Attempts:
2 left
💡 Hint

Look at the meaning of 'public' and 'max-age' in Cache-Control headers.

state_output
intermediate
2:00remaining
What is the cache behavior of this Flask route using @cache.cached?

Given this Flask route using Flask-Caching:

from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})

@app.route('/time')
@cache.cached(timeout=30)
def get_time():
    import time
    return str(time.time())

What happens when you call /time multiple times within 30 seconds?

Flask
from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})

@app.route('/time')
@cache.cached(timeout=30)
def get_time():
    import time
    return str(time.time())
AThe first call computes time; subsequent calls within 30 seconds return the cached time string.
BEach call computes a new time string regardless of caching.
CThe cache is ignored because SimpleCache does not support timeout.
DThe cache stores the time but expires immediately, so no caching occurs.
Attempts:
2 left
💡 Hint

Think about what @cache.cached with timeout does.

📝 Syntax
advanced
2:00remaining
Which option correctly sets a Flask response to never cache?

Which code snippet correctly sets HTTP headers to prevent caching of a Flask response?

Aresponse.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
Bresponse.headers['Cache-Control'] = 'public, max-age=0'
Cresponse.headers['Cache-Control'] = 'private, max-age=60'
Dresponse.headers['Cache-Control'] = 'max-age=3600, public'
Attempts:
2 left
💡 Hint

Look for headers that explicitly prevent caching.

🔧 Debug
advanced
2:00remaining
Why does this Flask cache not work as expected?

Consider this Flask route using Flask-Caching:

from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'NullCache'})

@app.route('/data')
@cache.cached(timeout=60)
def data():
    return 'Cached Data'

Why does caching not occur?

Flask
from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'NullCache'})

@app.route('/data')
@cache.cached(timeout=60)
def data():
    return 'Cached Data'
AThe timeout value is too low to cache anything.
BThe decorator @cache.cached is used incorrectly and does not cache.
CNullCache disables caching, so no data is stored or reused.
DFlask-Caching requires a Redis backend to cache data.
Attempts:
2 left
💡 Hint

Check the meaning of 'NullCache' in Flask-Caching.

🧠 Conceptual
expert
3:00remaining
Which caching strategy best suits dynamic user-specific content in Flask?

You have a Flask app serving user dashboards with personalized data that changes frequently. Which caching strategy is best to improve performance without showing wrong data?

ADisable all caching to ensure fresh data every request.
BSet Cache-Control to 'public, max-age=300' so all users share cached dashboards.
CUse client-side caching only with 'private, max-age=600' headers.
DUse server-side cache keyed by user ID to store each user's dashboard data separately.
Attempts:
2 left
💡 Hint

Think about caching personalized data safely.