Bird
0
0

What is wrong with this Django cache usage?

medium📝 Debug Q7 of 15
Django - Caching
What is wrong with this Django cache usage?
cache.set('user_1', {'name': 'Alice'})
user = cache.get('user_1')
print(user['age'])
Acache.set requires a timeout argument
BThe cached dictionary has no 'age' key, causing a KeyError
Ccache.get returns a string, not a dictionary
Dcache.set cannot store dictionaries
Step-by-Step Solution
Solution:
  1. Step 1: Check cached data structure

    The cached dictionary only has 'name' key, no 'age'.
  2. Step 2: Analyze print statement

    Accessing user['age'] causes KeyError because 'age' key is missing.
  3. Final Answer:

    The cached dictionary has no 'age' key, causing a KeyError -> Option B
  4. Quick Check:

    Missing dict key causes KeyError [OK]
Quick Trick: Check keys exist before accessing cached dict values [OK]
Common Mistakes:
MISTAKES
  • Assuming cache.set can't store dicts
  • Expecting cache.get to return string always
  • Forgetting to provide timeout (optional)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes