Cache levels
Django provides the following levels of caching, listed here by ascending order of granularity:
- Low-level cache API: Provides the highest granularity. Allows you to cache specific queries or calculations.
- Template cache: Allows you to cache template fragments.
- Per-view cache: Provides caching for individual views.
- Per-site cache: The highest-level cache. It caches your entire site.
Think about your cache strategy before implementing caching. Focus first on expensive queries or calculations that are not calculated on a per-user basis.
In the upcoming sections, we will explore how to use each of these caching levels in our project.
Let’s start by learning how to use the low-level cache API in your Python code.
Using the low-level cache API
The low-level cache API allows you to store objects in the cache with any granularity. It is located at django.core.cache
. You can import it like this:
from...