Skip to content

Commit

Permalink
Added caching to directory-based facility, so we do not need to read
Browse files Browse the repository at this point in the history
files all the time.
  • Loading branch information
strichter committed Sep 29, 2010
1 parent fc5f2d2 commit 9745e8d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CHANGES
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)
------------------
Expand Down
19 changes: 15 additions & 4 deletions src/keas/kmi/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class KeyManagementFacility(EncryptionService):

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)
Expand All @@ -91,11 +92,15 @@ def __iter__(self):
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:
Expand All @@ -120,10 +125,12 @@ def __contains__(self, name):
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)
Expand Down Expand Up @@ -184,7 +191,9 @@ def generate(self):
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."""
Expand All @@ -194,7 +203,9 @@ def getEncryptionKey(self, key):
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

Expand Down
12 changes: 0 additions & 12 deletions src/keas/kmi/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ def read(self, amt=10*2**10):
self.fp = None
return data

def getheader(self, name):
if name.lower() == 'content-length':
return str(len(self.fp.getvalue()))

def close(self):
pass

Expand All @@ -85,21 +81,13 @@ def getresponse(self, buffering=False):
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(
Expand Down

0 comments on commit 9745e8d

Please sign in to comment.