Bird
0
0

You want to cache a Flask route but have separate cache entries based on the query parameter ?category=. Which decorator usage correctly achieves this?

hard📝 Application Q8 of 15
Flask - Middleware and Extensions
You want to cache a Flask route but have separate cache entries based on the query parameter ?category=. Which decorator usage correctly achieves this?
A@cache.cached(timeout=30, unless=lambda: 'category' not in request.args)
B@cache.cached(timeout=30, key_prefix='category')
C@cache.cached(timeout=30, query_string=True)
D@cache.cached(timeout=30, make_cache_key=lambda: 'category')
Step-by-Step Solution
Solution:
  1. Step 1: Understand query_string parameter

    Setting query_string=True makes the cache key vary by the full query string.
  2. Step 2: Effect on caching

    This causes different cache entries for different query parameters like ?category=books vs ?category=movies.
  3. Final Answer:

    @cache.cached(timeout=30, query_string=True) -> Option C
  4. Quick Check:

    Use query_string=True to vary cache by URL params [OK]
Quick Trick: Use query_string=True to cache by query params [OK]
Common Mistakes:
MISTAKES
  • Using key_prefix incorrectly for query params
  • Misusing unless or make_cache_key for this purpose

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes