]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/memcached.py
Deploy rate-limiting
[ipfire.org.git] / src / backend / memcached.py
1 #!/usr/bin/python
2
3 import logging
4 import memcache
5
6 from .misc import Object
7
8 class Memcached(Object):
9 def init(self):
10 self._connection = memcache.Client(["localhost"], debug=1)
11
12 def get(self, key, *args, **kwargs):
13 logging.debug("Retrieving %s from cache..." % key)
14
15 ret = self._connection.get(key, *args, **kwargs)
16
17 if ret is None:
18 logging.debug("Found nothing for %s" % key)
19 else:
20 logging.debug("Found object of %s bytes for %s" % (len(ret), key))
21
22 return ret
23
24 def get_multi(self, keys, *args, **kwargs):
25 logging.debug("Retrieving keys from cache: %s" % keys)
26
27 ret = self._connection.get_multi(keys, *args, **kwargs)
28
29 if ret is None:
30 logging.debug("Found nothing for %s" % keys)
31 else:
32 logging.debug("Found object of %s bytes for %s" % (len(ret), keys))
33
34 return ret
35
36 def add(self, key, data, *args, **kwargs):
37 if data is None:
38 logging.debug("Putting nothing into cache for %s" % key)
39 else:
40 logging.debug("Putting %s bytes into cache for %s" % (len(data), key))
41
42 return self._connection.add(key, data, *args, **kwargs)
43
44 def set(self, key, data, *args, **kwargs):
45 if data is None:
46 logging.debug("Putting nothing into cache for %s" % key)
47 else:
48 logging.debug("Putting %s bytes into cache for %s" % (len(data), key))
49
50 return self._connection.set(key, data, *args, **kwargs)
51
52 def delete(self, key, *args, **kwargs):
53 return self._connection.delete(key, *args, **kwargs)
54
55 def incr(self, key):
56 logging.debug("Incrementing key %s" % key)
57
58 return self._connection.incr(key)