Ticket #11675: memcached.py.diff

File memcached.py.diff, 2.2 KB (added by anonymous, 15 years ago)
Line 
1@@ -4,22 +4,28 @@
2 from django.utils.encoding import smart_unicode, smart_str
3
4 try:
5- import cmemcache as memcache
6+ import pylibmc as memcache
7 except ImportError:
8 try:
9- import memcache
10- except:
11- raise InvalidCacheBackendError("Memcached cache backend requires either the 'memcache' or 'cmemcache' library")
12+ import cmemcache as memcache
13+ except ImportError:
14+ try:
15+ import memcache
16+ except:
17+ raise InvalidCacheBackendError(
18+ "Memcached cache backend requires either the 'memcache' or 'cmemcache' library")
19
20 class CacheClass(BaseCache):
21 def __init__(self, server, params):
22 BaseCache.__init__(self, params)
23 self._cache = memcache.Client(server.split(';'))
24
25- def add(self, key, value, timeout=0):
26+ def add(self, key, value, timeout=None):
27 if isinstance(value, unicode):
28 value = value.encode('utf-8')
29- return self._cache.add(smart_str(key), value, timeout or self.default_timeout)
30+ # if timeout is 0, it is eternal cache.
31+ return self._cache.add(smart_str(key), value,
32+ self.default_timeout if timeout is None else timeout)
33
34 def get(self, key, default=None):
35 val = self._cache.get(smart_str(key))
36@@ -31,10 +37,11 @@
37 else:
38 return val
39
40- def set(self, key, value, timeout=0):
41+ def set(self, key, value, timeout=None):
42 if isinstance(value, unicode):
43 value = value.encode('utf-8')
44- self._cache.set(smart_str(key), value, timeout or self.default_timeout)
45+ self._cache.set(smart_str(key), value,
46+ self.default_timeout if timeout is None else timeout)
47
48 def delete(self, key):
49 self._cache.delete(smart_str(key))
50@@ -45,8 +52,17 @@
51 def close(self, **kwargs):
52 self._cache.disconnect_all()
53
54+ # a convenient function(needed?)
55+ def disconnect_all(self, **kwargs):
56+ self._cache.disconnect_all()
57+
58 def incr(self, key, delta=1):
59 return self._cache.incr(key, delta)
60
61 def decr(self, key, delta=1):
62 return self._cache.decr(key, delta)
63+
64+ def flush_all(self):
65+ self._cache.flush_all()
66+
67+
Back to Top