Add caching to blog and now blueprints for improved performance
Co-authored-by: Nathanwoodburn <62039630+Nathanwoodburn@users.noreply.github.com>
This commit is contained in:
@@ -3,11 +3,13 @@ from flask import Blueprint, render_template, request, jsonify
|
|||||||
import markdown
|
import markdown
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import re
|
import re
|
||||||
|
from functools import lru_cache
|
||||||
from tools import isCLI, getClientIP, getHandshakeScript
|
from tools import isCLI, getClientIP, getHandshakeScript
|
||||||
|
|
||||||
app = Blueprint('blog', __name__, url_prefix='/blog')
|
app = Blueprint('blog', __name__, url_prefix='/blog')
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=32)
|
||||||
def list_page_files():
|
def list_page_files():
|
||||||
blog_pages = os.listdir("data/blog")
|
blog_pages = os.listdir("data/blog")
|
||||||
# Sort pages by modified time, newest first
|
# Sort pages by modified time, newest first
|
||||||
@@ -21,28 +23,43 @@ def list_page_files():
|
|||||||
return blog_pages
|
return blog_pages
|
||||||
|
|
||||||
|
|
||||||
def render_page(date, handshake_scripts=None):
|
@lru_cache(maxsize=64)
|
||||||
# Convert md to html
|
def get_blog_content(date):
|
||||||
|
"""Get and cache blog content."""
|
||||||
if not os.path.exists(f"data/blog/{date}.md"):
|
if not os.path.exists(f"data/blog/{date}.md"):
|
||||||
return render_template("404.html"), 404
|
return None
|
||||||
|
|
||||||
with open(f"data/blog/{date}.md", "r") as f:
|
with open(f"data/blog/{date}.md", "r") as f:
|
||||||
content = f.read()
|
return f.read()
|
||||||
# Get the title from the file name
|
|
||||||
title = date.removesuffix(".md").replace("_", " ")
|
|
||||||
# Convert the md to html
|
@lru_cache(maxsize=64)
|
||||||
content = markdown.markdown(
|
def render_markdown_to_html(content):
|
||||||
|
"""Convert markdown to HTML with caching."""
|
||||||
|
html = markdown.markdown(
|
||||||
content, extensions=['sane_lists', 'codehilite', 'fenced_code'])
|
content, extensions=['sane_lists', 'codehilite', 'fenced_code'])
|
||||||
# Add target="_blank" to all links
|
# Add target="_blank" to all links
|
||||||
content = content.replace('<a href="', '<a target="_blank" href="')
|
html = html.replace('<a href="', '<a target="_blank" href="')
|
||||||
|
html = html.replace("<h4", "<h4 style='margin-bottom:0px;'")
|
||||||
|
html = fix_numbered_lists(html)
|
||||||
|
return html
|
||||||
|
|
||||||
content = content.replace("<h4", "<h4 style='margin-bottom:0px;'")
|
|
||||||
content = fix_numbered_lists(content)
|
def render_page(date, handshake_scripts=None):
|
||||||
|
# Get cached content
|
||||||
|
content = get_blog_content(date)
|
||||||
|
if content is None:
|
||||||
|
return render_template("404.html"), 404
|
||||||
|
|
||||||
|
# Get the title from the file name
|
||||||
|
title = date.removesuffix(".md").replace("_", " ")
|
||||||
|
# Convert the md to html (cached)
|
||||||
|
html_content = render_markdown_to_html(content)
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
"blog/template.html",
|
"blog/template.html",
|
||||||
title=title,
|
title=title,
|
||||||
content=content,
|
content=html_content,
|
||||||
handshake_scripts=handshake_scripts,
|
handshake_scripts=handshake_scripts,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -134,12 +151,11 @@ def path(path):
|
|||||||
if not isCLI(request):
|
if not isCLI(request):
|
||||||
return render_page(path, handshake_scripts=getHandshakeScript(request.host))
|
return render_page(path, handshake_scripts=getHandshakeScript(request.host))
|
||||||
|
|
||||||
# Convert md to html
|
# Get cached content
|
||||||
if not os.path.exists(f"data/blog/{path}.md"):
|
content = get_blog_content(path)
|
||||||
|
if content is None:
|
||||||
return render_template("404.html"), 404
|
return render_template("404.html"), 404
|
||||||
|
|
||||||
with open(f"data/blog/{path}.md", "r") as f:
|
|
||||||
content = f.read()
|
|
||||||
# Get the title from the file name
|
# Get the title from the file name
|
||||||
title = path.replace("_", " ")
|
title = path.replace("_", " ")
|
||||||
return jsonify({
|
return jsonify({
|
||||||
@@ -154,11 +170,9 @@ def path(path):
|
|||||||
|
|
||||||
@app.route("/<path:path>.md")
|
@app.route("/<path:path>.md")
|
||||||
def path_md(path):
|
def path_md(path):
|
||||||
if not os.path.exists(f"data/blog/{path}.md"):
|
content = get_blog_content(path)
|
||||||
|
if content is None:
|
||||||
return render_template("404.html"), 404
|
return render_template("404.html"), 404
|
||||||
|
|
||||||
with open(f"data/blog/{path}.md", "r") as f:
|
|
||||||
content = f.read()
|
|
||||||
|
|
||||||
# Return the raw markdown file
|
# Return the raw markdown file
|
||||||
return content, 200, {'Content-Type': 'text/plain; charset=utf-8'}
|
return content, 200, {'Content-Type': 'text/plain; charset=utf-8'}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from flask import Blueprint, render_template, make_response, request, jsonify
|
from flask import Blueprint, render_template, make_response, request, jsonify
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
from functools import lru_cache
|
||||||
from tools import getHandshakeScript, error_response, isCLI
|
from tools import getHandshakeScript, error_response, isCLI
|
||||||
from curl import get_header, MAX_WIDTH
|
from curl import get_header, MAX_WIDTH
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
@@ -10,6 +11,7 @@ import re
|
|||||||
app = Blueprint('now', __name__, url_prefix='/now')
|
app = Blueprint('now', __name__, url_prefix='/now')
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=16)
|
||||||
def list_page_files():
|
def list_page_files():
|
||||||
now_pages = os.listdir("templates/now")
|
now_pages = os.listdir("templates/now")
|
||||||
now_pages = [
|
now_pages = [
|
||||||
@@ -19,12 +21,14 @@ def list_page_files():
|
|||||||
return now_pages
|
return now_pages
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=16)
|
||||||
def list_dates():
|
def list_dates():
|
||||||
now_pages = list_page_files()
|
now_pages = list_page_files()
|
||||||
now_dates = [page.split(".")[0] for page in now_pages]
|
now_dates = [page.split(".")[0] for page in now_pages]
|
||||||
return now_dates
|
return now_dates
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=8)
|
||||||
def get_latest_date(formatted=False):
|
def get_latest_date(formatted=False):
|
||||||
if formatted:
|
if formatted:
|
||||||
date = list_dates()[0]
|
date = list_dates()[0]
|
||||||
|
|||||||
Reference in New Issue
Block a user