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(server, 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
41 osrf.log.log_internal("cache: %s => %s" % (str(key), json))
42 return self.client.set(str(key), json, timeout)
45 obj = self.client.get(str(key))
46 osrf.log.log_internal("cache: fetching %s => %s" % (str(key), obj))
47 return to_object(obj or "null")
49 def delete(self, key):
50 osrf.log.log_internal("cache: deleting %s" % str(key))
51 self.client.delete(str(key))
56 osrf.log.logDebug("cache: connecting to servers %s" % str(svrs))
57 _client = memcache.Client(svrs, debug=1)