Skip to content

Instantly share code, notes, and snippets.

@ipmb
Last active September 27, 2020 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ipmb/b93e234333dfd25934e5 to your computer and use it in GitHub Desktop.
Save ipmb/b93e234333dfd25934e5 to your computer and use it in GitHub Desktop.
import logging
from functools import wraps
from django.core.cache.backends.memcached import PyLibMCCache
logger = logging.getLogger(__name__)
def fault_tolerant_wrapper(f):
@wraps(f)
def wrapper(*args, **kwargs):
try:
result = f(*args, **kwargs)
except Exception as err:
logger.error(u"cache.%s failed with args: %s", f.__name__, args)
logger.exception(err)
result = None
return result
return wrapper
class FaultTolerantCacheMixin(object):
"""
Wraps memcache client methods allowing them to fail
without raising an exception.
"""
methods_to_patch = ('get', 'set', 'incr', 'decr', 'delete',
'get_multi', 'set_multi', 'delete_multi')
@property
def _cache(self):
"""
Implements transparent thread-safe access to a memcached client.
"""
if getattr(self, '_client', None) is None:
self._client = self._lib.Client(self._servers)
for name in self.methods_to_patch:
method = fault_tolerant_wrapper(getattr(self._client, name))
setattr(self._client, name, method)
return self._client
class FaultTolerantPyLibMCCache(FaultTolerantCacheMixin, PyLibMCCache):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment