Skip to content

Instantly share code, notes, and snippets.

@SamLiu79
Forked from matclayton/pylibmcd.py
Created June 9, 2010 07:31
Show Gist options
  • Save SamLiu79/431171 to your computer and use it in GitHub Desktop.
Save SamLiu79/431171 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
Use it by setting CACHE_BACKEND in settings.py, e.g.:
CACHE_BACKEND = 'utils.backends.cache.pylibmcd://127.0.0.1:11211/'
NEW: can be transmit pylibmc cache behaviors to cache backend
for example:
# Pylibmcd cache behaviors , for more info please check http://sendapatch.se/projects/pylibmc/behaviors.html
CACHE_BEHAVIORS = {
'tcp_nodelay':True,
'distribution':'consistent ketama',
'ketama_hash':'murmur',
'failure_limit':3,
'cas':True,
'cache_lookups':True,
'connect_timeout':1,
'no_block':True,
'threadpool':None, #threadpool or aio, if given None will be use aio, else use threadpool and set maximum thread number
}
put above to django project's settings.py
fork from gist: 334755, thanks Matclayton, http://gist.github.com/matclayton
"""
from __future__ import with_statement
from django.conf import settings
from django.core.cache.backends.base import BaseCache
from django.utils.encoding import smart_unicode, smart_str
import pylibmc
import _pylibmc
import sys
class CacheClass(BaseCache):
def __init__(self, server, params):
super(CacheClass, self).__init__(params)
mc = pylibmc.Client(server.split(';'))
if settings.CACHE_BEHAVIORS:
mc.behaviors = settings.CACHE_BEHAVIORS
else:
mc.behaviors = params
if settings.CACHE_BEHAVIORS['threadpool'] == None:
self._pool = pylibmc.ThreadMappedPool(mc)
self._close = True
else:
self._pool = pylibmc.ClientPool(mc, settings.CACHE_BEHAVIORS['threadpool'])
self._close = False
def _call(self, method, *params):
with self._pool.reserve() as mc:
return getattr(mc, method)(*params)
def add(self, key, value, timeout=None):
if isinstance(value, unicode):
value = value.encode('utf-8')
return self._call('add', smart_str(key), value,
self.default_timeout if timeout is None else timeout)
def get(self, key, default=None):
val = self._call('get', smart_str(key))
if val is None:
return default
else:
if isinstance(val, basestring):
return smart_unicode(val)
else:
return val
def set(self, key, value, timeout=None):
if isinstance(value, unicode):
value = value.encode('utf-8')
self._call('set', smart_str(key), value,
self.default_timeout if timeout is None else timeout)
def delete(self, key):
self._call('delete', smart_str(key))
def get_many(self, keys):
return self._call('get_multi', map(smart_str, keys))
def set_many(self, mapping, timeout=None):
return self._call('set_multi',
dict((smart_str(key), val) for key, val in mapping.iteritems()),
self.default_timeout if timeout is None else timeout)
def close(self, **kwargs):
if self._close:
self._pool.master.disconnect_all()
def incr(self, key, delta=1):
return self._call('incr', smart_str(key), delta)
def decr(self, key, delta=1):
return self._call('decr', smart_str(key), delta)
def has_key(self, key):
val = self._call('get', smart_str(key))
if val is None:
return False
else:
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment