Permalink
Browse files

added a retry-loop in the django class

  • Loading branch information...
1 parent d1c05b0 commit 3044f55c0ada3920c544746e2891216adb51121e @tarekziade tarekziade committed Dec 10, 2012
Showing with 33 additions and 28 deletions.
  1. +33 −28 memcachepool/cache.py
View
@@ -37,6 +37,18 @@ def __init__(self, server, params):
self._pool = ClientPool(self._get_client, maxsize=self.maxsize,
wait_for_connection=self.socktimeout)
self._blacklist = {}
+ self.retries = int(params.get('MAX_RETRIES', 3))
+
+ def call(self, func, *args, **kwargs):
+ retries = 0
+ while retries < self.retries:
+ with self._pool.reserve() as conn:
+ try:
+ return getattr(conn, func)(*args, **kwargs)
+ except Exception, exc:
+ # log
+ retries += 1
+ raise exc
# XXX using python-memcached style pickling
# but maybe we could use something else like
@@ -119,20 +131,19 @@ def _value_for_flag(self, value, flag):
def add(self, key, value, timeout=0, version=None):
flag = self._flag_for_value(value)
- if flag == self._FLAG_SERIALIED:
+ if flag == self._FLAG_SERIALIZED:
value = self.serialize(value)
else:
value = '%d' % value
+
key = self.make_key(key, version=version)
- with self._pool.reserve() as conn:
- return conn.add(key, value, self._get_memcache_timeout(timeout),
- flag)
+ return self.call('add', value, self._get_memcache_timeout(timeout),
@phpdude
phpdude Sep 21, 2014

you missed key param! I lost 5 hours for debugging, thought my code was wrong :(

+ flag)
def get(self, key, default=None, version=None):
key = self.make_key(key, version=version)
- with self._pool.reserve() as conn:
- val = conn.get(key)
+ val = self.call('get', key)
if val is None:
return default
@@ -146,13 +157,11 @@ def set(self, key, value, timeout=0, version=None):
else:
value = '%d' % value
key = self.make_key(key, version=version)
- with self._pool.reserve() as conn:
- conn.set(key, value, self._get_memcache_timeout(timeout), flag)
+ self.call('set', key, value, self._get_memcache_timeout(timeout), flag)
def delete(self, key, version=None):
key = self.make_key(key, version=version)
- with self._pool.reserve() as conn:
- conn.delete(key)
+ self.call('delete', key)
def get_many(self, keys, version=None):
if keys == {}:
@@ -161,12 +170,12 @@ def get_many(self, keys, version=None):
new_keys = map(lambda x: self.make_key(x, version=version), keys)
ret = {}
- with self._pool.reserve() as conn:
- for key in new_keys:
- res = conn.get(key)
- if res is None:
- continue
- ret[key] = res
+
+ for key in new_keys:
+ res = self.call('get', key)
+ if res is None:
+ continue
+ ret[key] = res
if ret:
res = {}
@@ -186,8 +195,7 @@ def close(self, **kwargs):
def incr(self, key, delta=1, version=None):
key = self.make_key(key, version=version)
try:
- with self._pool.reserve() as conn:
- val = conn.incr(key, delta)
+ val = self.call('incr', key, delta)
# python-memcache responds to incr on non-existent keys by
# raising a ValueError, pylibmc by raising a pylibmc.NotFound
@@ -202,8 +210,7 @@ def incr(self, key, delta=1, version=None):
def decr(self, key, delta=1, version=None):
key = self.make_key(key, version=version)
try:
- with self._pool.reserve() as conn:
- val = conn.decr(key, delta)
+ val = self.call('decr', key, delta)
# python-memcache responds to incr on non-existent keys by
# raising a ValueError, pylibmc by raising a pylibmc.NotFound
@@ -226,15 +233,13 @@ def set_many(self, data, timeout=0, version=None):
value = '%d' % value
safe_data[key] = value
- with self._pool.reserve() as conn:
- for key, value in safe_data.items():
- conn.set(key, value, self._get_memcache_timeout(timeout), flag)
+ for key, value in safe_data.items():
+ self.call('set', key, value, self._get_memcache_timeout(timeout),
+ flag)
def delete_many(self, keys, version=None):
- with self._pool.reserve() as conn:
- for key in keys:
- conn.delete(self.make_key(key, version=version))
+ for key in keys:
+ self.call('delete', self.make_key(key, version=version))
def clear(self):
- with self._pool.reserve() as conn:
- conn.flush_all()
+ self.call('flush_all')

0 comments on commit 3044f55

Please sign in to comment.