Skip to content

Commit

Permalink
Return empty dict from get_many()
Browse files Browse the repository at this point in the history
- Return an empty dict from django_redis.cache.RedisCache.get_many() when
  IGNORE_EXCEPTIONS is True.
- Convert omit_exception decorator to a callable. Pass it a return_value
  keyword argument to specify a default return value when
  IGNORE_EXCEPTIONS is True.
  • Loading branch information
ecmendenhall authored and niwinz committed Jul 31, 2015
1 parent 3095fda commit 0f3f546
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 7 additions & 8 deletions django_redis/cache.py
Expand Up @@ -16,26 +16,25 @@
logger = logging.getLogger((DJANGO_REDIS_LOGGER or __name__))


def omit_exception(method):
def omit_exception(method=None, return_value=None):
"""
Simple decorator that intercepts connection
errors and ignores these if settings specify this.
Note: this doesn't handle the `default` argument in .get().
"""

if method is None:
return functools.partial(omit_exception, return_value=return_value)

@functools.wraps(method)
def _decorator(self, *args, **kwargs):
try:
return method(self, *args, **kwargs)
except ConnectionInterrupted as e:
if self._ignore_exceptions:

if DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS:
logger.error(str(e))

return None

return return_value
raise e.parent

return _decorator
Expand Down Expand Up @@ -79,7 +78,7 @@ def add(self, *args, **kwargs):
def get(self, key, default=None, version=None, client=None):
try:
return self.client.get(key, default=default, version=version,
client=client)
client=client)
except ConnectionInterrupted:
if self._ignore_exceptions:
if DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS:
Expand All @@ -103,7 +102,7 @@ def delete_many(self, *args, **kwargs):
def clear(self):
return self.client.clear()

@omit_exception
@omit_exception(return_value={})
def get_many(self, *args, **kwargs):
return self.client.get_many(*args, **kwargs)

Expand Down
6 changes: 6 additions & 0 deletions tests/redis_backend_testapp/tests.py
Expand Up @@ -547,9 +547,15 @@ def setUp(self):
self._orig_setting = django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = True
self.cache = get_cache("doesnotexist")
self.cache._orig_ignore_exceptions = self.cache._ignore_exceptions
self.cache._ignore_exceptions = True

def tearDown(self):
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = self._orig_setting
self.cache._ignore_exceptions = self.cache._orig_ignore_exceptions

def test_get_many_returns_default_arg(self):
self.assertEqual(self.cache.get_many(["key1", "key2", "key3"]), {})

def test_get(self):
self.assertIsNone(self.cache.get("key"))
Expand Down

0 comments on commit 0f3f546

Please sign in to comment.