Skip to content

Commit f2055fb

Browse files
author
Ask Solem
committed
cache backend: Fallback to memcache if pylibmc not available
1 parent f7dcff5 commit f2055fb

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

celery/backends/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
BACKEND_ALIASES = {
77
"amqp": "celery.backends.amqp.AMQPBackend",
8+
"cache": "celery.backends.cache.CacheBackend",
89
"redis": "celery.backends.pyredis.RedisBackend",
910
"mongodb": "celery.backends.mongodb.MongoBackend",
1011
"tyrant": "celery.backends.tyrant.TyrantBackend",

celery/backends/cache.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import pylibmc
2-
31
from carrot.utils import partition
42

53
from celery import conf
64
from celery.backends.base import KeyValueStoreBackend
5+
from celery.exceptions import ImproperlyConfigured
76
from celery.utils import timeutils
87

8+
try:
9+
import pylibmc as memcache
10+
except ImportError:
11+
try:
12+
import memcache
13+
except ImportError:
14+
raise ImproperlyConfigured("Memcached backend requires either "
15+
"the 'memcache' or 'pylibmc' library")
16+
917

1018
class CacheBackend(KeyValueStoreBackend):
1119
Client = pylibmc.Client
@@ -21,7 +29,7 @@ def __init__(self, expires=conf.TASK_RESULT_EXPIRES,
2129
self.options = dict(conf.CELERY_CACHE_BACKEND_OPTIONS, options)
2230
self.backend, _, servers = partition(backend, "://")
2331
self.servers = servers.split(";")
24-
self.client = pylibmc.Client(servers, **options)
32+
self.client = memcache.Client(servers, **options)
2533

2634
assert self.backend == "pylibmc"
2735

0 commit comments

Comments
 (0)