Skip to content

Commit

Permalink
catch and log memcached server errors (bug 568175)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbalogh committed May 26, 2010
1 parent e99e91c commit 9f59ddf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion django_pylibmc/__init__.py
@@ -1,2 +1,2 @@
VERSION = (0, 2, 1)
VERSION = (0, 2, 2)
__version__ = '.'.join(map(str, VERSION))
19 changes: 15 additions & 4 deletions django_pylibmc/memcached.py
Expand Up @@ -10,18 +10,22 @@
Unlike the default Django caching backends, this backend lets you pass 0 as a
timeout, which translates to an infinite timeout in memcached.
"""
import logging
import time

from django.conf import settings
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
from django.utils.encoding import smart_unicode, smart_str
from django.utils.encoding import smart_str

try:
import pylibmc
except ImportError:
raise InvalidCacheBackendError('Could not import pylibmc.')


log = logging.getLogger('django.pylibmc')


# It would be nice to inherit from Django's memcached backend, but that
# requires import python-memcache or cmemcache. Those probably aren't
# available since we're using pylibmc, hence the copy/paste.
Expand Down Expand Up @@ -54,7 +58,13 @@ def _get_memcache_timeout(self, timeout):
def add(self, key, value, timeout=None):
if isinstance(value, unicode):
value = value.encode('utf-8')
return self._cache.add(smart_str(key), value, self._get_memcache_timeout(timeout))
try:
return self._cache.add(smart_str(key), value,
self._get_memcache_timeout(timeout))
except pylibmc.ServerError:
log.error('ServerError saving %s => [%s]' % (key, value),
exc_info=True)
return False

def get(self, key, default=None):
val = self._cache.get(smart_str(key))
Expand All @@ -63,13 +73,14 @@ def get(self, key, default=None):
return val

def set(self, key, value, timeout=None):
self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout))
self._cache.set(smart_str(key), value,
self._get_memcache_timeout(timeout))

def delete(self, key):
self._cache.delete(smart_str(key))

def get_many(self, keys):
return self._cache.get_multi(map(smart_str,keys))
return self._cache.get_multi(map(smart_str, keys))

def close(self, **kwargs):
self._cache.disconnect_all()
Expand Down

0 comments on commit 9f59ddf

Please sign in to comment.