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 django.core.cache import cache

This uses the default cache. It's equivalent to caches['default']. Accessing a specific cache is also possible via its alias:

from django.core.cache import cachesmy_cache = caches['alias']

Let's take a look at how the cache API works. Open the shell with the command python manage.py shell and execute the following code:

>>> from django.core.cache import cache>>> cache.set('musician', 'Django Reinhardt', 20)

We access the default cache backend and use set(key, value, timeout) to store a key named 'musician' with a value that ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.