Skip to content

Add missing pylibmc client methods; adds graceful handling of memcached errors #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_elasticache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION = (1, 0, 3)
VERSION = (1, 0, 4)
__version__ = '.'.join(map(str, VERSION))
18 changes: 16 additions & 2 deletions django_elasticache/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
Backend for django cache
"""
import socket
import logging

from functools import wraps
from django.core.cache import InvalidCacheBackendError
from django.core.cache.backends.memcached import PyLibMCCache
from .cluster_utils import get_cluster_info


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


def invalidate_cache_after_error(f):
"""
catch any exception and invalidate internal cache with list of nodes
Expand All @@ -16,9 +21,10 @@ def invalidate_cache_after_error(f):
def wrapper(self, *args, **kwds):
try:
return f(self, *args, **kwds)
except Exception:
except Exception as e:
log.warning('MemcachedError: %s', e, exc_info=True)
self.clear_cluster_nodes_cache()
raise
return None # Treat as a cache miss
return wrapper


Expand Down Expand Up @@ -103,6 +109,10 @@ def _cache(self):

return client

@invalidate_cache_after_error
def add(self, *args, **kwargs):
return super(ElastiCache, self).add(*args, **kwargs)

@invalidate_cache_after_error
def get(self, *args, **kwargs):
return super(ElastiCache, self).get(*args, **kwargs)
Expand All @@ -122,3 +132,7 @@ def set_many(self, *args, **kwargs):
@invalidate_cache_after_error
def delete(self, *args, **kwargs):
return super(ElastiCache, self).delete(*args, **kwargs)

@invalidate_cache_after_error
def delete_many(self, *args, **kwargs):
return super(ElastiCache, self).delete_many(*args, **kwargs)