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??
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 = ''
To start Redis client, open the terminal and type the command redis-cli. This will connect to your local server and now you can run any command. In the above example, we connect to Redis server running on the local machine and execute a command PING, that checks whether the server is running or not.
For a clustered cache, you would see shards instead. From there, you can expand a cache resource to view all the keys inside it. By clicking on a Redis key name, all its contents will open in a new editor tab. With a collection type Redis key, clicking on it will reveal the individual elements under the key name.
ElastiCache supports both cluster mode disabled and cluster mode enabled Redis clusters. To test a connection to these clusters, you can use the redis-cli utility. The latest version of redis-cli also supports SSL/TLS for connecting to clusters with encryption and/or authentication turned on.
The way to test for this in Redis is to simply query the key. If the key is empty, populate it. If it is a string use get (or exists). If it is a hash then query for the specific members you need.
Didnt work with Django yet, but 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 a 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 cant see is if your cache helps speeding up your application. For that you have to do performance tests as suggested in the comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With