Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache compiled templates within component by template_name #27

Closed
wants to merge 0 commits into from

Conversation

rbeard0330
Copy link
Contributor

As discussed in #26.

Only difference from what was discussed earlier is that I didn't invoke the compilation method when the template is compiled. It would require catching KeyErrors that are thrown when a client tries to look up something they expect to be in the context, and I don't think it will actually improve responsiveness--I'm not sure I have this right, but I think Django compiles a template immediately before the first time it renders it, so it's part of the first request-response cycle either way.

I used lru_cache for the caching, and defined a do-nothing decorator to avoid breaking Python 2.

Copy link
Owner

@EmilStenstrom EmilStenstrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very happy with this! We even got the simpler interface for creating Components in Python back too! I added a couple of comments, but they are minor.

def decorator(func):
return func
return decorator

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed support for Python 2, so this can be removed!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to get tests to pass by rebasing your code from latest master and force pushing to your branch.

@@ -55,15 +64,16 @@ def render_js_dependencies(self):
def slots_in_template(template):
return {node.name: node.nodelist for node in template.template.nodelist if is_slot_node(node)}

def compile_instance_template(self, slots_for_instance):
@lru_cache(maxsize=128)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a rationale for setting this to 128? Should be the number of unique times all your components get called with different values for template_name right?

Maybe we could make this configurable via the COMPONENTS setting that @danjac added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the default value, but I think it does make sense to make it configurable. I would bet that it won't matter for 95% of users, because they'll have < 10 templates ever used, but people who care at all will probably care a lot.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully agree. So we add a default to COMPONENTS and always read from there?

@@ -32,7 +32,7 @@ class Media:

comp = SimpleComponent("simple_component")
context = Context(comp.context(variable="test"))
comp.compile_instance_template({})
#comp.compile_instance_template({})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed, not commented out, right?

@EmilStenstrom
Copy link
Owner

I'm not sure that rebase worked. I see a merge commit and my own commits above. Anyways, and can select the commits I want in the list above and review them.

About the code! This looks great. I think we need to add a default for TEMPLATE_CACHE_SIZE in app_settings the same way the two other properties are set there. Otherwise we're good to go.

Thanks again! :)

@EmilStenstrom
Copy link
Owner

@rbeard0330 I messed up my git game, but your code is merged, I promise :) Thanks! Also released version 0.9 of the library with your fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants