Ticket #6262: cache_templates.diff

File cache_templates.diff, 3.3 KB (added by SmileyChris, 8 years ago)
  • home/chris/django/trunk/django/conf/global_settings.py

     
    157157#    'django.core.context_processors.request',
    158158)
    159159
     160# Number of seconds to cache templates retreived using the
     161# django.template.loader get_template method (0 means no caching).
     162# Templates are only cached if DEBUG is False.
     163CACHE_TEMPLATES = 600
     164
    160165# Output to use in template system for invalid (e.g. misspelled) variables.
    161166TEMPLATE_STRING_IF_INVALID = ''
    162167
  • home/chris/django/trunk/django/template/loader.py

     
    2121# installed, because pkg_resources is necessary to read eggs.
    2222
    2323from django.core.exceptions import ImproperlyConfigured
     24from django.core.cache import cache
    2425from django.template import Origin, Template, Context, TemplateDoesNotExist, add_to_builtins
    2526from django.conf import settings
    2627
     
    7576    """
    7677    Returns a compiled Template object for the given template name,
    7778    handling template inheritance recursively.
     79   
     80    If settings.DEBUG is False, settings.CACHE_TEMPLATES is non-zero and the
     81    template source is found, then the compiled template is cached.
    7882    """
     83    if settings.DEBUG:
     84        cache_template_duration = 0
     85    else:
     86        cache_template_duration = settings.CACHE_TEMPLATES
     87    if cache_template_duration:
     88        # Try to retreive the template from cache before looking through
     89        # template_source_loaders for it.
     90        cache_name = 'django.template.loader.%s' % template_name
     91        template = cache.get(cache_name)
     92        if template:
     93            return template
     94        elif template == 0:
     95            # This template couldn't be pickled, don't try again.
     96            cache_template_duration = 0
    7997    source, origin = find_template_source(template_name)
    8098    template = get_template_from_string(source, origin, template_name)
     99    if cache_template_duration:
     100        try:
     101            cache.set(cache_name, template, cache_template_duration)
     102        except TypeError:
     103            # Template couldn't be pickled, most likely because of an decorated
     104            # filter (Python 2.3 can't pickle any filter decorated with
     105            # stringfilter).
     106            cache.set(cache_name, 0, cache_template_duration)
    81107    return template
    82108
    83109def get_template_from_string(source, origin=None, name=None):
  • home/chris/django/trunk/docs/settings.txt

     
    269269The default number of seconds to cache a page when the caching middleware or
    270270``cache_page()`` decorator is used.
    271271
     272CACHE_TEMPLATES
     273---------------
     274
     275Default: ``600``
     276
     277The number of seconds to cache a template loaded and compiled from the file
     278system.
     279
     280Note that Django will only cache templates if ``DEBUG`` is ``False``.
     281
     282See also ``DEBUG``.
     283
    272284DATABASE_ENGINE
    273285---------------
    274286
Back to Top