diff options
author | Drew DeVault <sir@cmpwn.com> | 2019-06-11 12:36:06 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-06-11 12:38:15 -0400 |
commit | 0e1feffc370e2738ab9f211d803c60a0ac8d4050 (patch) | |
tree | 938563d2d61b433057f97bfdea227c6cae81a571 | |
parent | 3942d7b1d9374f8f6da9f62e9ff0bd282cf8893e (diff) |
Cache rendered pages
-rw-r--r-- | mansrht/blueprints/html.py | 71 | ||||
-rw-r--r-- | mansrht/redis.py | 6 |
2 files changed, 48 insertions, 29 deletions
diff --git a/mansrht/blueprints/html.py b/mansrht/blueprints/html.py index 2b93c4a..cb0ebbc 100644 --- a/mansrht/blueprints/html.py +++ b/mansrht/blueprints/html.py @@ -6,11 +6,13 @@ from srht.flask import loginrequired from srht.markdown import markdown, extract_toc from srht.validation import Validation from mansrht.access import UserAccess, check_access +from mansrht.redis import redis from mansrht.types import User, Wiki from mansrht.wikis import create_wiki, delete_wiki -from datetime import datetime +from datetime import datetime, timedelta from jinja2 import Markup from urllib.parse import urlparse, urlunparse +import json import pygit2 import os import yaml @@ -56,35 +58,46 @@ def content(repo, path, wiki=None, is_root=False, **kwargs): return redirect(urlunparse(url)) else: abort(404) - blob = repo.get(tree.id) - if blob.is_binary: - abort(404) - md = blob.data.decode() - frontmatter = dict() - if md.startswith("---\n"): - try: - end = md.index("---\n\n", 1) - except ValueError: - end = -1 # this is dumb, Guido - if end != -1: - frontmatter = md[4:end] - md = md[end+5:] - if frontmatter: - try: - frontmatter = yaml.safe_load(frontmatter) - if not isinstance(frontmatter, dict): - raise Exception() - except: - md = "<!-- Error parsing YAML frontmatter -->\n\n" + md - frontmatter = dict() - if tree.name.endswith(".md"): - html = markdown(md, ["h1", "h2", "h3", "h4", "h5"], baselevel=3) - else: - html = Markup(md) - if current_user: - html = html.replace("{{{srht_username}}}", current_user.username) + html_cachekey = f"man.sr.ht:content:{str(tree.id)}" + frontmatter_cachekey = f"man.sr.ht:frontmatter:{str(tree.id)}" + html = redis.get(html_cachekey) + if not html: + blob = repo.get(tree.id) + if blob.is_binary: + abort(404) + md = blob.data.decode() + frontmatter = dict() + if md.startswith("---\n"): + try: + end = md.index("---\n\n", 1) + except ValueError: + end = -1 # this is dumb, Guido + if end != -1: + frontmatter = md[4:end] + md = md[end+5:] + if frontmatter: + try: + frontmatter = yaml.safe_load(frontmatter) + if not isinstance(frontmatter, dict): + raise Exception() + except: + md = "<!-- Error parsing YAML frontmatter -->\n\n" + md + frontmatter = dict() + if tree.name.endswith(".md"): + html = markdown(md, ["h1", "h2", "h3", "h4", "h5"], baselevel=3) + else: + html = Markup(md) + if current_user: + html = html.replace("{{{srht_username}}}", current_user.username) + else: + html = html.replace("{{{srht_username}}}", "USERNAME") + redis.setex(html_cachekey, timedelta(days=7), html) + redis.setex(frontmatter_cachekey, + timedelta(days=7), json.dumps(frontmatter)) else: - html = html.replace("{{{srht_username}}}", "USERNAME") + html = Markup(html.decode()) + frontmatter = redis.get(frontmatter_cachekey) + frontmatter = json.loads(frontmatter.decode()) title = path[-1].rstrip(".md") if path else "index" ctime = datetime.fromtimestamp(commit.commit_time) toc = extract_toc(html) diff --git a/mansrht/redis.py b/mansrht/redis.py new file mode 100644 index 0000000..a815f90 --- /dev/null +++ b/mansrht/redis.py @@ -0,0 +1,6 @@ +try: + from redis import StrictRedis as Redis +except ImportError: + from redis import Redis + +redis = Redis() |