Removes some code duplication.

main
Olivier Mangin 9 years ago
parent d9f24052fc
commit 57a32e5601

@ -47,22 +47,22 @@ class DataCache(object):
def _create(self): def _create(self):
self._databroker = databroker.DataBroker(self.directory, create=True) self._databroker = databroker.DataBroker(self.directory, create=True)
def _try_pull_cache(self, name):
try:
return self.databroker.pull_cache(name)
except Exception as e: # take no prisonners; if something is wrong, no cache.
return {}
@property @property
def metacache(self): def metacache(self):
if self._metacache is None: if self._metacache is None:
try: self._metacache = self._try_pull_cache('metacache')
self._metacache = self.databroker.pull_cache('metacache')
except Exception as e: # take no prisonners; if something is wrong, no cache.
self._metacache = {}
return self._metacache return self._metacache
@property @property
def bibcache(self): def bibcache(self):
if self._bibcache is None: if self._bibcache is None:
try: self._bibcache = self._try_pull_cache('bibcache')
self._bibcache = self.databroker.pull_cache('bibcache')
except Exception as e:
self._bibcache = {}
return self._bibcache return self._bibcache
def flush_cache(self, force=False): def flush_cache(self, force=False):
@ -74,12 +74,15 @@ class DataCache(object):
self.databroker.push_cache('bibcache', self.bibcache) self.databroker.push_cache('bibcache', self.bibcache)
self._bibcache_modified = False self._bibcache_modified = False
def _is_uptodate(self, cached_data, file_mtime):
boundary = file_mtime if self.nsec_support else file_mtime + 1
return cached_data.timestamp <= boundary
def pull_metadata(self, citekey): def pull_metadata(self, citekey):
mtime = self.databroker.filebroker.mtime_metafile(citekey) mtime = self.databroker.filebroker.mtime_metafile(citekey)
if citekey in self.metacache: if citekey in self.metacache:
cached_metadata = self.metacache[citekey] cached_metadata = self.metacache[citekey]
boundary = mtime if self.nsec_support else mtime + 1 if self._is_uptodate(cached_metadata, mtime):
if cached_metadata.timestamp >= boundary:
return cached_metadata.data return cached_metadata.data
# if we get here, we must update the cache. # if we get here, we must update the cache.
@ -93,8 +96,7 @@ class DataCache(object):
mtime = self.databroker.filebroker.mtime_bibfile(citekey) mtime = self.databroker.filebroker.mtime_bibfile(citekey)
if citekey in self.bibcache: if citekey in self.bibcache:
cached_bibdata = self.bibcache[citekey] cached_bibdata = self.bibcache[citekey]
boundary = mtime if self.nsec_support else mtime + 1 if self._is_uptodate(cached_bibdata, mtime):
if cached_bibdata.timestamp >= boundary:
return cached_bibdata.data return cached_bibdata.data
# if we get here, we must update the cache. # if we get here, we must update the cache.

Loading…
Cancel
Save