0
0
Djangoframework~20 mins

Cache invalidation strategies in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery in Django
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding cache timeout in Django
What happens when you set a cache timeout to a very low value in Django's caching framework?
AThe cache entries expire quickly, causing frequent cache misses and more database hits.
BThe cache entries never expire, so the data stays forever in the cache.
CThe cache automatically refreshes data from the database without any misses.
DThe cache entries are deleted only when the server restarts.
Attempts:
2 left
💡 Hint
Think about what 'timeout' means for cached data.
component_behavior
intermediate
2: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?
AChanging the version number causes Django to look for a different cache entry, effectively invalidating the old cache.
BChanging the version number merges old and new cache entries.
CChanging the version number has no effect; Django ignores it.
DChanging the version number deletes all cache entries.
Attempts:
2 left
💡 Hint
Think about how versioning helps manage cache entries.
state_output
advanced
2: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()
AA new cache entry 'product_1' is created with 'New Name'.
BThe cache entry 'product_1' remains unchanged with 'Old Data'.
CThe cache entry 'product_1' is deleted, so cache.get('product_1') returns None.
DThe cache raises a KeyError because the key is missing.
Attempts:
2 left
💡 Hint
What does cache.delete do to the cache entry?
📝 Syntax
advanced
1: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?
Acache.clear_key('user_profile_5')
Bcache.remove('user_profile_5')
Ccache.invalidate('user_profile_5')
Dcache.delete('user_profile_5')
Attempts:
2 left
💡 Hint
Check Django cache API for deleting keys.
🔧 Debug
expert
3: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?
AThe cache backend automatically updates cache on model save, so this is impossible.
BThe cache key was not deleted or updated after the model save, so stale data remains.
CThe cache timeout is set to zero, so data never expires.
DThe model instance was saved with update_fields, which clears the cache automatically.
Attempts:
2 left
💡 Hint
Think about what triggers cache invalidation after data changes.