summaryrefslogtreecommitdiff
path: root/jinja2/environment.py
diff options
context:
space:
mode:
authorpgjones <philip.graham.jones@googlemail.com>2016-05-08 16:10:35 +0100
committerpgjones <philip.graham.jones@googlemail.com>2016-05-19 19:42:16 +0100
commit7d8ec0f3e8c731c835f59825c1ad5b92c9339886 (patch)
tree37eeadb5f3b73246f371693edbc7cbda211e903d /jinja2/environment.py
parentb927c9f27115de078f143ea23f6fb25e7ae204cd (diff)
downloadjinja2-7d8ec0f3e8c731c835f59825c1ad5b92c9339886.tar.gz
Change cache key definitiion in environment
In 6671b973e6de5abc46829a27fd3bbb989d68ca3a 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, #485, as the get_source method (in the FileStoreLoader) loads the template (causing IO). The purpose of #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 tuple of the id(loader) and the template name. Therefore fixing the above case without calling the get_source method and thereby avoiding the IO load. A test has been added to ensure the above case works as expected, this required a minor refactor of the caching tests.
Diffstat (limited to 'jinja2/environment.py')
-rw-r--r--jinja2/environment.py10
1 files changed, 1 insertions, 9 deletions
diff --git a/jinja2/environment.py b/jinja2/environment.py
index 6d8c17d..67d05dd 100644
--- a/jinja2/environment.py
+++ b/jinja2/environment.py
@@ -769,15 +769,7 @@ class Environment(object):
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 = (id(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