Django Caching Overview
In this page:
Why Caching Matters
Every request to a Django view can involve database queries, template rendering, and Python logic. If a page is expensive to build but doesn't change often, rebuilding it on every single request wastes time. Caching stores the finished result so the next request can be served instantly from memory instead of redoing all that work.
Note: Caching is most useful for pages that are read often but written rarely.
Example: Why Caching Matters
Every request to a Django view can involve database queries, template rendering, and Python logic. If a page is expensive to build but doesn't change often, rebuilding it on every single request wastes time. Caching stores the finished result so the next request can be served instantly from memory instead of redoing all that work.
# settings.py -- configure a cache backend
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Low-Level Cache API
Django exposes a simple cache object with get, set, and delete methods. You choose a cache key, store a value with an optional expiry time, and read it back later instead of recomputing it.
Example: Low-Level Cache API
Django exposes a simple cache object with get, set, and delete methods. You choose a cache key, store a value with an optional expiry time, and read it back later instead of recomputing it.
from django.core.cache import cache
# Store a value for 300 seconds
cache.set('homepage_stats', {'users': 120}, 300)
# Read it back -- returns None if expired or missing
stats = cache.get('homepage_stats')
if stats is None:
stats = {'users': 120} # recompute
cache.set('homepage_stats', stats, 300)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Choosing What Not to Cache
Not everything should be cached. Data tied to a specific logged-in user, real-time counters, or anything that must always be accurate (like a bank balance) should be fetched fresh every time.
Warning: Caching sensitive or fast-changing per-user data can leak stale or wrong information between users.
Example: Choosing What Not to Cache
Not everything should be cached. Data tied to a specific logged-in user, real-time counters, or anything that must always be accurate (like a bank balance) should be fetched fresh every time.
from django.core.cache import cache
# Safe to cache: same for every visitor
cache.set('site_announcement', 'Welcome!', 3600)
# NOT cached: differs per logged-in user
def get_user_balance(user):
return user.account.balance # always fetched live
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Caching data that changes every second, so users see stale results.
- Never invalidating the cache after updating the underlying data.
- Assuming caching is on by default — Django's default cache backend does nothing until you configure a real one.
- Django's cache framework stores expensive results so they don't need to be recomputed on every request.
- You configure a cache backend in the CACHES setting in settings.py.
- Caching trades freshness for speed, so it's best for data that doesn't change often.
- Django supports caching at multiple levels: per-site, per-view, and low-level manual caching.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: