2 from osrf.json import osrfObjectToJSON, osrfJSONToObject
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)
30 raise CacheException("not connected to any memcache servers. try CacheClient.connect(servers)")
33 def put(self, key, val, timeout=None):
36 timeout = defaultTimeout
37 s = osrfObjectToJSON(val)
38 osrfLogInternal("cache: %s => %s" % (str(key), s))
39 return self.client.set(str(key), s, timeout)
42 o = self.client.get(str(key))
43 osrfLogInternal("cache: fetching %s => %s" % (str(key), o))
44 return osrfJSONToObject(o or "null")
46 def delete(self, key):
47 osrfLogInternal("cache: deleting %s" % str(key))
48 self.client.delete(str(key))
53 osrfLogDebug("cache: connecting to servers %s" % str(svrs))
54 _client = memcache.Client(svrs, debug=1)