From ce07f2a7dec12b9bdd3fa1991ad286b5ecef57a4 Mon Sep 17 00:00:00 2001 From: Bob Cribbs Date: Wed, 27 Apr 2016 13:17:57 +0300 Subject: [PATCH 1/3] refs #59 add wrapper around `cache.get()` --- jobtastic/task.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/jobtastic/task.py b/jobtastic/task.py index 08658da..79a8ffc 100644 --- a/jobtastic/task.py +++ b/jobtastic/task.py @@ -215,6 +215,19 @@ def simulate_async_error(self, exception): return async_result + def _get_cache_value(self, key, prefix=None): + """ + Wrapper around ``cache.get()``. + Use this if you want to bust cache in some scenarios. + """ + if prefix is not None: + key = "{prefix}:{key}".format( + prefix=prefix, + key=key, + ) + + return cache.get(key) + @classmethod def apply_async(self, args, kwargs, **options): """ @@ -230,7 +243,7 @@ def apply_async(self, args, kwargs, **options): cache_key = self._get_cache_key(**kwargs) # Check for an already-computed and cached result - task_id = cache.get(cache_key) # Check for the cached result + task_id = self._get_cache_value(cache_key) # Check for the cached result if task_id: # We've already built this result, just latch on to the task that # did the work @@ -239,7 +252,7 @@ def apply_async(self, args, kwargs, **options): return self.AsyncResult(task_id) # Check for an in-progress equivalent task to avoid duplicating work - task_id = cache.get('herd:%s' % cache_key) + task_id = self._get_cache_value(cache_key, prefix='herd') # Check for the cached result if task_id: logging.info('Found existing in-progress task: %s', task_id) return self.AsyncResult(task_id) From c8414f51b80d11af0a1e70b24fc728908d25564c Mon Sep 17 00:00:00 2001 From: Bob Cribbs Date: Wed, 27 Apr 2016 13:34:57 +0300 Subject: [PATCH 2/3] make `_get_cache_value` a class method --- jobtastic/task.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/jobtastic/task.py b/jobtastic/task.py index 79a8ffc..a049761 100644 --- a/jobtastic/task.py +++ b/jobtastic/task.py @@ -215,19 +215,6 @@ def simulate_async_error(self, exception): return async_result - def _get_cache_value(self, key, prefix=None): - """ - Wrapper around ``cache.get()``. - Use this if you want to bust cache in some scenarios. - """ - if prefix is not None: - key = "{prefix}:{key}".format( - prefix=prefix, - key=key, - ) - - return cache.get(key) - @classmethod def apply_async(self, args, kwargs, **options): """ @@ -243,7 +230,7 @@ def apply_async(self, args, kwargs, **options): cache_key = self._get_cache_key(**kwargs) # Check for an already-computed and cached result - task_id = self._get_cache_value(cache_key) # Check for the cached result + task_id = self._get_cache_value(key=cache_key) # Check for the cached result if task_id: # We've already built this result, just latch on to the task that # did the work @@ -252,7 +239,7 @@ def apply_async(self, args, kwargs, **options): return self.AsyncResult(task_id) # Check for an in-progress equivalent task to avoid duplicating work - task_id = self._get_cache_value(cache_key, prefix='herd') # Check for the cached result + task_id = self._get_cache_value(key=cache_key, prefix='herd') # Check for the cached result if task_id: logging.info('Found existing in-progress task: %s', task_id) return self.AsyncResult(task_id) @@ -461,6 +448,20 @@ def _get_cache_key(self, **kwargs): cache_prefix = '%s.%s' % (self.__module__, self.__name__) return '%s:%s' % (cache_prefix, m.hexdigest()) + @classmethod + def _get_cache_value(self, key, prefix=None): + """ + Wrapper around ``cache.get()``. + Use this if you want to bust cache in some scenarios. + """ + if prefix is not None: + key = "{prefix}:{key}".format( + prefix=prefix, + key=key, + ) + + return cache.get(key) + def _get_memory_usage(self): current_process = psutil.Process(os.getpid()) usage = current_process.memory_info() From 28bca908994aa08282865bbdebfba03b4b2dcb08 Mon Sep 17 00:00:00 2001 From: Bob Cribbs Date: Wed, 27 Apr 2016 13:59:53 +0300 Subject: [PATCH 3/3] refs #59 flake8 --- jobtastic/task.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jobtastic/task.py b/jobtastic/task.py index a049761..632f37e 100644 --- a/jobtastic/task.py +++ b/jobtastic/task.py @@ -230,7 +230,7 @@ def apply_async(self, args, kwargs, **options): cache_key = self._get_cache_key(**kwargs) # Check for an already-computed and cached result - task_id = self._get_cache_value(key=cache_key) # Check for the cached result + task_id = self._get_cache_value(key=cache_key) if task_id: # We've already built this result, just latch on to the task that # did the work @@ -239,7 +239,7 @@ def apply_async(self, args, kwargs, **options): return self.AsyncResult(task_id) # Check for an in-progress equivalent task to avoid duplicating work - task_id = self._get_cache_value(key=cache_key, prefix='herd') # Check for the cached result + task_id = self._get_cache_value(key=cache_key, prefix='herd') if task_id: logging.info('Found existing in-progress task: %s', task_id) return self.AsyncResult(task_id)