Cache Backends and Redis
Configure a production cache backend.
Cache Backends and Redis is a free Django Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What a Cache Saves You
A cache stores expensive results so the next request can skip the slow work and reply almost instantly. ⚡
Where Caches Live
Django talks to a cache through a backend, a pluggable adapter that decides where your cached data is physically stored.
The CACHES Setting
You configure caching in the CACHES dict in settings.py, always with a key named default that Django reads first.
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
}
}Local Memory for Dev
The default LocMemCache keeps data in your process memory. It is fine for development but is not shared across servers.
Why Redis in Production
For real apps, point the backend at Redis, a fast in-memory store that many web servers can share at once.
Configuring the Redis Backend
Set BACKEND to RedisCache and give it a LOCATION pointing at your Redis server URL.
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
}
}Talking to the Cache
Import the shared cache object from django.core.cache to read and write values from anywhere in your code.
from django.core.cache import cacheset and get
Use set to store a value under a key, then get to read it back later in another request.
cache.set("answer", 42)
value = cache.get("answer")Timeouts Keep Data Fresh
The third argument to set is a timeout in seconds. After it passes, the key expires and get returns None.
cache.set("answer", 42, 300)Handling Cache Misses
A missing key is a cache miss. Pass a default to get so your code has a safe value to fall back on.
value = cache.get("answer", "none yet")Other Backends Exist
Beyond Redis, Django ships database and file-based backends, handy when you cannot run a separate memory server.
Quick Check
Time to confirm where production caches usually live.
Recap
You met the cache backend: configure CACHES, use LocMem in dev and Redis in production, then set and get values with a timeout. 🎉
Frequently asked questions
Is the “Cache Backends and Redis” lesson free?
Yes — the full text of “Cache Backends and Redis” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Cache Backends and Redis”?
Configure a production cache backend. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cache Backends and Redis” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Cache Backends and Redis
- Per-View and Per-Site Caching
- Template Fragment Caching
- Low-Level cache API and Invalidation