0
0
Djangoframework~20 mins

Cache backends (memory, Redis, Memcached) 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!
component_behavior
intermediate
1:30remaining
What is the output of this Django cache code using memory backend?

Consider this Django view code snippet using the default memory cache backend:

from django.core.cache import cache

def get_data():
    cache.set('key', 'value', timeout=5)
    return cache.get('key')

What will get_data() return immediately after setting the cache?

Django
from django.core.cache import cache

def get_data():
    cache.set('key', 'value', timeout=5)
    return cache.get('key')
ANone
BRaises KeyError
C'value'
DEmpty string ''
Attempts:
2 left
💡 Hint

Think about what happens when you set and get a key immediately in the same cache.

📝 Syntax
intermediate
1:30remaining
Which cache backend setting is syntactically correct for Redis in Django?

Which of the following CACHES settings correctly configures Redis as the cache backend in Django?

A"""CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': 'redis://127.0.0.1:6379', } }"""
B"""CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.redis.RedisCache', 'LOCATION': 'redis://localhost:6379', } }"""
C"""CACHES = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'memcached://127.0.0.1:11211', } }"""
D"""CACHES = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'redis://127.0.0.1:6379/1', } }"""
Attempts:
2 left
💡 Hint

Redis backend in Django usually uses django_redis.cache.RedisCache and a Redis URL.

🔧 Debug
advanced
2:00remaining
Why does this Memcached cache code raise a connection error?

Given this Django cache setting and code snippet:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

from django.core.cache import cache
cache.set('foo', 'bar')
cache.get('foo')

Running this code raises a connection error. What is the most likely cause?

Django
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

from django.core.cache import cache
cache.set('foo', 'bar')
cache.get('foo')
AMemcached server is not running on 127.0.0.1:11211
BDjango cache API does not support Memcached
CThe backend string is misspelled
DThe cache key 'foo' is too long for Memcached
Attempts:
2 left
💡 Hint

Check if the Memcached service is active and reachable at the specified address.

state_output
advanced
1:30remaining
What is the value of the cache key after expiration with Redis backend?

Assume Redis cache backend is configured with a 1-second timeout. This code runs:

from django.core.cache import cache
cache.set('temp', 'data', timeout=1)
import time
time.sleep(2)
value = cache.get('temp')

What is the value of value after this code runs?

Django
from django.core.cache import cache
cache.set('temp', 'data', timeout=1)
import time
time.sleep(2)
value = cache.get('temp')
ANone
B'data'
CRaises TimeoutError
DEmpty string ''
Attempts:
2 left
💡 Hint

Think about what happens when you get a cache key after its timeout expires.

🧠 Conceptual
expert
2:00remaining
Which cache backend is best suited for sharing cache across multiple Django app instances?

You have multiple Django app servers running behind a load balancer. You want a cache backend that all instances share to keep data consistent. Which cache backend is best suited?

AFile-based cache backend (django.core.cache.backends.filebased.FileBasedCache)
BRedis cache backend (django_redis.cache.RedisCache)
CLocal memory cache backend (django.core.cache.backends.locmem.LocMemCache)
DDummy cache backend (django.core.cache.backends.dummy.DummyCache)
Attempts:
2 left
💡 Hint

Consider which cache backend supports sharing data across multiple machines.