Skip to content
Geoffroy Noël edited this page Jan 30, 2019 · 12 revisions

Bare-bones default Django caching uses Redis. Currently if Redis is not installed on the VM, accesses to the cache will fail silently which can negatively affect performances but shouldn't cause any error. There is a single Redis service running on the VM shared by all three sites for that project. The service offers multiple databases, each one has its own number, 0 for live/local site, 1 for staging and 2 for dev.

Settings

settings/base.py uses 0 and your local.py on stg site should redefine the url like this:

CACHE_REDIS_DATABASE = '1'

CACHES['default']['LOCATION'] = 'redis://127.0.0.1:6379/' + CACHE_REDIS_DATABASE

On dev site, use '2'.

Please be aware that due to a bug in bare-bones, the url for dev and stg contained two errors:

  • the protocol was missing
  • the database number was prefixed with : instead of /

Also, please be very careful about the numbering, make sure different instances don't use the same number.

WRONG EXAMPLE: CACHES['default']['LOCATION'] = '127.0.0.1:6379:' + CACHE_REDIS_DATABASE

How to install Redis?

Redis should be installed by default on the server VMs. If not, you can type apt install redis-server to install it and service redis start to start it.

What is the cache used for?

Django and third party apps can use their own cache or the default one. For instance you can cache blocks of django templates which are expensive to generate (e.g. result of heavy database queries). You can also cache intermediate results of the business or application logic on the server side.

Wagtail, for instance, will cache things like the path to the home page of your site.

Django-compressor uses the default cache. But if it is not working it will default to file caching. In the cache it will save the blocks of html that point to the minified and aggregated assets in PRJ/static/CACHE/js|css .

How can I test if the cache is actually used?

Change into your project, activate the virtual environment and paste the following as a single ommand in the terminal.

./manage.py shell -c "from django.core.cache import caches; print(caches['default']); caches['default'].set('k1', 'v1'); print('Cache is working' if (caches['default'].get('k1') == 'v1') else 'Cache is NOT WORKING')"

This will give you the type of the default cache (You should see Redis in the name), and on the second line, whether the cache is working or not.

If you get a negative result, please check that Redis is actually running and that your settings are correct and distinct from other sites on that VM.

How can I see values in the cache?

Use redis-cli from the command line to enter redis interactive shell (apt install redis-tools, if missing).

From there you can use the following commands:

# show all the keys
keys * 
# set a key=value
set MYKEY MYVALUE
# get the value of a key
get MYKEY
# remove a key
del MYKEY
# change to a different database number (default is 0)
select NUMBER

Something's messed up after setting up Redis?

Some apps like django-compressor can lose the plot after making change to the default cache. If you want to start from fresh then go to the terminal, activate your virtualenv and do the following:

cd PROJ_ROOT
rm -rf static/CACHE/*
./manage clear_cache
touch PROJ/wsgi.py

Do a hard-reload on the web pages that lost their assets, it should now work fine.