diff --git a/django_pylibmc/__init__.py b/django_pylibmc/__init__.py index a98a624..5705ec1 100644 --- a/django_pylibmc/__init__.py +++ b/django_pylibmc/__init__.py @@ -1,2 +1,2 @@ -VERSION = (0, 2, 1) +VERSION = (0, 2, 2) __version__ = '.'.join(map(str, VERSION)) diff --git a/django_pylibmc/memcached.py b/django_pylibmc/memcached.py index c59edc7..0415b30 100644 --- a/django_pylibmc/memcached.py +++ b/django_pylibmc/memcached.py @@ -10,11 +10,12 @@ 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 @@ -22,6 +23,9 @@ 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. @@ -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)) @@ -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()