Skip to content

Commit

Permalink
Fix: skip the cache if it somehow got corrupted (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueBrain authored Nov 12, 2020
1 parent fe9b485 commit 4d11597
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions truewiki/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ def page_changed(self):

return CATEGORIES, FILES, LANGUAGES, LINKS, PAGES, PAGES_LC, TEMPLATES, TRANSLATIONS

def _load_metadata_from_cache(self):
with open(CACHE_FILENAME, "r") as fp:
try:
payload = json.loads(fp.read())
except json.JSONDecodeError:
log.info("Cache was corrupted; reloading metadata ...")
return

if payload.get("version", 1) != CACHE_VERSION:
return

CATEGORIES.update(payload["categories"])
FILES.update(payload["files"])
LINKS.update(payload["links"])
PAGES.update(payload["pages"])
TEMPLATES.update(payload["templates"])
TRANSLATIONS.update(payload["translations"])

def load_metadata(self):
start = time.time()
log.info("Loading metadata (this can take a while the first run) ...")
Expand All @@ -298,15 +316,7 @@ def load_metadata(self):
TRANSLATIONS.clear()

if os.path.exists(CACHE_FILENAME):
with open(CACHE_FILENAME, "r") as fp:
payload = json.loads(fp.read())
if payload.get("version", 1) == CACHE_VERSION:
CATEGORIES.update(payload["categories"])
FILES.update(payload["files"])
LINKS.update(payload["links"])
PAGES.update(payload["pages"])
TEMPLATES.update(payload["templates"])
TRANSLATIONS.update(payload["translations"])
self._load_metadata_from_cache()

# Ensure no file is scanned more than once.
notified = set()
Expand Down

0 comments on commit 4d11597

Please sign in to comment.