On Thu, Aug 6, 2009 at 5:24 AM, Mike Malone<[email protected]> wrote:
> Hey everyone,
>
> I've been working on a patch for Django that would allow you to optionally
> cache templates after they've been lexed and parsed (compiled) by the
> template engine. I've got things far enough along that I have a working
> implementation, so I thought I'd share here and see if anyone had any
> thoughts / comments. I've attached my diff to ticket #6262, so
> visit http://code.djangoproject.com/ticket/6262 to check it out.
>
> I'd like to add template caching as a feature to Django 1.2, and am willing
> to do whatever is necessary to make this happen.

I haven't had a chance to dig into your patch in detail, but I'm
certainly interested in seeing template improvements like the ones you
describe. At the very least, it's worth putting on the list for
consideration for v1.2.

As is noted in the ticket, one of the reasons that this wasn't done
originally was that the performance boost wasn't seen as being that
considerable. You have provided some compelling stats for the complex
case (400+ template fragments) - but this is a slightly pathological
case. What happens in the simple case? What's the cost/benefit for for
rendering a simple template with just a small handful of fragments?
What about a single page with a very complex node parse tree? Stats
like that would be very helpful in making the case and getting over
any institutionalized lethargy regarding this change.

> By simply checking that the template has a ``render`` method in
> ``get_template``, we get the added benefit of allowing users to write
> loaders that return custom Template instances, or Template instances that
> use an alternative template language like Jinja or Mako.

I'd be particularly interested in hearing Jinja and Mako users weigh
in on this particular comment. If we can improve the accommodation for
external template engines with a relatively simple change to our
rendering, I'm all for it.

> Template-Tag State:
>
> In order to make cached templates usable in practice (and backwards
> compatible) some changes need to be made in the template tags as well.

This is the only part of your proposal that makes me nervous is the
template-tag bit. This isn't because your approach is obviously wrong
or anything like that - it's just a critical part of Django that we
don't want to accidentally cock up in a subtle but
backwards-incompatible way. This is one area that needs to be
inspected _very_ closely.

> Cached Template Loader Options:
>
> Once all this groundwork is done, we need to decide how to implement a
> caching template loader. There are several options:
>
> 1. Don't include one at all. Let users write their own and implement it
> however they want.
>
> 2. Implement a generic caching loader that can be instantiated with a list
> of loaders that it should try to use to load templates, caching the results.
> This requires a bit of a change to
> ``django.template.loader.find_template_source()`` since the current
> implementation assumes you're passing in a string containing the module
> path.
>
> 3. Implement a caching version of each existing template loader.
>
> I've implemented option 2, and I think it's probably the best alternative.

I'm inclined to agree. The decision to cache a template should be
independent of the template loader that is used. Option 1 requires
everyone to reinvent the wheel. Option 3 means that anyone with a
custom template loader would need to implement a second cache-enabled
version. Option 2 means you can wrap the underlying template loader in
a "and cache it while you're at it" layer that is separated from the
loader itself.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to