Bird
0
0

You want to cache a sidebar that shows user-specific data but also updates every 10 minutes. Which is the best way to use template fragment caching?

hard📝 Application Q15 of 15
Django - Caching
You want to cache a sidebar that shows user-specific data but also updates every 10 minutes. Which is the best way to use template fragment caching?
ADo not use cache because user data changes
B{% cache 600 'sidebar' %} ... {% endcache %} to cache once for all users
C{% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes
D{% cache 'sidebar' 600 user.id %} ... {% endcache %} with wrong argument order
Step-by-Step Solution
Solution:
  1. Step 1: Understand caching user-specific data

    To cache user-specific content, include a unique user identifier in the cache key.
  2. Step 2: Set cache timeout to 600 seconds (10 minutes)

    Use 600 seconds as timeout to update cache every 10 minutes.
  3. Step 3: Verify correct syntax and usage

    {% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes uses correct syntax with timeout first, key string second, and user.id as extra parameter.
  4. Final Answer:

    {% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes -> Option C
  5. Quick Check:

    User-specific cache with timeout = {% cache 600 'sidebar' user.id %} ... {% endcache %} to cache per user for 10 minutes [OK]
Quick Trick: Use user.id param and timeout for user-specific cache [OK]
Common Mistakes:
MISTAKES
  • Caching once for all users ignoring user.id
  • Swapping timeout and key order
  • Avoiding cache for user data unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes