perf: cache dynamic web pages

This commit is contained in:
Ankush Menat 2023-06-04 12:47:09 +05:30 committed by Ankush Menat
parent 7c7c11b454
commit c94c0591c3
3 changed files with 17 additions and 9 deletions

View file

@ -8,6 +8,7 @@ from jinja2.exceptions import TemplateSyntaxError
import frappe
from frappe import _
from frappe.utils import get_datetime, now, quoted, strip_html
from frappe.utils.caching import redis_cache
from frappe.utils.jinja import render_template
from frappe.utils.safe_exec import safe_exec
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
@ -30,11 +31,9 @@ class WebPage(WebsiteGenerator):
if not self.dynamic_route:
self.route = quoted(self.route)
def on_update(self):
super().on_update()
def on_trash(self):
super().on_trash()
def clear_cache(self):
super().clear_cache()
get_dynamic_web_pages.clear_cache()
def get_context(self, context):
context.main_section = get_html_content_based_on_type(self, "main_section", self.content_type)
@ -247,3 +246,10 @@ def extract_script_and_style_tags(html):
style.extract()
return str(soup), scripts, styles
@redis_cache(ttl=60 * 60)
def get_dynamic_web_pages() -> dict[str, str]:
return frappe.get_all(
"Web Page", fields=["name", "route", "modified"], filters=dict(published=1, dynamic_route=1)
)

View file

@ -103,4 +103,7 @@ def _find_matching_document_webview(route: str) -> tuple[str, str] | None:
def clear_routing_cache():
from frappe.website.doctype.web_page.web_page import get_dynamic_web_pages
_find_matching_document_webview.clear_cache()
get_dynamic_web_pages.clear_cache()

View file

@ -16,12 +16,11 @@ def get_page_info_from_web_page_with_dynamic_routes(path):
"""
Query Web Page with dynamic_route = 1 and evaluate if any of the routes match
"""
from frappe.website.doctype.web_page.web_page import get_dynamic_web_pages
rules, page_info = [], {}
# build rules from all web page with `dynamic_route = 1`
for d in frappe.get_all(
"Web Page", fields=["name", "route", "modified"], filters=dict(published=1, dynamic_route=1)
):
for d in get_dynamic_web_pages():
rules.append(Rule("/" + d.route, endpoint=d.name))
d.doctype = "Web Page"
page_info[d.name] = d