Challenge - 5 Problems
Cache Mastery in Django
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding cache timeout in Django
What happens when you set a cache timeout to a very low value in Django's caching framework?
Attempts:
2 left
💡 Hint
Think about what 'timeout' means for cached data.
✗ Incorrect
Setting a low timeout means cached data expires quickly. This causes the cache to miss often and fetch fresh data from the database repeatedly.
❓ component_behavior
intermediate2:00remaining
Effect of cache key versioning in Django
Given a Django cache key with versioning, what is the effect of changing the version number when retrieving cached data?
Attempts:
2 left
💡 Hint
Think about how versioning helps manage cache entries.
✗ Incorrect
Cache key versioning lets you keep multiple versions of cached data. Changing the version makes Django look for a new key, so old cache is ignored.
❓ state_output
advanced2:30remaining
Cache invalidation with signals in Django
Consider a Django model with a post_save signal that clears a related cache key. What is the expected cache state after saving an instance?
Django
from django.db import models from django.core.cache import cache from django.db.models.signals import post_save from django.dispatch import receiver class Product(models.Model): name = models.CharField(max_length=100) @receiver(post_save, sender=Product) def clear_product_cache(sender, instance, **kwargs): cache_key = f'product_{instance.pk}' cache.delete(cache_key) # Assume cache initially has {'product_1': 'Old Data'} product = Product(pk=1, name='New Name') product.save()
Attempts:
2 left
💡 Hint
What does cache.delete do to the cache entry?
✗ Incorrect
The post_save signal deletes the cache key for the saved product. So after saving, the cache entry is removed and returns None on get.
📝 Syntax
advanced1:30remaining
Correct syntax for cache invalidation in Django
Which of the following code snippets correctly invalidates a cache key named 'user_profile_5' in Django?
Attempts:
2 left
💡 Hint
Check Django cache API for deleting keys.
✗ Incorrect
The correct method to remove a cache entry by key is cache.delete(key). Other methods do not exist in Django's cache API.
🔧 Debug
expert3:00remaining
Diagnosing stale cache after model update
A Django app caches user data under 'user_data_42'. After updating the user model instance with pk=42, the cache still returns old data. Which is the most likely cause?
Attempts:
2 left
💡 Hint
Think about what triggers cache invalidation after data changes.
✗ Incorrect
Django cache does not auto-update on model changes. You must manually delete or update cache keys after saving models to avoid stale data.