Johnny Cache on Heroku / Redis
15 Jun 2012
Hooking up Johnny Cache and seeing your SQL query count drop down to next to nothing is a really satisfying feeling. Recently we've taken on a client running on Heroku, which enforces certain constraints meaning we had to do a few additional things to get Johnny running.
Heroku's memcache add-on requires SASL authentication, which isn't currently supported by the Django provided pylibmc cache backend. There's a django-pylibmc-sasl module, which is a drop in solution for using memcache as Django's cache directly, but this isn't supported by Johnny out of the box, so the the best solution seems to be to use Redis instead.
The stable release of johnny-cache now supports Redis, so we can install it from PyPI along with redis and django-redis-cache.
pip install johnny-cache redis django-redis-cache
If you're doing this on Heroku, then we need to add the Redis To Go add on. The smallest option with persistent connections is the $5/month "mini".
heroku addons:add redistogo:mini
So in total the Django CACHES setting to use Johnny on our Heroku site looks like this:
from urlparse import urlparse
redis_url = urlparse(os.environ.get('REDISTOGO_URL', 'http://localhost:6379'))
CACHES = {
'default': {
'BACKEND': 'johnny.backends.redis.RedisCache',
'LOCATION': '%s:%s' % (redis_url.hostname, redis_url.port),
'JOHNNY_CACHE': True,
'OPTIONS': {
'DB': 0,
'PASSWORD': redis_url.password,
}
}
}
Cue happy feeling.
Rolo.