[Checkins] SVN: keas.kmi/trunk/ Added caching to directory-based facility, so we do not need to read

Stephan Richter srichter at gmail.com
Wed Sep 29 01:03:08 EDT 2010


Log message for revision 117040:
  Added caching to directory-based facility, so we do not need to read 
  files all the time.
  

Changed:
  U   keas.kmi/trunk/CHANGES.txt
  U   keas.kmi/trunk/src/keas/kmi/facility.py
  U   keas.kmi/trunk/src/keas/kmi/testing.py

-=-
Modified: keas.kmi/trunk/CHANGES.txt
===================================================================
--- keas.kmi/trunk/CHANGES.txt	2010-09-29 04:48:52 UTC (rev 117039)
+++ keas.kmi/trunk/CHANGES.txt	2010-09-29 05:03:07 UTC (rev 117040)
@@ -11,6 +11,8 @@
   instead of the ZODB. This increases transparency in the data store and makes
   backups easier.
 
+- Added caching to directory-based facility, so we do not need to read files
+  all the time.
 
 1.1.1 (2010-08-27)
 ------------------

Modified: keas.kmi/trunk/src/keas/kmi/facility.py
===================================================================
--- keas.kmi/trunk/src/keas/kmi/facility.py	2010-09-29 04:48:52 UTC (rev 117039)
+++ keas.kmi/trunk/src/keas/kmi/facility.py	2010-09-29 05:03:07 UTC (rev 117040)
@@ -82,6 +82,7 @@
 
     def __init__(self, storage_dir):
         self.storage_dir = storage_dir
+        self.__cache = {}
 
     def keys(self):
         return [filename[:-4] for filename in os.listdir(self.storage_dir)
@@ -91,11 +92,15 @@
         return iter(self.keys())
 
     def __getitem__(self, name):
+        if name in self.__cache:
+            return self.__cache[name]
         if name+'.dek' not in os.listdir(self.storage_dir):
             raise KeyError(name)
         fn = os.path.join(self.storage_dir, name+'.dek')
         with open(fn, 'rb') as file:
-            return file.read()
+            data = file.read()
+            self.__cache[name] = data
+            return data
 
     def get(self, name, default=None):
         try:
@@ -120,10 +125,12 @@
     def __setitem__(self, name, key):
         fn = os.path.join(self.storage_dir, name+'.dek')
         with open(fn, 'w') as file:
-            return file.write(key)
+            file.write(key)
         logger.info('New key added (hash): %s', name)
 
     def __delitem__(self, name):
+        if name in self.__cache:
+            del self.__cache[name]
         fn = os.path.join(self.storage_dir, name+'.dek')
         os.remove(fn)
         logger.info('Key removed (hash): %s', name)
@@ -184,7 +191,9 @@
         conn = self.httpConnFactory(pieces.netloc)
         conn.request('POST', '/new', '', {})
         response = conn.getresponse()
-        return response.read()
+        data = response.read()
+        response.close()
+        return data
 
     def getEncryptionKey(self, key):
         """Given the key encrypting key, get the encryption key."""
@@ -194,7 +203,9 @@
         pieces = urlparse.urlparse(self.url)
         conn = self.httpConnFactory(pieces.netloc)
         conn.request('POST', '/key', key, {'content-type': 'text/plain'})
-        encryptionKey = conn.getresponse().read()
+        response = conn.getresponse()
+        encryptionKey = response.read()
+        response.close()
         self._cache[key] = (time.time(), encryptionKey)
         return encryptionKey
 

Modified: keas.kmi/trunk/src/keas/kmi/testing.py
===================================================================
--- keas.kmi/trunk/src/keas/kmi/testing.py	2010-09-29 04:48:52 UTC (rev 117039)
+++ keas.kmi/trunk/src/keas/kmi/testing.py	2010-09-29 05:03:07 UTC (rev 117040)
@@ -62,10 +62,6 @@
             self.fp = None
         return data
 
-    def getheader(self, name):
-        if name.lower() == 'content-length':
-            return str(len(self.fp.getvalue()))
-
     def close(self):
         pass
 
@@ -85,21 +81,13 @@
         if url == '/new':
             view = rest.create_key
         elif url == '/key':
-            if self.request_data[3].get('content-type') != 'text/plain':
-                # ensure we don't trip on
-                # http://trac.pythonpaste.org/pythonpaste/ticket/294
-                raise ValueError('bad content type')
             view = rest.get_key
-        else:
-            raise ValueError(url)
 
         io = StringIO.StringIO(self.request_data[2])
         req = webob.Request({'wsgi.input': io})
         res = view(self.context, req)
         return FakeHTTPResponse(res.body)
 
-    def close(self):
-        pass
 
 def setupRestApi(localFacility, masterFacility):
     localFacility.httpConnFactory = type(



More information about the checkins mailing list