Bird
0
0

In a Django app, you want to ensure that cached user profile data is invalidated only when the profile is updated. Which cache invalidation approach is most appropriate?

hard📝 Conceptual Q8 of 15
Django - Caching
In a Django app, you want to ensure that cached user profile data is invalidated only when the profile is updated. Which cache invalidation approach is most appropriate?
AClear the entire cache periodically using cache.clear()
BSet a very short timeout so cache expires quickly without manual intervention
CUse signal handlers to delete or update cache entries when the user profile model changes
DNever invalidate cache and rely on manual server restarts
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    Invalidate cache only when the user profile updates.
  2. Step 2: Evaluate options

    Use signal handlers to delete or update cache entries when the user profile model changes uses Django signals to precisely invalidate cache on model changes, which is efficient.
  3. Set a very short timeout so cache expires quickly without manual intervention relies on timeouts which may cause stale data.
  4. Clear the entire cache periodically using cache.clear() clears all cache, which is inefficient and affects unrelated data.
  5. Never invalidate cache and rely on manual server restarts is not practical.
  6. Final Answer:

    Use signal handlers to delete or update cache entries when the user profile model changes -> Option C
  7. Quick Check:

    Signals enable targeted cache invalidation [OK]
Quick Trick: Use Django signals to invalidate cache on model updates [OK]
Common Mistakes:
MISTAKES
  • Relying solely on time-based expiration
  • Clearing entire cache unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes