added caching in cotton component

This commit is contained in:
Will Abbott 2024-07-09 22:49:21 +01:00
parent 326abc9bcd
commit c4b541be3a
4 changed files with 55 additions and 47 deletions

View file

@ -54,7 +54,7 @@ def benchmark_template_rendering(template_name, iterations=10000):
return end_time - start_time, render_to_string(template_name) return end_time - start_time, render_to_string(template_name)
def benchmark_template_rendering_alt(template_name, iterations=1000): def benchmark_template_rendering_alt(template_name, iterations=500):
data = list(range(1, iterations)) data = list(range(1, iterations))
start_time = time.time() start_time = time.time()
render_to_string(template_name, context={"data": data}) render_to_string(template_name, context={"data": data})
@ -68,32 +68,32 @@ simple_cotton = benchmark_template_rendering_alt("simple_cotton.html")
print(f"Native Django Template: {simple_native} seconds") print(f"Native Django Template: {simple_native} seconds")
print(f"Cotton Template: {simple_cotton} seconds") print(f"Cotton Template: {simple_cotton} seconds")
#
# # Benchmarking each template # Benchmarking each template
# time_native_include, output_native_include = benchmark_template_rendering( time_native_include, output_native_include = benchmark_template_rendering(
# "benchmarks/native_include.html" "benchmarks/native_include.html"
# ) )
# time_cotton_include, output_cotton_include = benchmark_template_rendering( time_cotton_include, output_cotton_include = benchmark_template_rendering(
# "cotton/benchmarks/cotton_include.html" "cotton/benchmarks/cotton_include.html"
# ) )
#
#
# time_native_extends, output_native_extends = benchmark_template_rendering( time_native_extends, output_native_extends = benchmark_template_rendering(
# "benchmarks/native_extends.html" "benchmarks/native_extends.html"
# ) )
# time_compiled_cotton, output_compiled_cotton = benchmark_template_rendering( time_compiled_cotton, output_compiled_cotton = benchmark_template_rendering(
# "cotton/benchmarks/cotton_compiled.html" "cotton/benchmarks/cotton_compiled.html"
# ) )
# time_cotton, output_cotton = benchmark_template_rendering( time_cotton, output_cotton = benchmark_template_rendering(
# "cotton/benchmarks/cotton.html" "cotton/benchmarks/cotton.html"
# ) )
#
# # Output results # Output results
# print("Include, native vs cotton:") print("Include, native vs cotton:")
# print(f"Native {{% include %}}: {time_native_include} seconds") print(f"Native {{% include %}}: {time_native_include} seconds")
# print(f"Cotton for include:: {time_cotton_include} seconds") print(f"Cotton for include:: {time_cotton_include} seconds")
# print("-------") print("-------")
# print("Block + Extends, native vs cotton:") print("Block + Extends, native vs cotton:")
# print(f"Native {{% block %}} and {{% extends %}}: {time_native_extends} seconds") print(f"Native {{% block %}} and {{% extends %}}: {time_native_extends} seconds")
# print(f"Uncompiled Cotton Template: {time_cotton} seconds") print(f"Uncompiled Cotton Template: {time_cotton} seconds")
# print(f"Compiled Cotton Template: {time_compiled_cotton} seconds") print(f"Compiled Cotton Template: {time_compiled_cotton} seconds")

View file

@ -38,11 +38,8 @@ class Loader(BaseLoader):
cached_content = self.cache_handler.get_cached_template(cache_key) cached_content = self.cache_handler.get_cached_template(cache_key)
if cached_content is not None: if cached_content is not None:
print("CACHE HIT", origin.name)
return cached_content return cached_content
print("CACHE MISS", origin.name)
template_string = self._get_template_string(origin.name) template_string = self._get_template_string(origin.name)
compiled_template = self.template_processor.process( compiled_template = self.template_processor.process(
template_string, origin.template_name template_string, origin.template_name

View file

@ -1,13 +1,24 @@
import ast import ast
from functools import lru_cache
from django import template from django import template
from django.template import Node from django.template import Node
from django.template.loader import render_to_string from django.template.loader import get_template
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django_cotton.utils import ensure_quoted from django_cotton.utils import ensure_quoted
@lru_cache(maxsize=128)
def get_cached_template(template_name):
return get_template(template_name)
def render_template(template_name, context):
template = get_cached_template(template_name)
return template.render(context)
def cotton_component(parser, token): def cotton_component(parser, token):
""" """
Template tag to render a cotton component with dynamic attributes. Template tag to render a cotton component with dynamic attributes.
@ -81,7 +92,7 @@ class CottonComponentNode(Node):
# Reset the component's slots in context to prevent bleeding into sibling components # Reset the component's slots in context to prevent bleeding into sibling components
all_slots[self.component_key] = {} all_slots[self.component_key] = {}
return render_to_string(self.template_path, local_context) return render_template(self.template_path, local_context)
def process_dynamic_attribute(self, value, context): def process_dynamic_attribute(self, value, context):
""" """

View file

@ -27,14 +27,14 @@ class InlineTestCase(CottonInlineTestCase):
def test_new_lines_in_attributes_are_preserved(self): def test_new_lines_in_attributes_are_preserved(self):
self.create_template( self.create_template(
"cotton/component.html", "cotton/preserved.html",
"""<div {{ attrs }}>{{ slot }}</div>""", """<div {{ attrs }}>{{ slot }}</div>""",
) )
self.create_template( self.create_template(
"view.html", "preserved_view.html",
""" """
<c-component x-data="{ <c-preserved x-data="{
attr1: 'im an attr', attr1: 'im an attr',
var1: 'im a var', var1: 'im a var',
method() { method() {
@ -45,7 +45,7 @@ class InlineTestCase(CottonInlineTestCase):
) )
# Register Url # Register Url
self.register_url("view/", self.make_view("view.html")) self.register_url("view/", self.make_view("preserved_view.html"))
# Override URLconf # Override URLconf
with self.settings(ROOT_URLCONF=self.get_url_conf()): with self.settings(ROOT_URLCONF=self.get_url_conf()):
@ -66,21 +66,21 @@ class InlineTestCase(CottonInlineTestCase):
self, self,
): ):
self.create_template( self.create_template(
"cotton/component.html", "cotton/hyphens.html",
""" """
<div x-data="{{ x_data }}" x-init="{{ x_init }}"></div> <div x-data="{{ x_data }}" x-init="{{ x_init }}"></div>
""", """,
) )
self.create_template( self.create_template(
"view.html", "hyphens_view.html",
""" """
<c-component x-data="{}" x-init="do_something()" /> <c-hyphens x-data="{}" x-init="do_something()" />
""", """,
) )
# Register Url # Register Url
self.register_url("view/", self.make_view("view.html")) self.register_url("view/", self.make_view("hyphens_view.html"))
# Override URLconf # Override URLconf
with self.settings(ROOT_URLCONF=self.get_url_conf()): with self.settings(ROOT_URLCONF=self.get_url_conf()):
@ -92,7 +92,7 @@ class InlineTestCase(CottonInlineTestCase):
self, self,
): ):
self.create_template( self.create_template(
"cotton/component.html", "cotton/cvar_hyphens.html",
""" """
<c-vars x-data="{}" x-init="do_something()" /> <c-vars x-data="{}" x-init="do_something()" />
@ -101,14 +101,14 @@ class InlineTestCase(CottonInlineTestCase):
) )
self.create_template( self.create_template(
"view.html", "cvar_hyphens_view.html",
""" """
<c-component /> <c-cvar-hyphens />
""", """,
) )
# Register Url # Register Url
self.register_url("view/", self.make_view("view.html")) self.register_url("view/", self.make_view("cvar_hyphens_view.html"))
# Override URLconf # Override URLconf
with self.settings(ROOT_URLCONF=self.get_url_conf()): with self.settings(ROOT_URLCONF=self.get_url_conf()):