From 7788dede8976673a955c6e548f697ad180ef225b Mon Sep 17 00:00:00 2001 From: erickson Date: Tue, 27 Nov 2007 20:07:36 +0000 Subject: [PATCH] adding a caching api. requires memcache: ftp://ftp.tummy.com/pub/python-memcached/ git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1148 9efc2488-bf62-4759-914b-345cdb29e865 --- src/python/osrf/cache.py | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/python/osrf/cache.py diff --git a/src/python/osrf/cache.py b/src/python/osrf/cache.py new file mode 100644 index 0000000..2a8d30a --- /dev/null +++ b/src/python/osrf/cache.py @@ -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) + + -- 2.43.2