Browse Source

First stab at a cache backend

Ask Solem 15 years ago
parent
commit
f7dcff50f1
2 changed files with 42 additions and 0 deletions
  1. 41 0
      celery/backends/cache.py
  2. 1 0
      celery/conf.py

+ 41 - 0
celery/backends/cache.py

@@ -0,0 +1,41 @@
+import pylibmc
+
+from carrot.utils import partition
+
+from celery import conf
+from celery.backends.base import KeyValueStoreBackend
+from celery.utils import timeutils
+
+
+class CacheBackend(KeyValueStoreBackend):
+    Client = pylibmc.Client
+
+    _client = None
+
+    def __init__(self, expires=conf.TASK_RESULT_EXPIRES,
+            backend=conf.CELERY_CACHE_BACKEND, options={}, **kwargs):
+        super(CacheBackend, self).__init__(self, **kwargs)
+        if isinstance(expires, timedelta):
+            expires = timeutils.timedelta_seconds(expires)
+        self.expires = expires
+        self.options = dict(conf.CELERY_CACHE_BACKEND_OPTIONS, options)
+        self.backend, _, servers = partition(backend, "://")
+        self.servers = servers.split(";")
+        self.client = pylibmc.Client(servers, **options)
+
+        assert self.backend == "pylibmc"
+
+    def get(self, key):
+        return self.client.get(key)
+
+    def set(self, key, value):
+        return self.client.set(key, value, self.expires)
+
+    @property
+    def client(self):
+        if self._client is None:
+            self._client = self.Client(self.servers, **self.options)
+        return self._client
+
+
+

+ 1 - 0
celery/conf.py

@@ -118,6 +118,7 @@ EAGER_PROPAGATES_EXCEPTIONS = _get("CELERY_EAGER_PROPAGATES_EXCEPTIONS")
 RESULT_BACKEND = _get("CELERY_RESULT_BACKEND", compat=["CELERY_BACKEND"])
 CELERY_BACKEND = RESULT_BACKEND # FIXME Remove in 1.4
 CELERY_CACHE_BACKEND = _get("CELERY_CACHE_BACKEND")
+CELERY_CACHE_OPTIONS = _get("CELERY_CACHE_OPTIONS") or {}
 TASK_SERIALIZER = _get("CELERY_TASK_SERIALIZER")
 TASK_RESULT_EXPIRES = _get("CELERY_TASK_RESULT_EXPIRES")
 IGNORE_RESULT = _get("CELERY_IGNORE_RESULT")