Challenge - 5 Problems
Cache Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django cache code?
Consider this Django code snippet using the low-level cache API:
What will be printed immediately after running this code?
from django.core.cache import cache
cache.set('key1', 'value1', timeout=5)
value = cache.get('key1')
print(value)What will be printed immediately after running this code?
Django
from django.core.cache import cache cache.set('key1', 'value1', timeout=5) value = cache.get('key1') print(value)
Attempts:
2 left
💡 Hint
Think about what cache.set and cache.get do immediately after setting a value.
✗ Incorrect
The cache.set stores 'value1' under 'key1' with a 5-second timeout. Immediately after, cache.get('key1') returns 'value1' because the key exists and has not expired.
❓ state_output
intermediate2:00remaining
What is the value of 'result' after this cache operation?
Given this code snippet:
What is the value of
from django.core.cache import cache
cache.set('counter', 10)
result = cache.incr('counter', 5)What is the value of
result after running this?Django
from django.core.cache import cache cache.set('counter', 10) result = cache.incr('counter', 5)
Attempts:
2 left
💡 Hint
What does cache.incr do to the stored value?
✗ Incorrect
cache.incr increments the integer value stored under 'counter' by 5, so 10 + 5 = 15.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in Django cache usage?
Identify which code snippet will cause a syntax error when using Django's low-level cache API:
Attempts:
2 left
💡 Hint
Look carefully at the commas separating arguments.
✗ Incorrect
Option C is missing a comma between 'value' and timeout=60, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this cache increment raise an error?
Given this code:
What error will be raised and why?
from django.core.cache import cache
cache.set('visits', 'ten')
cache.incr('visits')What error will be raised and why?
Django
from django.core.cache import cache cache.set('visits', 'ten') cache.incr('visits')
Attempts:
2 left
💡 Hint
cache.incr expects the stored value to be an integer.
✗ Incorrect
cache.incr tries to add 1 to the stored value. Since 'ten' is a string, it raises ValueError.
🧠 Conceptual
expert2:00remaining
Which statement about Django's low-level cache API is TRUE?
Select the correct statement about Django's low-level cache API behavior:
Attempts:
2 left
💡 Hint
Think about default return values and error handling in cache methods.
✗ Incorrect
cache.get() returns None if the key is missing and no default is given. cache.set() requires objects to be serializable. cache.incr() raises a ValueError if the key does not exist. cache.delete() does not raise an error if the key is missing.