|
| 1 | +import pylibmc |
| 2 | + |
| 3 | +from carrot.utils import partition |
| 4 | + |
| 5 | +from celery import conf |
| 6 | +from celery.backends.base import KeyValueStoreBackend |
| 7 | +from celery.utils import timeutils |
| 8 | + |
| 9 | + |
| 10 | +class CacheBackend(KeyValueStoreBackend): |
| 11 | + Client = pylibmc.Client |
| 12 | + |
| 13 | + _client = None |
| 14 | + |
| 15 | + def __init__(self, expires=conf.TASK_RESULT_EXPIRES, |
| 16 | + backend=conf.CELERY_CACHE_BACKEND, options={}, **kwargs): |
| 17 | + super(CacheBackend, self).__init__(self, **kwargs) |
| 18 | + if isinstance(expires, timedelta): |
| 19 | + expires = timeutils.timedelta_seconds(expires) |
| 20 | + self.expires = expires |
| 21 | + self.options = dict(conf.CELERY_CACHE_BACKEND_OPTIONS, options) |
| 22 | + self.backend, _, servers = partition(backend, "://") |
| 23 | + self.servers = servers.split(";") |
| 24 | + self.client = pylibmc.Client(servers, **options) |
| 25 | + |
| 26 | + assert self.backend == "pylibmc" |
| 27 | + |
| 28 | + def get(self, key): |
| 29 | + return self.client.get(key) |
| 30 | + |
| 31 | + def set(self, key, value): |
| 32 | + return self.client.set(key, value, self.expires) |
| 33 | + |
| 34 | + @property |
| 35 | + def client(self): |
| 36 | + if self._client is None: |
| 37 | + self._client = self.Client(self.servers, **self.options) |
| 38 | + return self._client |
| 39 | + |
| 40 | + |
| 41 | + |
0 commit comments