Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Django - Caching
What will be the output of this code?
from django.core.cache import cache
cache.set('count', 5, timeout=1)
import time
time.sleep(2)
print(cache.get('count'))
ANone
BError
C0
D5
Step-by-Step Solution
Solution:
  1. Step 1: Understand cache expiration

    The key 'count' is set with value 5 and expires after 1 second.
  2. Step 2: Analyze time.sleep(2)

    After sleeping 2 seconds, the cache entry has expired and is removed.
  3. Step 3: Check cache.get() result

    Since the key expired, cache.get('count') returns None.
  4. Final Answer:

    None -> Option A
  5. Quick Check:

    Expired cache keys return None on get() [OK]
Quick Trick: Cache keys expire after timeout and return None [OK]
Common Mistakes:
MISTAKES
  • Expecting old value after expiration
  • Assuming cache.get() returns 0 or error
  • Ignoring timeout effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes