adding a caching api. requires memcache: ftp://ftp.tummy.com/pub/python-memcached/
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Tue, 27 Nov 2007 20:07:36 +0000 (20:07 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Tue, 27 Nov 2007 20:07:36 +0000 (20:07 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1148 9efc2488-bf62-4759-914b-345cdb29e865

src/python/osrf/cache.py [new file with mode: 0644]

diff --git a/src/python/osrf/cache.py b/src/python/osrf/cache.py
new file mode 100644 (file)
index 0000000..2a8d30a
--- /dev/null
@@ -0,0 +1,42 @@
+import memcache
+from osrf.json import osrfObjectToJSON, osrfJSONToObject
+
+'''
+Abstracted OpenSRF caching interface.
+Requires memcache: ftp://ftp.tummy.com/pub/python-memcached/
+'''
+
+_client = None
+
+class CacheException(Exception):
+    def __init__(self, info):
+        self.info = info
+    def __str__(self):
+        return "%s: %s" % (self.__class__.__name__, self.info)
+
+class CacheClient(object):
+    def __init__(self, servers=None):
+        ''' If no servers are provided, this instance will use 
+            the global memcache connection.
+            servers takes the form ['server:port', 'server2:port2', ...]
+            '''
+        global _client
+        if servers:
+            self.client = memcache.Client(server, debug=0)
+        else:
+            if not _client:
+                raise CacheException("not connected to any memcache servers.  try CacheClient.connect(servers)")
+            self.client = _client
+
+    def put(self, key, val, timeout=0):
+        self.client.set(key, osrfObjectToJSON(val), timeout)
+
+    def get(self, key):
+        return osrfJSONToObject(self.client.get(key) or "null")
+
+    @staticmethod
+    def connect(svrs):
+        global _client
+        _client = memcache.Client(svrs, debug=0)
+
+