Skip to content

Commit f7dcff5

Browse files
author
Ask Solem
committed
First stab at a cache backend
1 parent 8d8fefd commit f7dcff5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

celery/backends/cache.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+

celery/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def _get(name, default=None, compat=None):
118118
RESULT_BACKEND = _get("CELERY_RESULT_BACKEND", compat=["CELERY_BACKEND"])
119119
CELERY_BACKEND = RESULT_BACKEND # FIXME Remove in 1.4
120120
CELERY_CACHE_BACKEND = _get("CELERY_CACHE_BACKEND")
121+
CELERY_CACHE_OPTIONS = _get("CELERY_CACHE_OPTIONS") or {}
121122
TASK_SERIALIZER = _get("CELERY_TASK_SERIALIZER")
122123
TASK_RESULT_EXPIRES = _get("CELERY_TASK_RESULT_EXPIRES")
123124
IGNORE_RESULT = _get("CELERY_IGNORE_RESULT")

0 commit comments

Comments
 (0)