0
0
Djangoframework~10 mins

Cache invalidation strategies in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a cache key with a timeout in Django.

Django
cache.set('user_123', user_data, timeout=[1])
Drag options to blanks, or click blank then click option'
A300
B'300'
CNone
D'None'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers for timeout
Setting timeout to None as a string
2fill in blank
medium

Complete the code to delete a cache key in Django.

Django
cache.[1]('user_123')
Drag options to blanks, or click blank then click option'
Adelete
Bclear
Cremove
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using clear deletes all cache, not a single key
Using pop is not a cache method
3fill in blank
hard

Fix the error in the code to invalidate cache after updating a model instance.

Django
def save(self, *args, **kwargs):
    super().save(*args, **kwargs)
    cache.[1](f'user_{self.id}')
Drag options to blanks, or click blank then click option'
Aclear
Bdelete
Cset
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using clear removes all cache
Using set overwrites cache instead of invalidating
4fill in blank
hard

Fill both blanks to create a cache key and set it with a timeout.

Django
cache_key = f'user_[1]'
cache.[2](cache_key, user_data, timeout=600)
Drag options to blanks, or click blank then click option'
Aid
Bset
Cdelete
Dpk
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete instead of set
Using pk when id is expected
5fill in blank
hard

Fill all three blanks to check cache, set if missing, and return data.

Django
cache_key = f'user_[1]'
data = cache.[2](cache_key)
if data is None:
    data = get_user_data([3])
    cache.set(cache_key, data, timeout=300)
return data
Drag options to blanks, or click blank then click option'
Aid
Bget
Cuser_id
Dpk
Attempts:
3 left
💡 Hint
Common Mistakes
Using set instead of get to retrieve data
Using pk instead of user_id in function call