Cache for _do_template call. May result in nice speed improvement (4-5 times faster).

This commit is contained in:
Yannig Perré 2015-11-01 22:30:55 +01:00
commit 87a9485b2f

View file

@ -122,6 +122,7 @@ class Templar:
self._filters = None
self._tests = None
self._available_variables = variables
self._cached_result = {}
if loader:
self._basedir = loader.get_basedir()
@ -298,8 +299,12 @@ class Templar:
elif resolved_val is None:
return C.DEFAULT_NULL_REPRESENTATION
# Using a cache in order to prevent template calls with already templated variables
cache_key = variable + str(preserve_trailing_newlines) + str(escape_backslashes) + str(overrides)
try:
result = self._cached_result[cache_key]
except KeyError:
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
if convert_data:
# if this looks like a dictionary or list, convert it to such using the safe_eval method
if (result.startswith("{") and not result.startswith(self.environment.variable_start_string)) or \
@ -310,6 +315,8 @@ class Templar:
else:
# FIXME: if the safe_eval raised an error, should we do something with it?
pass
self._cached_result[cache_key] = result
#return self._clean_data(result)
return result