Skip to content
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

Wrap cache.get in try-except block #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions django_elasticache/memcached.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
"""
Backend for django cache
"""
import logging
import socket
from functools import wraps
from django.core.cache import InvalidCacheBackendError
from django.core.cache.backends.memcached import BaseMemcachedCache
from threading import local
from .cluster_utils import get_cluster_info

try:
import pylibmc
from pylibmc import Error as MemcachedError
except ImportError:
raise InvalidCacheBackendError('Could not import pylibmc.')


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


def invalidate_cache_after_error(f):
"""
Expand Down Expand Up @@ -107,8 +117,13 @@ def _cache(self):
return client

@invalidate_cache_after_error
def get(self, *args, **kwargs):
return super(ElastiCache, self).get(*args, **kwargs)
def get(self, key, default=None, version=None):
try:
return super(ElastiCache, self).get(*args, **kwargs)
except MemcachedError as e:
logger.error('MemcachedError: %s', e, exc_info=True)
return default


@invalidate_cache_after_error
def get_many(self, *args, **kwargs):
Expand Down