Skip to content

Commit

Permalink
Improve filetree server side caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jmforsythe committed Aug 21, 2023
1 parent a05f644 commit 6dc0248
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ def filetree_json(name):
this_repo_dir = repos_dir / pathlib.Path(name)
db_path = (this_repo_dir / this_repo_dir.stem).with_suffix(".db")
json_path = this_repo_dir / "filetree.json"
if not json_path.is_file():
with open(json_path, "wb") as f:
json = databaseToJSON.get_json_from_db(db_path)
f.write(json.encode(errors="replace"))
return json
else:
with open(json_path, "rb") as f:
return f.read().decode(errors="replace")
if json_path.is_file():
database_change_time = db_path.stat().st_mtime
json_change_time = json_path.stat().st_mtime
if not database_change_time > json_change_time:
# Use cached result only if it exists and is newer than the database
with open(json_path, "rb") as f:
return f.read().decode(errors="replace")

with open(json_path, "wb") as f:
json = databaseToJSON.get_json_from_db(db_path)
f.write(json.encode(errors="replace"))
return json


@app.route("/<path:name>/highlight.json")
@cache_on_db_change
Expand Down

0 comments on commit 6dc0248

Please sign in to comment.