Add version to Cache to avoid unneeded writes
as calls to Pickle.dump are rather expensive
This commit is contained in:
parent
4c2a51bc5a
commit
662a359c4f
|
@ -18,6 +18,7 @@ class Cache(MutableMapping):
|
||||||
def __init__(self, path, data={}):
|
def __init__(self, path, data={}):
|
||||||
self.path = path
|
self.path = path
|
||||||
self.store = data
|
self.store = data
|
||||||
|
self.initial_store_version = self.store.setdefault('version', 0)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls, path):
|
def read(cls, path):
|
||||||
|
@ -34,6 +35,9 @@ class Cache(MutableMapping):
|
||||||
def write(self, path=None):
|
def write(self, path=None):
|
||||||
if not path:
|
if not path:
|
||||||
path = self.path
|
path = self.path
|
||||||
|
if self.initial_store_version == self.store['version']:
|
||||||
|
logger.debug('Cached content did not change, skipping write')
|
||||||
|
return
|
||||||
logger.debug('Writing cache to %s', path)
|
logger.debug('Writing cache to %s', path)
|
||||||
with open(path, 'wb') as f:
|
with open(path, 'wb') as f:
|
||||||
pickle.dump(self.store, f)
|
pickle.dump(self.store, f)
|
||||||
|
@ -45,6 +49,7 @@ class Cache(MutableMapping):
|
||||||
|
|
||||||
def __setitem__(self, path, events):
|
def __setitem__(self, path, events):
|
||||||
self.store[path] = CachedLog(path, _calculate_hash(path), list(events))
|
self.store[path] = CachedLog(path, _calculate_hash(path), list(events))
|
||||||
|
self.store['version'] += 1
|
||||||
|
|
||||||
def __getitem__(self, path):
|
def __getitem__(self, path):
|
||||||
return self.store[path]
|
return self.store[path]
|
||||||
|
|
Loading…
Reference in New Issue