[Opensrf-commits] r1148 - trunk/src/python/osrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Tue Nov 27 15:26:09 EST 2007
Author: erickson
Date: 2007-11-27 15:07:36 -0500 (Tue, 27 Nov 2007)
New Revision: 1148
Added:
trunk/src/python/osrf/cache.py
Log:
adding a caching api. requires memcache: ftp://ftp.tummy.com/pub/python-memcached/
Added: trunk/src/python/osrf/cache.py
===================================================================
--- trunk/src/python/osrf/cache.py (rev 0)
+++ trunk/src/python/osrf/cache.py 2007-11-27 20:07:36 UTC (rev 1148)
@@ -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)
+
+
More information about the opensrf-commits
mailing list