From 74afdc1b5ba363b1e7249f57edf1c2a641057ae8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 21 Nov 2025 11:31:49 +0000 Subject: [PATCH] Add caching to blog and now blueprints for improved performance Co-authored-by: Nathanwoodburn <62039630+Nathanwoodburn@users.noreply.github.com> --- blueprints/blog.py | 56 +++++++++++++++++++++++++++++----------------- blueprints/now.py | 4 ++++ 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/blueprints/blog.py b/blueprints/blog.py index 73105ea..1f79ab3 100644 --- a/blueprints/blog.py +++ b/blueprints/blog.py @@ -3,11 +3,13 @@ from flask import Blueprint, render_template, request, jsonify import markdown from bs4 import BeautifulSoup import re +from functools import lru_cache from tools import isCLI, getClientIP, getHandshakeScript app = Blueprint('blog', __name__, url_prefix='/blog') +@lru_cache(maxsize=32) def list_page_files(): blog_pages = os.listdir("data/blog") # Sort pages by modified time, newest first @@ -21,28 +23,43 @@ def list_page_files(): return blog_pages -def render_page(date, handshake_scripts=None): - # Convert md to html +@lru_cache(maxsize=64) +def get_blog_content(date): + """Get and cache blog content.""" 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: - content = f.read() - # Get the title from the file name - title = date.removesuffix(".md").replace("_", " ") - # Convert the md to html - content = markdown.markdown( + return f.read() + + +@lru_cache(maxsize=64) +def render_markdown_to_html(content): + """Convert markdown to HTML with caching.""" + html = markdown.markdown( content, extensions=['sane_lists', 'codehilite', 'fenced_code']) # Add target="_blank" to all links - content = content.replace('.md") 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 - with open(f"data/blog/{path}.md", "r") as f: - content = f.read() - # Return the raw markdown file return content, 200, {'Content-Type': 'text/plain; charset=utf-8'} diff --git a/blueprints/now.py b/blueprints/now.py index d6e4208..e7ddf17 100644 --- a/blueprints/now.py +++ b/blueprints/now.py @@ -1,6 +1,7 @@ from flask import Blueprint, render_template, make_response, request, jsonify import datetime import os +from functools import lru_cache from tools import getHandshakeScript, error_response, isCLI from curl import get_header, MAX_WIDTH from bs4 import BeautifulSoup @@ -10,6 +11,7 @@ import re app = Blueprint('now', __name__, url_prefix='/now') +@lru_cache(maxsize=16) def list_page_files(): now_pages = os.listdir("templates/now") now_pages = [ @@ -19,12 +21,14 @@ def list_page_files(): return now_pages +@lru_cache(maxsize=16) def list_dates(): now_pages = list_page_files() now_dates = [page.split(".")[0] for page in now_pages] return now_dates +@lru_cache(maxsize=8) def get_latest_date(formatted=False): if formatted: date = list_dates()[0]