From 5ba7d3c2dbaab5675653d89cae92b045cd7013ff Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 30 Oct 2017 11:34:21 -0400 Subject: [PATCH 1/3] Treat any Exceptions with the cache as a cache miss. Logs the error as a warning but return False. Also add the other missing memcached client end-points. --- django_elasticache/memcached.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/django_elasticache/memcached.py b/django_elasticache/memcached.py index a78fbd7..6074faa 100644 --- a/django_elasticache/memcached.py +++ b/django_elasticache/memcached.py @@ -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 @@ -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 False return wrapper @@ -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) @@ -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) From 7f7e6c3ae5984231a882af67d0df0de0411b0b84 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 30 Oct 2017 11:39:08 -0400 Subject: [PATCH 2/3] increment version number --- django_elasticache/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_elasticache/__init__.py b/django_elasticache/__init__.py index 81598db..65f9591 100644 --- a/django_elasticache/__init__.py +++ b/django_elasticache/__init__.py @@ -1,2 +1,2 @@ -VERSION = (1, 0, 3) +VERSION = (1, 0, 4) __version__ = '.'.join(map(str, VERSION)) From 4cca2a805ab695ef91ee4fb92210858dd4e4e8bb Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 30 Oct 2017 13:47:52 -0400 Subject: [PATCH 3/3] More logical default for a cache miss --- django_elasticache/memcached.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_elasticache/memcached.py b/django_elasticache/memcached.py index 6074faa..0b0caf3 100644 --- a/django_elasticache/memcached.py +++ b/django_elasticache/memcached.py @@ -24,7 +24,7 @@ def wrapper(self, *args, **kwds): except Exception as e: log.warning('MemcachedError: %s', e, exc_info=True) self.clear_cluster_nodes_cache() - return False + return None # Treat as a cache miss return wrapper