2 from osrf.json import to_json, to_object
6 Abstracted OpenSRF caching interface.
7 Requires memcache: ftp://ftp.tummy.com/pub/python-memcached/
13 class CacheException(Exception):
14 def __init__(self, info):
17 return "%s: %s" % (self.__class__.__name__, self.info)
19 class CacheClient(object):
20 def __init__(self, servers=None):
21 ''' If no servers are provided, this instance will use
22 the global memcache connection.
23 servers takes the form ['server:port', 'server2:port2', ...]
27 self.client = memcache.Client(servers, debug=1)
31 "not connected to any memcache servers."
32 "try CacheClient.connect(servers)"
36 def put(self, key, val, timeout=None):
39 timeout = defaultTimeout
40 timeout = int(timeout)
42 osrf.log.log_internal("cache: %s => %s" % (str(key), json))
43 return self.client.set(str(key), json, timeout)
46 obj = self.client.get(str(key))
47 osrf.log.log_internal("cache: fetching %s => %s" % (str(key), obj))
48 return to_object(obj or "null")
50 def delete(self, key):
51 osrf.log.log_internal("cache: deleting %s" % str(key))
52 self.client.delete(str(key))
57 osrf.log.log_debug("cache: connecting to servers %s" % str(svrs))
58 _client = memcache.Client(svrs, debug=1)