Be more selective about what variables we cache during templating

Fixes #13087
This commit is contained in:
James Cammarata 2015-11-09 14:07:03 -05:00
parent 1a04f354f3
commit 7e04947599
2 changed files with 7 additions and 4 deletions

View file

@ -299,6 +299,7 @@ class Templar:
# Check to see if the string we are trying to render is just referencing a single # Check to see if the string we are trying to render is just referencing a single
# var. In this case we don't want to accidentally change the type of the variable # var. In this case we don't want to accidentally change the type of the variable
# to a string by using the jinja template renderer. We just want to pass it. # to a string by using the jinja template renderer. We just want to pass it.
var_name = None
only_one = self.SINGLE_VAR.match(variable) only_one = self.SINGLE_VAR.match(variable)
if only_one: if only_one:
var_name = only_one.group(1) var_name = only_one.group(1)
@ -313,7 +314,7 @@ class Templar:
variable_hash = sha1(text_type(variable).encode('utf-8')) variable_hash = sha1(text_type(variable).encode('utf-8'))
options_hash = sha1((text_type(preserve_trailing_newlines) + text_type(escape_backslashes) + text_type(fail_on_undefined) + text_type(overrides)).encode('utf-8')) options_hash = sha1((text_type(preserve_trailing_newlines) + text_type(escape_backslashes) + text_type(fail_on_undefined) + text_type(overrides)).encode('utf-8'))
sha1_hash = variable_hash.hexdigest() + options_hash.hexdigest() sha1_hash = variable_hash.hexdigest() + options_hash.hexdigest()
if sha1_hash in self._cached_result: if var_name not in (None, 'item') and sha1_hash in self._cached_result:
result = self._cached_result[sha1_hash] result = self._cached_result[sha1_hash]
else: else:
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides) result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
@ -327,10 +328,13 @@ class Templar:
else: else:
# FIXME: if the safe_eval raised an error, should we do something with it? # FIXME: if the safe_eval raised an error, should we do something with it?
pass pass
self._cached_result[sha1_hash] = result
# we only cache in the case where we have a single variable
# name, to make sure we're not putting things which may otherwise
# be dynamic in the cache (filters, lookups, etc.)
if var_name not in (None, 'item'):
self._cached_result[sha1_hash] = result
#return self._clean_data(result)
return result return result
elif isinstance(variable, (list, tuple)): elif isinstance(variable, (list, tuple)):

View file

@ -272,7 +272,6 @@ class VariableManager:
# we assume each item in the list is itself a list, as we # we assume each item in the list is itself a list, as we
# support "conditional includes" for vars_files, which mimics # support "conditional includes" for vars_files, which mimics
# the with_first_found mechanism. # the with_first_found mechanism.
#vars_file_list = templar.template(vars_file_item)
vars_file_list = vars_file_item vars_file_list = vars_file_item
if not isinstance(vars_file_list, list): if not isinstance(vars_file_list, list):
vars_file_list = [ vars_file_list ] vars_file_list = [ vars_file_list ]