About Posts Notes Reading
Posts

Using Johnny Cache with Heroku and Memcachier

· 2 min read

Johnny Cache is a great Django caching framework that provides transparent query caching. It works well out of the box for many setups, but I ran into some issues getting it to work on Heroku with Memcachier.

The Problem

Johnny Cache doesn’t support SASL authentication out of the box, which is required for Memcachier. Memcachier recommends using django-pylibmc’s libmemcached backend, but that isn’t natively compatible with Johnny Cache either.

So what do you do when your tools don’t play nice together? You build a bridge.

The Solution

The fix is to create a custom backend that inherits from django-pylibmc’s PyLibMCCache and adds the timeout handling that Johnny Cache expects.

Here’s the approach:

  1. Create a custom backend class that inherits from PyLibMCCache
  2. Override the timeout method with Johnny Cache’s implementation
  3. Configure Django to use your new backend

The key is replacing the _get_memcache_timeout method with the version from Johnny Cache’s backend. This gives you SASL authentication from django-pylibmc while maintaining compatibility with Johnny Cache.

Configuration

In your Django settings, make sure to set BINARY to True in your CACHES configuration. This is required for SASL authentication to work properly.

CACHES = {
    'default': {
        'BACKEND': 'path.to.your.CustomBackend',
        'BINARY': True,
        # ... other settings
    }
}

Conclusion

Sometimes the tools you need don’t work together out of the box. When that happens, a little inheritance and method overriding can go a long way. The custom backend approach lets you combine the best of both libraries.