Skip to content

Commit

Permalink
Change cache key definitiion in environment
Browse files Browse the repository at this point in the history
In 6671b97 the load_template method
was altered to use a cache key other than the template name. The key
chosen was the abs path as returned from the loader get_source
method. Unless there is no path in which case the name is
used. Unfortunately this introduced a performance regression, pallets#485, as
the get_source method (in the FileStoreLoader) loads the template
(causing IO).

The purpose of pallets#332 was to allow the loader to change whilst ensuring
the correct template was loaded, i.e. to fix this case

    env.loader = loader1
    env.get_template('index.html') # return loader1/index.html
    env.loader = loader2
    env.get_template('index.html') # also return loader1/index.html because of cache

This commit changes the cache key to be a combination of a hash of the
loader and the template name. Therefore fixing the above case without
calling the get_source method and thereby avoiding the IO load.
  • Loading branch information
pgjones committed May 8, 2016
1 parent b927c9f commit 38cbbf0
Showing 1 changed file with 1 addition and 9 deletions.
10 changes: 1 addition & 9 deletions jinja2/environment.py
Expand Up @@ -769,15 +769,7 @@ def join_path(self, template, parent):
def _load_template(self, name, globals):
if self.loader is None:
raise TypeError('no loader for this environment specified')
try:
# use abs path for cache key
cache_key = self.loader.get_source(self, name)[1]
except RuntimeError:
# if loader does not implement get_source()
cache_key = None
# if template is not file, use name for cache key
if cache_key is None:
cache_key = name
cache_key = "%d%s" % (hash(self.loader), name)
if self.cache is not None:
template = self.cache.get(cache_key)
if template is not None and (not self.auto_reload or
Expand Down

0 comments on commit 38cbbf0

Please sign in to comment.