Browse Source

cache backend: Fallback to memcache if pylibmc not available

Ask Solem 15 years ago
parent
commit
f2055fbe08
2 changed files with 12 additions and 3 deletions
  1. 1 0
      celery/backends/__init__.py
  2. 11 3
      celery/backends/cache.py

+ 1 - 0
celery/backends/__init__.py

@@ -5,6 +5,7 @@ from celery.loaders import current_loader
 
 BACKEND_ALIASES = {
     "amqp": "celery.backends.amqp.AMQPBackend",
+    "cache": "celery.backends.cache.CacheBackend",
     "redis": "celery.backends.pyredis.RedisBackend",
     "mongodb": "celery.backends.mongodb.MongoBackend",
     "tyrant": "celery.backends.tyrant.TyrantBackend",

+ 11 - 3
celery/backends/cache.py

@@ -1,11 +1,19 @@
-import pylibmc
-
 from carrot.utils import partition
 
 from celery import conf
 from celery.backends.base import KeyValueStoreBackend
+from celery.exceptions import ImproperlyConfigured
 from celery.utils import timeutils
 
+try:
+    import pylibmc as memcache
+except ImportError:
+    try:
+        import memcache
+    except ImportError:
+        raise ImproperlyConfigured("Memcached backend requires either "
+                                   "the 'memcache' or 'pylibmc' library")
+
 
 class CacheBackend(KeyValueStoreBackend):
     Client = pylibmc.Client
@@ -21,7 +29,7 @@ class CacheBackend(KeyValueStoreBackend):
         self.options = dict(conf.CELERY_CACHE_BACKEND_OPTIONS, options)
         self.backend, _, servers = partition(backend, "://")
         self.servers = servers.split(";")
-        self.client = pylibmc.Client(servers, **options)
+        self.client = memcache.Client(servers, **options)
 
         assert self.backend == "pylibmc"