Browse Source

Cache backend now respects CELERY_TASK_RESULT_EXPIRES. Thanks to mulka

Ask Solem 15 years ago
parent
commit
0ae5d07710
4 changed files with 26 additions and 8 deletions
  1. 11 1
      celery/backends/cache.py
  2. 2 4
      celery/task/base.py
  3. 11 0
      celery/utils/__init__.py
  4. 2 3
      docs/configuration.rst

+ 11 - 1
celery/backends/cache.py

@@ -1,9 +1,12 @@
 """celery.backends.cache"""
+from datetime import timedelta
+
 from django.utils.encoding import smart_str
 from django.core.cache import cache, get_cache
 from django.core.cache.backends.base import InvalidCacheBackendError
 
 from celery import conf
+from celery.utils import timedelta_seconds
 from celery.backends.base import KeyValueStoreBackend
 
 # CELERY_CACHE_BACKEND overrides the django-global(tm) backend settings.
@@ -45,8 +48,15 @@ else:
 class CacheBackend(KeyValueStoreBackend):
     """Backend using the Django cache framework to store task metadata."""
 
+    def __init__(self, *args, **kwargs):
+        super(CacheBackend, self).__init__(self, *args, **kwargs)
+        expires = conf.TASK_RESULT_EXPIRES
+        if isinstance(expires, timedelta):
+            expires = timedelta_seconds(conf.TASK_RESULT_EXPIRES)
+        self.expires = expires
+
     def get(self, key):
         return cache.get(key)
 
     def set(self, key, value):
-        cache.set(key, value)
+        cache.set(key, value, self.expires)

+ 2 - 4
celery/task/base.py

@@ -7,7 +7,7 @@ from billiard.serialization import pickle
 
 from celery import conf
 from celery.log import setup_logger
-from celery.utils import gen_unique_id, get_full_cls_name, mexpand
+from celery.utils import gen_unique_id, mexpand, timedelta_seconds
 from celery.result import BaseAsyncResult, TaskSetResult, EagerResult
 from celery.execute import apply_async, apply
 from celery.registry import tasks
@@ -639,9 +639,7 @@ class PeriodicTask(Task):
         Doesn't account for negative timedeltas.
 
         """
-        if delta.days < 0:
-            return 0
-        return delta.days * 86400 + delta.seconds + (delta.microseconds / 10e5)
+        return timedelta_seconds(delta)
 
     def is_due(self, last_run_at):
         """Returns tuple of two items ``(is_due, next_time_to_run)``,

+ 11 - 0
celery/utils/__init__.py

@@ -163,3 +163,14 @@ def fun_takes_kwargs(fun, kwlist=[]):
     if keywords != None:
         return kwlist
     return filter(curry(operator.contains, args), kwlist)
+
+
+def timedelta_seconds(delta):
+    """Convert :class:`datetime.timedelta` to seconds.
+
+    Doesn't account for negative values.
+
+    """
+    if delta.days < 0:
+        return 0
+    return delta.days * 86400 + delta.seconds + (delta.microseconds / 10e5)

+ 2 - 3
docs/configuration.rst

@@ -352,9 +352,8 @@ Task execution settings
     Time (in seconds, or a :class:`datetime.timedelta` object) for when after
     stored task tombstones are deleted.
 
-    **NOTE**: For the moment this only works for the database and MongoDB
-    backends., except the result has already
-    been evaluated.
+    **NOTE**: For the moment this only works with the database, cache and MongoDB
+    backends.
 
 * CELERY_TASK_SERIALIZER
     A string identifying the default serialization