How can I test if my redis cache is working?

39,477

Solution 1

Didn't work with Django yet, but here's my default approach for checking if some component actually writes to redis during development:

First, I flush all keys stored in redis in order to remove old cache entries (never do this in production as this removes all data from redis):

> redis-cli FLUSHALL

Then activate caching in my application, and see what redis does:

> redis-cli MONITOR

You should enter an interactive session where you see every command sent to redis.

Reload your page and on your terminal you should see some SET* operations storing the cache data.

Reload again and if your cache works, you should see some GET* operations retrieving the cached data.

Note: with this method you can check if your cache is actually used. What you can't see is if your cache helps speeding up your application. For that you have to do performance tests as suggested in the comments.

Solution 2

You could install the django-debug-toolbar and see if the number of queries decreases when you enable caching. Though I don't think this is the best solution to the posed question, I still think you want to do this, as you can easily pinpoint costly queries using this setup, and then add the appropriate caching to them.

Share:
39,477
Matt Parrilla
Author by

Matt Parrilla

Co-founder at Ramble Maps

Updated on April 28, 2020

Comments

  • Matt Parrilla
    Matt Parrilla about 4 years

    I've installed django-redis-cache and redis-py. I've followed the caching docs for Django. As far as I know, the settings below are all that I need. But how do I tell if it's working properly??

    settings.py

        CACHES = {
            'default': {
                'BACKEND': 'redis_cache.RedisCache',
                'LOCATION': '<host>:<port>',
                'OPTIONS': {
                    'DB': mydb,
                    'PASSWORD': 'mydbspasswd',
                    'PARSER_CLASS': 'redis.connection.HiredisParser'
                },
            },
        }
    

    ...

        MIDDLEWARE_CLASSES = (
            'django.middleware.cache.UpdateCacheMiddleware',
             ...[the rest of my middleware]...
            'django.middleware.cache.FetchFromCacheMiddleware',
        )
    
        CACHE_MIDDLEWARE_ALIAS = 'default'
        CACHE_MIDDLEWARE_SECONDS = (60 * 60)
        CACHE_MIDDLEWARE_KEY_PREFIX = ''
    
  • Matt Parrilla
    Matt Parrilla over 12 years
    Cool @Max. Sounds perfect. Will check it out. I'll def come back and check it off when I confirm!