Skip to content

Instantly share code, notes, and snippets.

@mgrouchy
Last active December 13, 2015 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mgrouchy/4956137 to your computer and use it in GitHub Desktop.
Save mgrouchy/4956137 to your computer and use it in GitHub Desktop.
Use Johnny Cache with Memcachier. These Changes are required to make it work on Heroku with Memcachier as johnny-cache uses the Django Pylibmc backend which doesn't support SASL.
"""
This is taken wholesale from JohnnyCache except instead of overriding
_get_memcache_timeout in the Django pylibmc backend, we use the Pylibmc
backend from django_pylibmc which supports SASL
"""
from django_pylibmc.memcached import PyLibMCCache
class JohnnyPyLibMCCache(PyLibMCCache):
"""
PyLibMCCache version that interprets 0 to mean, roughly, 30 days.
This is because `pylibmc interprets 0 to mean literally zero seconds
<http://sendapatch.se/projects/pylibmc/misc.html#differences-from-python-memcached>`_
rather than "infinity" as memcached itself does. The maximum timeout
memcached allows before treating the timeout as a timestamp is just
under 30 days.
"""
def _get_memcache_timeout(self, timeout=None):
# pylibmc doesn't like our definition of 0
if timeout == 0:
return 2591999
return super(JohnnyPyLibMCCache, self)._get_memcache_timeout(timeout)
# if you are using Memcachier these are the settings
os.environ['MEMCACHE_SERVERS'] = os.environ.get('MEMCACHIER_SERVERS', '').replace(',', ';')
os.environ['MEMCACHE_USERNAME'] = os.environ.get('MEMCACHIER_USERNAME', '')
os.environ['MEMCACHE_PASSWORD'] = os.environ.get('MEMCACHIER_PASSWORD', '')
# some johnny with your cache backend rather then the Johnny-Cache backend.
CACHES = {
'default': dict(
BACKEND='yourapp.cache.backends.JohnnyPyLibMCCache',
BINARY=True,
LOCATION=os.environ.get('MEMCACHIER_SERVERS', '').replace(',', ';'),
JOHNNY_CACHE=True,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment